【问题标题】:JavaPairRDD convert key-value into key-listJavaPairRDD 将键值转换为键列表
【发布时间】:2021-05-22 16:26:17
【问题描述】:

我有一个包含 (Key, Value) 的 JavaPairRDD,我想按 Keys 对它进行分组,并将“第二列”作为一个列表,其中包含为该键看到的所有值。我目前正在使用groupby() 函数,该函数正确执行键聚合,但将我的值转换为长的 Iterable。这是,

Key1 Iterable<Long>
Key2 Iterable<Long>
...

有什么方法可以强制这个函数使用 Long 列表而不是 Iterable 对象?

Key1 List<Long>
Key2 List<Long>
...

我读到了一些关于名为combineByKey() 的函数,但我认为这不是一个用例。可能我需要使用 reduceByKey 但我没有看到它。应该是这样的:

myRDD.reduceByKey((a,b) -> new ArrayList<Long>()) //and add b to a 

最后,我想组合值得到一个Key n,List&lt;Long&gt;RDD。 感谢您的宝贵时间。

【问题讨论】:

    标签: java apache-spark rdd


    【解决方案1】:

    你可以试试这样的:

    JavaPairRDD <String, List<long>> keyValuePairs = rdd.map(t -> {
        return new Tuple2(t._1, Arrays.asList(new long[]{t._2}));
    }).reduceByKey((a, b) -> {
        a.addAll(b);
        return a;
    });
    

    首先,您映射以将值转换为 long 列表。然后 reduceByKey 并使用 arraylist 上的addAll 方法组合列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 2021-10-09
      • 2016-03-08
      • 2021-04-18
      相关资源
      最近更新 更多