假设我有以下数据帧:
scala> val dataset = Seq((0, "hello"), (0, "world"), (0, "hello"), (1, "foo"), (1, "bar")).toDF("id", "text")
dataset: org.apache.spark.sql.DataFrame = [id: int, text: string]
scala> dataset.show()
+---+-----+
| id| text|
+---+-----+
| 0|hello|
| 0|world|
| 0|hello|
| 1| foo|
| 1| bar|
+---+-----+
我如何获得text
每个的不同id
?即,我想做类似的事情:
dataset.groupBy("id").agg(distinct('text))
我必须创建一个UserDefinedAggregateFunction
吗?我最终想要每个id
.
您可以使用collect_set
:
dataset.groupBy("id").agg(collect_set($"text")).show
+---+-----------------+
| id|collect_set(text)|
+---+-----------------+
| 1| [bar, foo]|
| 0| [world, hello]|
+---+-----------------+
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句