【问题标题】:Avoid parsing json subfield in Spark避免在 Spark 中解析 json 子字段
【发布时间】:2021-12-28 06:58:15
【问题描述】:

我正在使用 Spark 读取具有复杂架构(见下文)的 json 文件。我发现源数据中的某些字段重复,因此 Spark 在读取期间抛出错误(如预期的那样)。重复的名称位于storageidlist 字段下。我想做的是将storageidlist字段作为未解析的字符串加载到字符串类型列中,然后手动解析。这在 Spark 中是否可行?

root
 |-- errorcode: string (nullable = true)
 |-- errormessage: string (nullable = true)
 |-- ip: string (nullable = true)
 |-- label: string (nullable = true)
 |-- status: string (nullable = true)
 |-- storageidlist: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- errorcode: string (nullable = true)
 |    |    |-- errormessage: string (nullable = true)
 |    |    |-- fedirectorList: array (nullable = true)
 |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |-- directorId: string (nullable = true)
 |    |    |    |    |-- errorcode: string (nullable = true)
 |    |    |    |    |-- errordesc: string (nullable = true)
 |    |    |    |    |-- metrics: string (nullable = true)
 |    |    |    |    |-- portMetricDataList: array (nullable = true)
 |    |    |    |    |    |-- element: array (containsNull = true)
 |    |    |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |    |    |-- data: array (nullable = true)
 |    |    |    |    |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |    |    |    |    |-- ts: string (nullable = true)
 |    |    |    |    |    |    |    |    |    |-- value: string (nullable = true)
 |    |    |    |    |    |    |    |-- errorcode: string (nullable = true)
 |    |    |    |    |    |    |    |-- errordesc: string (nullable = true)
 |    |    |    |    |    |    |    |-- metricid: string (nullable = true)
 |    |    |    |    |    |    |    |-- portid: string (nullable = true)
 |    |    |    |    |    |    |    |-- status: string (nullable = true)
 |    |    |    |    |-- status: string (nullable = true)
 |    |    |-- metrics: string (nullable = true)
 |    |    |-- status: string (nullable = true)
 |    |    |-- storageGroupList: string (nullable = true)
 |    |    |-- storageid: string (nullable = true)
 |-- sublabel: string (nullable = true)
 |-- ts: string (nullable = true)

【问题讨论】:

    标签: json apache-spark schema


    【解决方案1】:

    其中一个选项是为此 JSON 对象创建一个 Java 类。 这样,您可以读取输入 JSON,并且 spark 在读取过程中不会抛出错误。只要您定义的架构与输入架构匹配,就允许重复。

        spark.read()
                .schema(Encoders.bean(YourPOJO.class).schema())
                .option("encoding", "UTF-8")
                .option("mode", "FAILFAST")
                .json("data.json")
                .as(Encoders.bean(YourPOJO.class));
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2020-08-21
      相关资源
      最近更新 更多