Apache spark 将管道()与reduceByKey()一起使用时作业挂起
我遇到一种情况: 当我使用Apache spark 将管道()与reduceByKey()一起使用时作业挂起,apache-spark,Apache Spark,我遇到一种情况: 当我使用 val a = rdd.pipe("./my_cpp_program").persist() a.count() // just use it to persist a val b = a.map(s => (s, 1)).reduceByKey().count() val b = rdd.pipe("./my_cpp_program").map(s => (s, 1)).reduceByKey().count() 太快了 但是当我使用 val
val a = rdd.pipe("./my_cpp_program").persist()
a.count() // just use it to persist a
val b = a.map(s => (s, 1)).reduceByKey().count()
val b = rdd.pipe("./my_cpp_program").map(s => (s, 1)).reduceByKey().count()
太快了
但是当我使用
val a = rdd.pipe("./my_cpp_program").persist()
a.count() // just use it to persist a
val b = a.map(s => (s, 1)).reduceByKey().count()
val b = rdd.pipe("./my_cpp_program").map(s => (s, 1)).reduceByKey().count()
太慢了。。。。
我的遗嘱执行人中有很多这样的记录:
15/10/31 19:53:58 INFO collection.ExternalSorter: Thread 78 spilling in-memory map of 633.1 MB to disk (8 times so far)
15/10/31 19:54:14 INFO collection.ExternalSorter: Thread 74 spilling in-memory map of 633.1 MB to disk (8 times so far)
15/10/31 19:54:17 INFO collection.ExternalSorter: Thread 79 spilling in-memory map of 633.1 MB to disk (8 times so far)
15/10/31 19:54:29 INFO collection.ExternalSorter: Thread 77 spilling in-memory map of 633.1 MB to disk (8 times so far)
15/10/31 19:54:50 INFO collection.ExternalSorter: Thread 76 spilling in-memory map of 633.1 MB to disk (9 times so far)
您尚未将函数传递给
reduceByKey()
。从reduceByKey的文档中:
在(K,V)对数据集上调用时,返回(K,V)对数据集,其中每个键的值使用给定的reduce函数func聚合,该函数必须为(V,V)=>V类型。与groupByKey中一样,reduce任务的数量可以通过可选的第二个参数进行配置
在这种情况下,您希望传递匿名函数(a,b)=>a+b
,以在键之间聚合值(也可以使用Scala的缩短下划线符号编写为。
)
由于您正在调用count()
(这实际上会在reduceByKey()
之后计算唯一键的数量),因此您只需使用distinct()
就可以了。distinct的实现实际上与您当前尝试的非常相似(而是映射到(s,null)
,然后调用reduceByKey),但是从代码可读性的角度来看,distinct更好地指示您的最终目标是什么。类似这样的方法会奏效:
val b = rdd.pipe("./my_cpp_program").distinct().count()
由于您实际上可能还对每个唯一键的计数感兴趣,因此该类中还有其他函数可以帮助您实现这一点。我将签出countByKey()
,countByKeyApprox()
,以及countApproxDistinctByKey()
。每个都有不同的用例,但为各自的问题提供了有趣的解决方案