I want to convert string variable below to dataframe on spark.
val jsonStr = "{ "metadata": { "key": 84896, "value": 54 }}"
I know how to create dataframe from json file.
sqlContext.read.json("file.json")
but I don't know how to create dataframe from string variable.
How can I convert json String variable to dataframe.
Spark SQL can automatically infer the schema of a JSON dataset and load it as a DataFrame. using the read. json() function, which loads data from a directory of JSON files where each line of the files is a JSON object. Note that the file that is offered as a json file is not a typical JSON file.
For Spark 2.2+:
import spark.implicits._ val jsonStr = """{ "metadata": { "key": 84896, "value": 54 }}""" val df = spark.read.json(Seq(jsonStr).toDS)
For Spark 2.1.x:
val events = sc.parallelize("""{"action":"create","timestamp":"2016-01-07T00:01:17Z"}""" :: Nil) val df = sqlContext.read.json(events)
Hint: this is using
sqlContext.read.json(jsonRDD: RDD[Stirng])
overload. There is alsosqlContext.read.json(path: String)
where it reads a Json file directly.
For older versions:
val jsonStr = """{ "metadata": { "key": 84896, "value": 54 }}""" val rdd = sc.parallelize(Seq(jsonStr)) val df = sqlContext.read.json(rdd)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With