【问题标题】:Spark GroupBy agg collect_list multiple columnsSpark GroupBy agg collect_list 多列
【发布时间】:2018-07-23 09:33:03
【问题描述】:

我有一个类似于this 的问题,但是collect_list 操作的列数由一个名单给出。例如:

scala> w.show
+---+-----+----+-----+
|iid|event|date|place|
+---+-----+----+-----+
|  A|   D1|  T0|   P1|
|  A|   D0|  T1|   P2|
|  B|   Y1|  T0|   P3|
|  B|   Y2|  T2|   P3|
|  C|   H1|  T0|   P5|
|  C|   H0|  T9|   P5|
|  B|   Y0|  T1|   P2|
|  B|   H1|  T3|   P6|
|  D|   H1|  T2|   P4|
+---+-----+----+-----+


scala> val combList = List("event", "date", "place")
combList: List[String] = List(event, date, place)

scala> val v = w.groupBy("iid").agg(collect_list(combList(0)), collect_list(combList(1)), collect_list(combList(2)))
v: org.apache.spark.sql.DataFrame = [iid: string, collect_list(event): array<string> ... 2 more fields]

scala> v.show
+---+-------------------+------------------+-------------------+
|iid|collect_list(event)|collect_list(date)|collect_list(place)|
+---+-------------------+------------------+-------------------+
|  B|   [Y1, Y2, Y0, H1]|  [T0, T2, T1, T3]|   [P3, P3, P2, P6]|
|  D|               [H1]|              [T2]|               [P4]|
|  C|           [H1, H0]|          [T0, T9]|           [P5, P5]|
|  A|           [D1, D0]|          [T0, T1]|           [P1, P2]|
+---+-------------------+------------------+-------------------+

有什么方法可以在不知道combList 中元素数量的情况下将collect_list 应用于agg 内的多个列?

【问题讨论】:

  • 嘿乔纳森,你知道了吗?我遇到了类似的问题
  • 恐怕我没有找到解决方案,但请注意,这种方法可能无法很好地适应大量数据。
  • 谢谢!我通过使用 dicts、for 循环和连接找到了解决方法。它实际上可以很好地扩展到多达 20 亿行和 30 列。如果我有时间发布图书馆,我会及时通知您
  • 也许这会有所帮助 - enter link description here

标签: group-by spark-dataframe aggregate


【解决方案1】:

您可以使用 collect_list(struct(col1, col2)) AS 元素。

例子:

df.select("cd_issuer", "cd_doc", "cd_item", "nm_item").printSchema
val outputDf = spark.sql(s"SELECT cd_issuer, cd_doc, collect_list(struct(cd_item, nm_item)) AS item FROM teste GROUP BY cd_issuer, cd_doc")
outputDf.printSchema

df
 |-- cd_issuer: string (nullable = true)
 |-- cd_doc: string (nullable = true)
 |-- cd_item: string (nullable = true)
 |-- nm_item: string (nullable = true)

outputDf
|-- cd_issuer: string (nullable = true)
|-- cd_doc: string (nullable = true)
|-- item: array (nullable = true)
|    |-- element: struct (containsNull = true)
|    |    |-- cd_item: string (nullable = true)
|    |    |-- nm_item: string (nullable = true)

【讨论】:

  • 我一直在寻找一个例子,其中collect_list只有一个我找到了。
猜你喜欢
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
相关资源
最近更新 更多