java - 尝试使用 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 组成的,我该如何使用它?稍后,我将需要动态分解此字段/数组。
解决方案
如果你可以修改 json 输入,你可以这样做:
在您的 json 文件中,您将数组放在一个大对象中。为了让 spark 能够将数组视为 DataFrame 中的单独行,您应该将数组作为根元素。
所以不是{"itens":[<element1>,...,<elementN>]}
,而是[<element1>, ..., <elementN>]
推荐阅读
- javascript - 错误 er_con_count_error 连接数过多表示 mysql
- api - 使用 Django REST 框架序列化 MPTT 树
- ios - Swift 回到根视图控制器
- php - 如何将传单多边形框放大 5%?
- python-3.x - 取最近的日期和最远的日期,并使用 .groupby 计算它们之间的月份
- javascript - 如何将 URL 查询解析到 mysql 数据库中?使用 node.js 和 express
- angular - 如何全局使用 scss 文件?
- python - Python:取消链接所有库并删除所有数据
- python - 关于时间序列数据python的条件语句
- python - 如何使用 findall 查找所有信息