我有下面的代码,一般来说,map函数是一个高阶函数,它在其参数中接受一个函数并使用该函数计算元素。 但在这种情况下,map不是一个函数而是一个Map类型。无法理解地图功能如何运作?
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/)
答案 0 :(得分:6)
特征Map[A, +B]
扩展了特征Function1[-T1, +R]
。换句话说,Map
是一个函数。在您的情况下,您有一个Map[String, String]
,这意味着您的地图将def apply(arg: String): String
应用于RDD
中的所有元素。
因此,即使在简单的Scala中,您也可以执行类似
的操作val m = Map(("a" -> "b"), ("c" -> "d"))
val s = Seq("a", "c")
s.map(m)
res0: Seq[String] = List(b, d)
为此编译m
和s
中需要匹配的类型。