pyspark - 使用 pyspark 读取复杂的 json 模式
问题描述
我正在将 json 文档读入数据框。但是,它的格式很复杂。我能够使用explode 函数来获取值。
root
|-- Name: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- Adap: string (nullable = true)
| | |-- Vid: string (nullable = true)
|-- Information: struct (nullable = true)
| |-- Caption: string (nullable = true)
| |-- No: string (nullable = true)
|-- License: struct (nullable = true)
| |-- Out: struct (nullable = true)
| | |-- ID: string (nullable = true)
| |-- In: struct (nullable = true)
| | |-- INS: string (nullable = true)
虽然 Json 很大,我不想手动编写所有内容。我所做的方式适用于所有值:
mdmDF.withColumn("Name", explode("Name")).select(col("Name")["Adap"].alias("Name.Adap")))
其次,我尝试了:虽然它每列只给我一个数据框。!
Name= mdmDF.selectExpr("explode(Name) AS Name").selectExpr("Name.*")
+------------------+----------+
| Name | Adap|
+------------------+----------+
|NVIDIA | 0|
+------------------+----------+
我想要的是:
+------------------+----------+----------+----------+----------+----------+
| adap | vid | Caption | no | Out | In |
+------------------+----------+----------+----------+----------+----------+
|NVIDIA | 0| test| 1 | etx | val |
+------------------+----------+----------+----------+----------+----------+
解决方案
推荐阅读
- python - TypeError("所有调色板列表元素必须是颜色字符串") TypeError: 所有调色板列表元素必须是颜色字符串
- html - 将视频浮动到我的网页右侧
- rust - 响应类型与字符串不兼容。”(响应为零)查询 redis 时
- r - 如何使用冲积图(或桑基图)使用 R 显示类别随时间的变化
- python - urllib.request.urlopen 错误但请求变得有用
- javascript - 通过上下文在子组件中设置父组件的状态会导致 TypeError: Invalid attempt to destructure non-iterable instance
- javafx - javafx vlcj播放多个视频得到IndexOutOfBoundsException错误
- python - 管理存储在数据框中的一些字段数据
- rx-swift - 在什么情况下可以使用 zip 和 combineLatest?RxSwift
- angularjs - 我如何将 angular.forEach 存储到表中?