且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从数据块中的数组列获取数据,而无需交叉联接

更新时间:2023-09-26 21:44:34

尝试 inline :

df.selectExpr('id', 'inline(array_col)').show()
+---+------+-----+
| id|system|value|
+---+------+-----+
|101|     x|    1|
|101|     y|    2|
|101|     z|    3|
+---+------+-----+

以上假设数组包含结构,而不是字符串结构.如果您的结构是字符串,则需要先使用 from_json 解析它们:

The above assumes that the arrays contains structs, not structs as strings. If your structs are strings, you need to parse them with from_json first:

df2 = df.selectExpr(
    'id', 'explode(array_col) array_col'
).selectExpr(
    'id', "inline(array(from_json(array_col, 'struct<system:string, value:string>')))"
)

df2.show()
+---+------+-----+
| id|system|value|
+---+------+-----+
|101|     x|    1|
|101|     y|    2|
|101|     z|    3|
+---+------+-----+