Pyspark收集/分组嵌套数据

问题描述

我正在使用Spark 2.4.5中的研究论文元数据,示例是:

dataframe picture

我需要在df2 # A tibble: 8 x 3 col1 col2 comments <chr> <dbl> <chr> 1 a 1 "fitz" 2 a 1 "buzz" 3 c 3 "hello world" 4 c 3 "today is Thursday " 5 c 3 " its 2:00" 6 c 3 "another comment" 7 c 3 "and yet another comment" 8 d 4 "fitz" 上分组,并收集关联的auideids(从从属关系列),affiliation ids(我从其他df加入,因为该df是不完整的) ,以便以后追踪隶属城市随时间的变化。因此,最后,我可能选择了affiliation citiesauideidyearaffil_id的结构。 我尝试了几件事,但对结果并不满意,所以在这里寻求支持。如果需要,我可以以文本形式发布df示例。

解决方法

有效的

df.groupBy('auid').agg(func.collect_set(func.struct('eid','Year','affiliation.affiliation_organization','affiliation.affiliation_city','affiliation.afid')).alias('eid_attr_set'))