我有一个 json 文件:
{
"a": {
"b": 1
}
}
我正在尝试阅读它:val path = "D:/playground/input.json"
val df = spark.read.json(path)
df.show()
但是得到一个错误:Exception in thread "main" org.apache.spark.sql.AnalysisException: Since Spark 2.3, the queries from raw JSON/CSV files are disallowed when the referenced columns only include the internal corrupt record column (named _corrupt_record by default). For example: spark.read.schema(schema).json(file).filter($"_corrupt_record".isNotNull).count() and spark.read.schema(schema).json(file).select("_corrupt_record").show(). Instead, you can cache or save the parsed results and then send the same query. For example, val df = spark.read.schema(schema).json(file).cache() and then df.filter($"_corrupt_record".isNotNull).count().;
所以我尝试按照他们的建议缓存它:
val path = "D:/playground/input.json"
val df = spark.read.json(path).cache()
df.show()
但我不断收到同样的错误。
最佳答案
您可以尝试这两种方式中的任何一种。
选项 1 : JSON 单行,如@Avishek Bhattacharya 上面的回答。
选项 2 : 在代码中添加读取多行 JSON 的选项,如下所示。您也可以阅读嵌套属性,如下所示。
val df = spark.read.option("multiline","true").json("C:\\data\\nested-data.json")
df.select("a.b").show()
这是选项 2 的输出。20/07/29 23:14:35 INFO DAGScheduler: Job 1 finished: show at NestedJsonReader.scala:23, took 0.181579 s
+---+
| b|
+---+
| 1|
+---+
关于json - 从 Spark 2.3 开始,当引用的列仅包含内部损坏的记录列时,不允许从原始 JSON/CSV 文件进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57451719/