scala下划线参数不作为命名参数,在火花映射中减少

问题描述:

在spark map函数中使用下划线参数或命名参数时,我看到一些差异。scala下划线参数不作为命名参数,在火花映射中减少

看看这个代码(在火花外壳执行):

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", "" + _)) 

而看到打字员扩展(​​),你可以看到:

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$1: Any) => "".+(x$1)) 

如果我们孤立的具体相关的部分,我们可以看到, :

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

是数组创建过程中发生的扩展。

+0

在第二个代码行中有一个额外的'“”+“。 –

+0

@Alexey谢谢!固定。 –