2016-09-23 44 views
0

我有下面的代码,一般来说映射函数是一个高阶函数,它在其参数中使用一个函数并使用该函数计算元素。 但是在这种情况下,map没有使用Map类型的函数。无法理解map函数的工作方式?RDD映射函数的工作方式不同

Spark context available as sc (master = yarn-client, app id = application_1473775536920_2711). 
SQL context available as sqlContext. 

scala> val pws = Map("Apache Spark" -> "http://spark.apache.org/", "Scala" -> "http://www.scala-lang.org/") 
pws: scala.collection.immutable.Map[String,String] = Map(Apache Spark -> http://spark.apache.org/, Scala -> http://www.scala-lang.org/) 

scala> val websites = sc.parallelize(Seq("Apache Spark", "Scala")).map(pws).collect 
16/09/23 02:50:15 WARN util.ClosureCleaner: Expected a closure; got scala.collection.immutable.Map$Map2 
[Stage 0:>               (0 + 0)/2]16/09/23 02:50:31 WARN cluster.YarnScheduler: Initial job has not accepted any resources; check your cluster UI to ensure that workers are registered and have sufficient resources 
websites: Array[String] = Array(http://spark.apache.org/, http://www.scala-lang.org/) 

回答

6

的特质Map[A, +B]延伸性状Function1[-T1, +R]。换句话说,Map的一个函数。在你的情况下,你有一个Map[String, String]这意味着你的地图将有def apply(arg: String): String这是什么适用于您的RDD中的所有元素。

因此,即使在普通的斯卡拉,你可以这样做

val m = Map(("a" -> "b"), ("c" -> "d")) 
val s = Seq("a", "c") 

s.map(m) 
res0: Seq[String] = List(b, d) 

对于这个编译类型ms需要匹配。