如何在pyspark中将arraytype的列转换为dictionary

czq61nw1  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(332)

我在一个Dataframe上做collect\u list,这个Dataframe产生所需的 column(TASourceId) 具体如下:

[{"TASourceId": "Source1", "flag": "true"}, {"TASourceId": "Source2", "flag": "true"}]

查询:

SELECT col1, col2,col3, collect_list(struct(TASourceId, flag)) as TASourceId
FROM table
GROUP BY 1,2,3

有没有可能获得dictionary对象的dictionary作为列的输出,如下所示?

{"col1":{"TASourceId": "Source1", "flag": "true"}, "col2":{"TASourceId": "Source2", "flag": "true"}}

我想用 spark UDF 获取此项,但出现以下错误:
类dict错误应为零参数。

5sxhfpxr

5sxhfpxr1#

我取了一个样本作为你的数组,并将其转换为字典。

a=[{"TASourceId": "Source1", "flag": "true"}, {"TASourceId": "Source2", "flag": "true"}]
s={}
for i in range(0,len(a)):
    s["col"+str(i)]=a[i]
print(s)

相关问题