如何在强类型 Spark 数据集上使用 Window 聚合? [英] How use Window aggrgates on strongly typed Spark Datasets?

查看:48
本文介绍了如何在强类型 Spark 数据集上使用 Window 聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在缓慢地尝试适应 Spark 2.x 中的新(强类型)Dataset[U],但在使用时努力维护类型信息窗口函数.

I'm slowely trying to adapt to the new (strongly typed) Dataset[U] from Spark 2.x, but struggling to maintain the type info when using Window functions.

case class Measurement(nb:Long,x:Double)

ds 是一个 Dataset[Measurement],我想做类似的事情

ds being a Dataset[Measurement], I would like to do something like

ds.map{m => (m,sum($"x").over(Window.orderBy($"nb"))}

但这行不通(因为它给了我一个 Dataset[(Measurement,Column)])而不是 Dataset[(Measurement,Double)]

But this will not work (as it gives my a Dataset[(Measurement,Column)]) instead of Dataset[(Measurement,Double)]

使用 withColumn 给了我一个 Dataset[Row],所以我丢失了类型信息:

Using withColumn gives me a Dataset[Row], so I'm loosing the type info:

ds.withColumn("cumsum",sum($"x").over(Window.orderBy($"nb")))

那么,有没有更好的方法在强类型的Datasets 上使用Window 函数?

So, is there a better way to use Window functions on strongly typed Datasets?

推荐答案

当您向数据集添加新列时,我想除了使用 dataframe.as[New Type] 之外别无选择方法

As you adding a new column to your dataset, I guess there is no choices but using the dataframe.as[New Type] method

可以在此处找到更多信息 如何在不从 DataFrame 转换并访问它的情况下向 Dataset 添加列?

More information can be found here How to add a column to Dataset without converting from a DataFrame and accessing it?

关于窗口函数的更多信息可以在这篇博客文章中找到 Databricks 的 Spark SQL 中的窗口函数

More information on Window functions can be found on this blog article Window Functions in Spark SQL by Databricks

这篇关于如何在强类型 Spark 数据集上使用 Window 聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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