json - 从 Spark 2.3 开始,当引用的列仅包含内部损坏的记录列时,不允许从原始 JSON/CSV 文件进行查询

标签 json scala apache-spark apache-spark-sql

我有一个 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/

相关文章:

hadoop - Spark RDD 持久化和分区

c# - JToken.ToObject<T>() 与 JToken.Value<T>() 之间的区别

scala - Spark 2.0数据集groupByKey并对操作和类型安全进行划分

javascript - 如何在 Javascript 中将 JSON 转换为数组

scala - 如何使用 scalatra 通过 sbt-assembly 创建包含 webapp 资源的可执行单个 jar

scala - 当加特林负载测试表现不佳时如何使 Jenkins 工作失败

scala - isin 在 Spark 中的 withcolumn 函数中抛出 stackoverflow 错误

scala - Spark 结构化流 + Kafka 集成 : MicroBatchExecution PartitionOffsets Error

php - 如何将嵌套的 php 数组放入 javascript 中?

javascript - JavaScript中如何解析JSON取值