为什么mapPartitions没有打印到stdout?

Nic*_*ick 2 scala apache-spark

我在scala中有这个代码

object SimpleApp {

  def myf(x: Iterator[(String, Int)]): Iterator[(String, Int)] = {
    while (x.hasNext) {
     println(x.next)
    }
    x
  }

  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("Simple Application")
    val sc = new SparkContext(conf)
    val tx1 = sc.textFile("/home/paourissi/Desktop/MyProject/data/testfile1.txt")
    val file1 = tx1.flatMap(line => line.split(" ")).map(word => (word, 1))
    val s = file1.mapPartitions(x => myf(x))
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚为什么它不会在输出上打印任何东西.我在本地计算机上运行它而不是在集群上运行它.

Gre*_*reg 5

你只有变形,没有行动.在调用操作之前,Spark不会执行.添加此行可打印出结果的前10位.

s.take(10).foreach(println)
Run Code Online (Sandbox Code Playgroud)