gpt4 book ai didi

java - Spark-Java : Display join RDD

转载 作者:行者123 更新时间:2023-11-30 03:28:17 25 4
gpt4 key购买 nike

我正在尝试加入两个pairRDD,如下所示,而

lat1:K,V -> k-整数,V- double lat2:K,V -> k-整数,V- double

   JavaPairRDD<Integer,Tuple2<Double,Double>> latlong = lat.join(long);

假设新的 RDD 为 K,[V1,V2],我想显示新的 RDD

如果我想根据值进行操作,执行方式是什么

请在 Spark-Java Api 中建议

P.s:我看到很多答案都是在scala中,但我的要求是在JAVa中实现

最佳答案

来自 Spark 文档:

When join called on datasets of type (K, V) and (K, W), returns a dataset of (K, (V, W)) pairs with all pairs of elements for each key.

所以你的这个假设是正确的:

JavaPairRDD<Integer,Tuple2<Double,Double>> latlong = lat.join(long);

当您需要使用JavaPairRDD中的值时,可以使用#mapValues()方法:

Pass each value in the key-value pair RDD through a map function without changing the keys; this also retains the original RDD's partitioning.

要显示JavaPairRDD,您可以使用与平常相同的输出方法,例如: G。 #saveAsTextFile()

<小时/>

当您需要将 (K, (V, W)) 中的值映射到 (K,V-W) 等其他内容时,您可以使用提到的 mapValues() 转换:

JavaPairRDD<Integer, String> pairs = latlong.mapValues(
new Function<Tuple2<Double, Double>, String>() {
@Override
public String call(Tuple2<Double, Double> value) throws Exception {
return value._1() + "-" + value._2();
}
});

关于java - Spark-Java : Display join RDD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29665292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com