【发布时间】:2021-01-29 14:55:32
【问题描述】:
如何从 pyspark 中的 spark 数据帧行中解析和转换 json 字符串?
我正在寻求如何解析的帮助:
- json 字符串到 json 结构
output 1 - 将json字符串转换为a、b和id列
output 2
背景:我通过 API 获取具有大量行的 json 字符串(jstr1、jstr2、...),这些行保存到 spark df。我可以分别读取每一行的模式,但这不是解决方案,因为模式有大量行,所以速度很慢。每个 jstr 具有相同的架构,列/键 a 和 b 保持不变,只是 id 和列中的值发生变化。
编辑:使用 MapType 架构的 blackbishop 解决方案就像一个魅力 schema = "map<string, array<struct<a:int,b:int>>>"
问题扩展到: How to transform JSON string with multiple keys, from spark data frame rows in pyspark?
from pyspark.sql import Row
jstr1 = '{"id_1": [{"a": 1, "b": 2}, {"a": 3, "b": 4}]}'
jstr2 = '{"id_2": [{"a": 5, "b": 6}, {"a": 7, "b": 8}]}'
df = sqlContext.createDataFrame([Row(json=jstr1),Row(json=jstr2)])
schema = F.schema_of_json(df.select(F.col("json")).take(1)[0].json)
df2 = df.withColumn('json', F.from_json(F.col('json'), schema))
df2.show()
当前输出:
+--------------------+
| json|
+--------------------+
|[[[1, 2], [3, 4]]] |
| []|
+--------------------+
所需的输出 1:
+--------------------+-------+
| json | id |
+--------------------+-------+
|[[[1, 2], [3, 4]]] | id_1 |
|[[[5, 6], [7, 8]]] | id_2 |
+--------------------+-------+
所需的输出 2:
+---------+----------+-------+
| a | b | id |
+--------------------+-------+
| 1 | 2 | id_1 |
| 3 | 4 | id_1 |
| 5 | 6 | id_2 |
| 7 | 8 | id_2 |
+---------+----------+-------+
【问题讨论】:
-
您能否指定从问题中的 api 调用获得的 json 行输出?或者你是否在一次 api 调用中得到一个 json 字符串?
-
这能回答你的问题吗? Pyspark: Parse a column of json strings
-
@Chris 这不能回答问题,因为必须使用 MapType 模式来解决问题
标签: python apache-spark pyspark apache-spark-sql