最近使用Scala操作Spark,想要输出RDD中相关内容去查找错误。
这样就遇到了一个问题:在单机模式下输出了相关的内容,而在集群模式中的操作却没有输出。
试过了一些方法,包含idea中调试,Print输出都不好使。
实际上在官方文档中已经有了这样的一句话。
Printing elements of an RDD
Another common idiom is attempting to print out the elements of an RDD using rdd.foreach(println) or rdd.map(println). On a single machine, this will generate the expected output and print all the RDD’s elements. However, in cluster mode, the output to stdout being called by the executors is now writing to the executor’s stdout instead, not the one on the driver, so stdout on the driver won’t show these! To print all elements on the driver, one can use the collect() method to first bring the RDD to the driver node thus: rdd.collect().foreach(println). This can cause the driver to run out of memory, though, because collect() fetches the entire RDD to a single machine; if you only need to print a few elements of the RDD, a safer approach is to use the take(): rdd.take(100).foreach(println).
谷歌翻译过来就是
RDD的打印元素
另一个常见用法是尝试使用rdd.foreach(println)或rdd.map(println)打印出RDD的元素。 在单台机器上,这将产生预期的输出并打印所有RDD的元素。 但是,在集群模式下,执行者正在调用stdout的输出现在写入执行者的stdout,而不是驱动程序上的那个,因此驱动程序上的stdout不会显示这些信息! 要在驱动程序上打印所有元素,可以使用collect()方法首先将RDD带到驱动程序节点:rdd.collect()。foreach(println)。 但是,这可能导致驱动程序用尽内存,因为collect()将整个RDD提取到一台计算机上。 如果只需要打印RDD的一些元素,则更安全的方法是使用take():rdd.take(100).foreach(println)。
大意就是常见的输出方式,比如RDD.foreach(println),或者是使用RDD.map(printlen)操作,在单机模式下进行输出是完全可行的,但是在cluster模式(集群)下则是无法使用。
解决方式则是使用collect()方法,将整个RDD汇总,但是如果数据量过大,则会导致内存不足,因此更加安全的方式是使用tack()。
即: RDD.take(30).foreach(println)
在Spark集群模式下,使用RDD.foreach(println)或rdd.map(println)无法正常输出元素。官方建议通过collect()方法将RDD拉取到driver端再使用foreach(println)打印,但可能造成内存溢出。安全做法是使用take(n)限制拉取数量,如rdd.take(30).foreach(println)。

604

被折叠的 条评论
为什么被折叠?



