gpt4 book ai didi

scala - 为什么 mapPartitions 不向标准输出打印任何内容?

转载 作者:行者123 更新时间:2023-12-01 11:05:08 26 4
gpt4 key购买 nike

我在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))
}
}

我试图弄清楚为什么它不会在输出上打印任何内容。我在本地机器上而不是在集群上运行它。

最佳答案

你只有转换,没有actions .在调用 Action 之前,Spark 不会执行。添加此行以打印出前 10 个结果。

s.take(10).foreach(println)

关于scala - 为什么 mapPartitions 不向标准输出打印任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33830676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com