通过Spark组查找时间戳的最小值 [英] Find minimum for a timestamp through Spark groupBy dataframe

查看:183
本文介绍了通过Spark组查找时间戳的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将我的数据框分组到一列上,然后尝试找到每个分组groupbyDatafram.min('timestampCol')的最小值时,我似乎无法在非数字列上做到这一点.然后,如何正确过滤分组依据上的最短(最早)日期?

When I try to group my dataframe on a column then try to find the minimum for each grouping groupbyDatafram.min('timestampCol') it appears I cannot do it on non numerical columns. Then how can I properly filter the minimum (earliest) date on the groupby?

我正在从Postgresql S3实例流式传输数据帧,因此已经配置了数据.

I am streaming the dataframe from a postgresql S3 instance, so that data is already configured.

推荐答案

只需直接执行聚合即可,而不是使用min帮助程序:

Just perform aggregation directly instead of using min helper:

import org.apache.spark.sql.functions.min

val sqlContext: SQLContext = ???

import sqlContext.implicits._

val df = Seq((1L, "2016-04-05 15:10:00"), (1L, "2014-01-01 15:10:00"))
  .toDF("id", "ts")
  .withColumn("ts", $"ts".cast("timestamp"))

df.groupBy($"id").agg(min($"ts")).show

// +---+--------------------+
// | id|             min(ts)|
// +---+--------------------+
// |  1|2014-01-01 15:10:...|
// +---+--------------------+

min不同,它可以在任何Orderable类型上使用.

Unlike min it will work on any Orderable type.

这篇关于通过Spark组查找时间戳的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆