Dataset.reduce 不支持速记功能 [英] Dataset.reduce doesn't support shorthand function

查看:33
本文介绍了Dataset.reduce 不支持速记功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的代码:

test("0153") {
  val c = Seq(1,8,4,2,7)
  val max = (x:Int, y:Int)=> if (x > y) x else y
  c.reduce(max)
}

它工作正常.但是,当我按照相同的方式使用 Dataset.reduce 时,

It works fine. But, when I follow the same way to use Dataset.reduce,

test("SparkSQLTest") {
  def max(x: Int, y: Int) = if (x > y) x else y
  val spark = SparkSession.builder().master("local").appName("SparkSQLTest").enableHiveSupport().getOrCreate()
  val ds = spark.range(1, 100).map(_.toInt)
  ds.reduce(max) //compiling error:Error:(20, 15) missing argument list for method max
}

编译器抱怨缺少方法max的参数列表,我不知道这里发生了什么.

Compiler complains that missing argument list for method max, I don't what's going on here.

推荐答案

改为函数而不是方法,它应该可以工作,即代替

Change to a function instead of a method and it should work, i.e. instead of

def max(x: Int, y: Int) = if (x > y) x else y

使用

val max = (x: Int, y: Int) => if (x > y) x else y

使用函数,使用 ds.reduce(max) 应该可以直接工作.有关差异的更多信息,请参见此处.

Using the function, using ds.reduce(max) should work directly. More about the differences can be found here.

否则,正如 hadooper 指出的,您可以通过提供参数来使用该方法,

Otherwise, as hadooper pointed out you can use the method by supplying the arguments,

def max(x: Int, y: Int) = if (x > y) x else y
ds.reduce((x, y) => max(x,y))

这篇关于Dataset.reduce 不支持速记功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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