Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何在Spark中对GroupedData执行自定义操作?_Scala_Apache Spark_Grouping - Fatal编程技术网

Scala 如何在Spark中对GroupedData执行自定义操作?

Scala 如何在Spark中对GroupedData执行自定义操作?,scala,apache-spark,grouping,Scala,Apache Spark,Grouping,我想重写一些用RDD编写的代码以使用数据帧。在我发现这一点之前,它工作得相当顺利: events .keyBy(row => (row.getServiceId + row.getClientCreateTimestamp + row.getClientId, row) ) .reduceByKey((e1, e2) => if(e1.getClientSendTimestamp <= e2.getClientSendTimestamp) e1 else e2)

我想重写一些用RDD编写的代码以使用数据帧。在我发现这一点之前,它工作得相当顺利:

 events
  .keyBy(row => (row.getServiceId + row.getClientCreateTimestamp + row.getClientId, row) )
  .reduceByKey((e1, e2) => if(e1.getClientSendTimestamp <= e2.getClientSendTimestamp) e1 else e2)
  .values
但是接下来呢?如果我想迭代当前组中的每个元素呢?有可能吗?
提前谢谢

GroupedData
不能直接使用。数据不是物理分组的,它只是一个逻辑操作。您必须应用
agg
方法的一些变体,例如:

events
 .groupBy($"service_id", $"client_create_timestamp", $"client_id")
 .min("client_send_timestamp")

其中,
client\u send\u timestamp
是要聚合的列

如果要保留信息而不是聚合,只需加入或使用窗口函数-请参阅

Spark还支持用户定义的聚合函数-请参阅

Spark 2.0+

您可以使用将组公开为迭代器的
Dataset.groupByKey

events
 .groupBy($"service_id", $"client_create_timestamp", $"client_id")
 .min("client_send_timestamp")
events
 .groupBy($"service_id", $"client_create_timestamp", $"client_id")
 .agg(min($"client_send_timestamp"))