I am trying to read the Json
data by using Apache Spark
. Here is the code what i have tried so far:
val conf = new SparkConf()
.setAppName("ExplodeDemo")
.setMaster("local")
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
import sqlContext.implicits._
val df = sqlContext.read.json("file location")
df.printSchema()
which works well when i pass the file name as a argument to the sqlContext.read.json
, but my requirement is to pass the json String
directly insted of file.
for that i tried i tried like this:
val rdd = sc.parallelize(Seq(r))
val df = sqlContext.read.json(rdd)
df.printSchema()
where r
is my json String, by using this code, there are no compilation errors. But when i tried df.printSchema() it shows like this, and not able to retreive the data.
root
|-- _corrupt_record: string (nullable = true)