Apache spark 如何访问reduceByKey和combineByKey中使用的两个关键对象?

Apache spark 如何访问reduceByKey和combineByKey中使用的两个关键对象?,apache-spark,Apache Spark,reduceByKey和combineByKey使用Function2聚合同一密钥的计数。由于Function2只传递键的当前计数,因此如何访问与reduceByKey和combineByKey一起使用的实际两个键对象?将两个键组合成一个元组2,然后您可以轻松地执行reduceByKey和combineByKey 这是一个例子 JavaRDD<Tuple3<String,String,Integer>> data = { .... }; JavaPairRDD<Tu

reduceByKey和combineByKey使用Function2聚合同一密钥的计数。由于Function2只传递键的当前计数,因此如何访问与reduceByKey和combineByKey一起使用的实际两个键对象?

将两个键组合成一个元组2,然后您可以轻松地执行reduceByKey和combineByKey

这是一个例子

JavaRDD<Tuple3<String,String,Integer>> data = { .... };
JavaPairRDD<Tuple2<String, String>, Integer> map = data.mapToPair(new PairFunction<Tuple3<String,String,Integer>, Tuple2<String,String>, Integer>() {
    @Override
    public Tuple2<Tuple2<String, String>, Integer> call(Tuple3<String, String, Integer> t) throws Exception {
        return new Tuple2<Tuple2<String, String>, Integer>(new Tuple2<String, String>(t._1(),t._2()),t._3());
    }
});

map.reduceByKey(new Function2<Integer, Integer, Integer>() {
    @Override
    public Integer call(Integer v1, Integer v2) throws Exception {
        return v1+v2;
    }
});
javarddata={….};
javapairrdmap=data.mapToPair(新的PairFunction(){
@凌驾
公共Tuple2调用(tuple3t)引发异常{
返回新的Tuple2(新的Tuple2(t._1(),t._2()),t._3());
}
});
map.reduceByKey(新函数2(){
@凌驾
公共整数调用(整数v1、整数v2)引发异常{
返回v1+v2;
}
});

我找到了解决方案。感谢Kaushal和Justin之前的回答。

为了澄清,这个问题与访问RDD中的任意密钥无关。它是关于在reduceByKey和combineByKey处理RDD时访问匹配键。例如,RDD有4个键/变量对。两对具有相同的“A1”键,另外两对具有相同的“B1”键。在通过Function2()的reduceByKey方法中,它只传递匹配键的值。在本例中,如何访问匹配键“A1”的实际对象?根据上面的说明,这个问题是关于在reduceByKey和combineByKey处理RDD时访问匹配键。。JavaPairRDD已经存在一组键/值对,其中一些对具有匹配的键。在通过Function2()的reduceByKey和combineByKey方法中,它们只传递匹配键的值。在本例中,如何使用匹配的键“A1”访问实际的键对象?使用JavaPairdd的查找函数,如
Pairdd.lookup(key)
根据键获取值。