2016-10-30 10 views
-1

Ich bin neu zu scala und nur zu verstehen, wie kann ich über Map Call transformieren. Die Foo-Funktion wird nicht aufgerufen. Was fehlt mir?Scala-Funktion nicht von Lambda-Funktion aufgerufen

import org.apache.spark.rdd.RDD 
import org.apache.spark.SparkConf 
import org.apache.spark.SparkContext 

object Expt { 

def main(args: Array[String]): Unit = { 
     var conf = new SparkConf().setAppName("Test").setMaster("local[*]") 
       val sc = new SparkContext(conf) 
       val a1 = sc.parallelize(List((1,"one"),(2,"two"),(3,"three")) 
       val a3 = a1.map(x => Expt.foo(x)) 
} 

def foo(x: (Int,String)) : (Int,String) = { 
     println(x) 
     x 
} 
} 

Antwort

2

Sie führen keine Aktion aus, daher wird die Karte nie ausgewertet. Auch println in map hat in der Regel keinen sichtbaren Effekt.

0

Trigger Berechnung unter Verwendung collect oder foreach (eine Aktion tun)

scala> val a1 = sc.parallelize(List((1,"one"),(2,"two"),(3,"three"))) 
a1: org.apache.spark.rdd.RDD[(Int, String)] = ParallelCollectionRDD[6] at parallelize at <console>:24 

scala> val a3 = a1.map(x => foo(x)) 
a3: org.apache.spark.rdd.RDD[(Int, String)] = MapPartitionsRDD[7] at map at <console>:28 

scala> a3.collect 
(1,one) 
(2,two) 
(3,three) 
res3: Array[(Int, String)] = Array((1,one), (2,two), (3,three)) 

scala> a3.foreach(_ =>()) 
(1,one) 
(3,three) 
(2,two) 
Verwandte Themen