2

I have a JSON file like this:

{"ID": "1234566", "Name": "abcd", "Hobby": "run"}

How can I parse that file and get the ID and Hobby?

Pang
  • 9,564
  • 146
  • 81
  • 122
DaehyunPark
  • 77
  • 2
  • 6

1 Answers1

1

You can read json file using sqlContext.read.json(input) in Spark.

Sample code with Spark version 1.6.2:

import org.apache.spark._
import org.apache.spark.sql.SQLContext;  

object JsonParser {
    val conf = new SparkConf().setAppName("Spark json extract")

             // Set this for debug mode on eclipse 
              conf.setMaster("local");

    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)

    val input = "C:\\Users\\json_extract\\test1.json"

    def main(args: Array[String]): Unit = {
      val df = sqlContext.read.json(input)
      df.registerTempTable("jsonExtract")

      val data = sqlContext.sql("select * from jsonExtract")
      data.show();
    sc.stop

  }
}
Sandeep Singh
  • 7,790
  • 4
  • 43
  • 68