我有以下 JavaPairRDD,代表每个客户的订单数量:
JavaPairRDD<String, Integer> customersToOrderCountRDD1 = ...
JavaPairRDD<String, Integer> customersToOrderCountRDD2 = ...
第一个是从 Cassandra 中的表中检索的,第二个是从外部 Web API 中检索的。
计算这两个 RDD 的组合值的最有效方法是什么,换句话说,获取每个客户的总订单数:例如,如果我在 RDD 中有以下数据集:
customersToOrderCountRDD1: (email1@email.com, 3) (email2@email.com, 4)
customersToOrderCountRDD2: (email1@email.com, 1) (email2@email.com, 2)
得到:
customersToTotalOrderCount: (email1@email.com, 4) (email2@email.com, 6)
最佳答案
请引用“使用键值对”部分和 union/reduceByKey API:
http://spark.apache.org/docs/latest/programming-guide.html#working-with-key-value-pairs
customersToOrderCountRDD1.union(customersToOrderCountRDD2).reduceByKey((a, b) -> a + b)
关于java - 如何对两个 Apache Spark JavaPairRDD 求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34266022/