[英]Avoid parsing json subfield in Spark
我有 json 文件,其中包含我正在使用 Spark 讀取的復雜模式(見下文)。 我發現源數據中的某些字段重復,因此 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 ZA8CFDE6331BD59EB26AC96F8911ZB4 創建 Java Class。 這樣,您可以讀取輸入 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));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.