【问题标题】:Printing reduceByKey output打印 reduceByKey 输出
【发布时间】:2019-04-27 01:58:36
【问题描述】:

reduceByKey后如何打印输出

我尝试过类似的事情 totalsByAge.foreach{ i =>println("Value = " + i )}

我有几行代码 val totalsByAgeEntry = rdd.mapValues(x => (x, 1))

val totalsByAge = totalsByAgeEntry.reduceByKey( (x,y) => (x._1 + y._1, x._2 + y._2))

我想打印调用 reduceByKey 时得到的元组。在计算 (x._1 + y._1, x._2 + y._2) 之后,我不打印输出。

我知道在 reduceByKey 之后创建的数据是这样的: (x,((x1,y1),(x2,y2)) 但是我怎样才能打印出来

【问题讨论】:

  • foreachExecutors 上执行,您在 Driver 上,您不会看到输出。要么使用 collect 来检索 local Array 的元组,然后调用 foreach 。或者,使用reduceByKeyLocally,它会给你一个local Map

标签: scala apache-spark


【解决方案1】:

那是因为reduceByKey由执行器执行,println在执行器的标准输出中打印输出。执行者的标准输出通常位于master.application.ip.address:8080

如果您想打印/查看您的数据,您可以通过多种方式进行。例如:1)通过申请totalByAge.take(numberOfLines).foreach(println); 2)通过收集(.collect())RDD给驱动程序; 3) 通过将 RDD 转换为 Dataframe,然后应用 .show()

val rdd: RDD[(Int, Int)] = 
    sparkContext
        .parallelize(Vector(1, 2, 3))
        .map(i => (i, 1))
        .reduceByKey(_ + _)
rdd.take(10).foreach(println) // take the first 10 lines and print them
rdd.collect().foreach(println) // centralize the entire RDD and print it
import spark.implicits._
rdd.toDF().show(10) // conver to dataframe and show the first 10 lines

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-19
    • 2015-11-15
    • 2015-07-25
    • 2020-09-24
    • 2019-06-01
    • 2013-08-27
    • 2016-06-20
    • 2019-07-29
    相关资源
    最近更新 更多