Spark RDD输出不显示

问题

在本地环境中,直接使用rdd.collect(),无法打印rdd的内容


有三种方法

result .foreach(line => println(line))

result.collect().foreach {println}

rdd.take(10).foreach { println } 

官网解释
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,因此驱动程序上的stdout不会显示这些!要打印驱动程序上的所有元素,可以使用collect()方法首先将RDD带到驱动程序节点,即:RDD .collect().foreach(println)。但是,这会导致驱动程序耗尽内存,因为collect()将整个RDD提取到一台机器上;如果只需要打印RDD的几个元素,则更安全的方法是使用take(): RDD .take(100).foreach(println)。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值