【发布时间】:2020-01-12 14:59:53
【问题描述】:
我正在尝试在 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 组成的,我该如何使用它? 稍后,我需要动态分解这个字段/数组。
【问题讨论】:
标签: java json apache-spark bigdata