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

查看:56
本文介绍了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天全站免登陆