首页 > 解决方案 > 尝试使用 SPARK 1.6 (WrappedArray) 从嵌套 JSON 中收集值

问题描述

我正在尝试在 Dataframe 中转换一个 json 文件,但我被困在一个基于数组的字段中。我正在使用 Spark 1.6 和 Java。当我读取嵌套的 Json 并转换为 Dataframe 时,我可以读取一些字段,但是当我尝试输入特定路径时会显示错误。

DataFrame df = spark.read().json(sc.wholeTextFiles("PATH").values());

我正在阅读一个 json 文件。

df.select(col("orcamentos.itens")).printSchema();

root
 |-- itens: array (nullable = true)
 |    |-- element: array (containsNull = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- criticas: array (nullable = true)
 |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |-- codigo: long (nullable = true)
 |    |    |    |    |    |-- codigoCenario: string (nullable = true)
 |    |    |    |    |    |-- codigodevolutiva: long (nullable = true)
 |    |    |    |    |    |-- descricao: string (nullable = true)
 |    |    |    |    |    |-- flagLiberacao: string (nullable = true)
 |    |    |    |    |    |-- statusCenario: string (nullable = true)
 |    |    |    |-- devolutivas: array (nullable = true)
 |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |-- codigo: long (nullable = true)
 |    |    |    |    |    |-- descricao: string (nullable = true)
 |    |    |    |    |    |-- texto: string (nullable = true)
 |    |    |    |    |    |-- tipo: struct (nullable = true)
 |    |    |    |    |    |    |-- codigo: long (nullable = true)
 |    |    |    |    |    |    |-- descricao: string (nullable = true)
 |    |    |    |-- numeroItem: long (nullable = true)
 |    |    |    |-- rastreador: struct (nullable = true)
 |    |    |    |    |-- concessao: struct (nullable = true)
 |    |    |    |    |    |-- codigo: long (nullable = true)
 |    |    |    |    |    |-- descricao: string (nullable = true)
 |    |    |    |    |-- dispositivo: struct (nullable = true)
 |    |    |    |    |    |-- codigo: long (nullable = true)
 |    |    |    |    |    |-- descricao: string (nullable = true)

我正在尝试使用以下内容打印字段“numeroItem”:

df.select(col("orcamentos.itens.numeroItem")).show();

但我收到以下错误:

Exception in thread "main" org.apache.spark.sql.AnalysisException: cannot resolve 'orcamentos.itens[numeroItem]' due to data type mismatch: argument 2 requires integral type, however, 'numeroItem' is of string type.;
    at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:42)
    at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$$anonfun$checkAnalysis$1$$anonfun$apply$2.applyOrElse(CheckAnalysis.scala:65)
    at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$$anonfun$checkAnalysis$1$$anonfun$apply$2.applyOrElse(CheckAnalysis.scala:57)

当我尝试仅打印上面的一个节点(numeroItem 的父亲)时,从数据框中返回了该列,如下所示:

df.select(col("orcamentos.itens")).show();

+---------------------------------------+
|itens                                  |
+---------------------------------------+
|[WrappedArray([WrappedArray([5000,3,4,D|
+---------------------------------------+

如果这个字段是由 WrappedArray 组成的,我该如何使用它?稍后,我将需要动态分解此字段/数组。

标签: javajsonapache-sparkbigdata

解决方案


如果你可以修改 json 输入,你可以这样做:

在您的 json 文件中,您将数组放在一个大对象中。为了让 spark 能够将数组视为 DataFrame 中的单独行,您应该将数组作为根元素。

所以不是{"itens":[<element1>,...,<elementN>]},而是[<element1>, ..., <elementN>]


推荐阅读