Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading multiple json files from Spark

Tags:

apache-spark

I have a list of json files which I would like load in parallel.

I can't use read.json("*") cause files are not in the same folder and there is no specific pattern I can implement.

I've tried sc.parallelize(fileList).select(hiveContext.read.json) but hive context, as expected, doesn't exists in executor.

Any ideas?

like image 232
Roman Kagan Avatar asked Apr 25 '16 08:04

Roman Kagan


3 Answers

Looks like I found the solution:

val text sc.textFile("file1,file2....")
val df = sqlContext.read.json(text)
like image 124
Roman Kagan Avatar answered Nov 19 '22 06:11

Roman Kagan


Also, you can specify directory as a parameter:

cat 1.json
{"x": 1.0, "y": 2.0}
{"x": 1.5, "y": 1.0}
sudo -u hdfs hdfs dfs -put 1.json /tmp/test

cat 2.json
{"x": 3.0, "y": 4.0}
{"x": 1.8, "y": 7.0}
sudo -u hdfs hdfs dfs -put 2.json /tmp/test

sqlContext.read.json("/tmp/test").show()
+---+---+
|  x|  y|
+---+---+
|1.0|2.0|
|1.5|1.0|
|3.0|4.0|
|1.8|7.0|
+---+---+    
like image 2
Vitalii Kotliarenko Avatar answered Nov 19 '22 08:11

Vitalii Kotliarenko


Function json(paths:String*) takes variable arguments. (documentation)

So you can change your code like this:

sc.read.json(file1, file2, ...)
like image 2
dmigo Avatar answered Nov 19 '22 08:11

dmigo