簡體   English   中英

在 Apache Spark 中解析 JSON 時出現奇怪的錯誤

[英]Weird error while parsing JSON in Apache Spark

嘗試解析 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。

_corrupt_record列是 Spark 在嘗試攝取它們時存儲格式錯誤的記錄的地方。 這可能是一個提示。

Spark 還處理兩種類型的 JSON 文檔,JSON Lines 和普通 JSON(在早期版本中 Spark 只能處理 JSON Lines)。 您可以在這篇曼寧文章中找到更多信息。

您可以嘗試multiline選項,如下所示:

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

看看它是否有幫助。 如果沒有,請分享您的 JSON 文檔(如果可以)。

將多行選項設置為 true。 如果它不起作用,請分享您的 json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM