在 spark map reduce 中,scala 下划线参数不作为命名参数

scala underscore parameter not acting as named parameter, in spark map reduce

我发现在 spark map 函数中使用下划线参数或命名参数时有些不同。

看这段代码(在spark-shell中执行):

var ds = Seq(1,2,3).toDS()
ds.map(t => Array("something", "" + t)).collect // works cool
ds.map(Array("funk", "" + _)).collect // doesn't work

我得到的不工作行的异常是:

error: Unable to find encoder for type stored in a Dataset. Primitive types (Int, String, etc) and Product types (case classes) are supported by importing spark.implicits._ Support for serializing other types will be added in future releases.

那是因为展开了:

ds.map(Array("funk", "" + _)).collect 

并不像你想象的那样有效。它扩展为:

ds.map(Array("funk", ((x: Any) => "" + x))).collect 

数组创建中的 _ 扩展为一个函数。根据 DataSets 的文档,不支持函数。

如果我们进行最小复制:

val l = List(1,2,3)
val res = l.map(Array("42", "" + _))

并看到打字机扩展(scalac -Xprint:typer),你可以看到:

def main(args: Array[String]): Unit = {
  val l: List[Int] = scala.collection.immutable.List.apply[Int](1, 2, 3);
  val res: List[Object] = 
    l.map[Object, List[Object]]
    (scala.Predef.wrapRefArray[Object]
      (scala.Array.apply[Object]("42", ((x: Any) => "".+(x))

如果我们把具体的相关部分分离出来,我们可以看到:

(x: Any) => "".+(x)

是数组创建内部发生的扩展。