简体   繁体   English

限制 spark scala 中 collect_list 项目的数量

[英]Limiting number of collect_list items in spark scala

I have code that states我有代码说明

db.groupBy("ids")
  .agg(collect_list("names") as "alias")
  .select("ids", "alias")

However, there are some rows where the alias would be only 1 entry large, while others could be 200.但是,有些行的别名只有 1 个条目大,而其他可能是 200 个条目。

How can I limit the ones with >30 entries to a maximum of 30 entries?如何将 > 30 个条目的条目限制为最多 30 个条目?

You can use the slice function to subset the array.您可以使用slice函数对数组进行子集化。

db.groupBy("ids")                       
  .agg(slice(collect_list("names"), 1, 30) as "alias")
  .select("ids", "alias")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM