【问题标题】:Weird error while parsing JSON in Apache Spark在 Apache Spark 中解析 JSON 时出现奇怪的错误
【发布时间】:2019-06-28 06:57:40
【问题描述】:

尝试解析 JSON 文档,Spark 给我一个错误:

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().;
at org.apache.spark.sql.execution.datasources.json.JsonFileFormat.buildReader(JsonFileFormat.scala:120)
...
at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:73)
at org.apache.spark.sql.Dataset.withAction(Dataset.scala:3364)
at org.apache.spark.sql.Dataset.head(Dataset.scala:2545)
at org.apache.spark.sql.Dataset.take(Dataset.scala:2759)
at org.apache.spark.sql.Dataset.getRows(Dataset.scala:255)
at org.apache.spark.sql.Dataset.showString(Dataset.scala:292)
at org.apache.spark.sql.Dataset.show(Dataset.scala:746)
at org.apache.spark.sql.Dataset.show(Dataset.scala:705)
at xxx.MyClass.xxx(MyClass.java:25)

我已经尝试在几个在线编辑器中打开 JSON 文档并且它是有效的。

这是我的代码:

Dataset<Row> df = spark.read()
    .format("json")
    .load("file.json");

df.show(3); // this is line 25

我正在使用 Java 8 和 Spark 2.4。

【问题讨论】:

    标签: java json apache-spark


    【解决方案1】:

    _corrupt_record 列是 Spark 在尝试提取错误记录时存储它们的位置。这可能是一个暗示。

    Spark 还处理两种类型的 JSON 文档,JSON Lines 和普通 JSON(在早期版本中,Spark 只能处理 JSON Lines)。您可以在此Manning article 中找到更多信息。

    您可以尝试multiline 选项,如:

    Dataset<Row> df = spark.read()
        .format("json")
        .option("multiline", true)
        .load("file.json");
    

    看看有没有帮助。如果没有,请分享您的 JSON 文档(如果可以)。

    【讨论】:

      【解决方案2】:

      将多行选项设置为 true。如果它不起作用,请分享您的 json

      【讨论】:

      • 请解释这是如何回答问题的。这应该是一条评论。
      • 具有相同的错误跟踪。我将多行设置为true,然后它起作用了。查看此链接issues.apache.org/jira/browse/SPARK-18352 并注意如果存在 json 解析异常,我们会收到此错误
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多