当前位置:首页 > Web开发 > 正文

返回Nonecase None = println("Parsing failed")}) }} 原始json数据: {

2024-03-31 Web开发

import org.apache.spark.{SparkConf, SparkContext} import scala.util.parsing.json.JSON object JSONParse { def main(args: Array[String]): Unit = { val inputFileName = "file:///Users/walker/learn/mycode/spark/test_data/people.json" val conf = new SparkConf().setAppName("JSONParse").setMaster("local") val sc = new SparkContext(conf) val jsonStrRDD = sc.textFile(inputFileName) val parsedResult = jsonStrRDD.map(JSON.parseFull(_)) //jsonStrRDD.map(line => JSON.parseFull(line)) parsedResult.foreach( r => r match { // 解析告成,返回Some(map: Map[String,Any]) case Some(map: Map[String,Any]) => println(map) // 解析弗成功,,返回None case None => println("Parsing failed") } ) } }

原始json数据:

{"name":"Michael"} {"name":"Andy", "age":30} {"name":"Justin", "age":19} {"name":"Justin", "age":19,hello} {57657:12345, "age":19}

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30068.html