如何在spark中映射JavaPairRDD的键? [英] How to map keys of JavaPairRDD in spark?
问题描述
我是Spark的新手,我使用 sc.wholeTextFiles(path);
来读取所有文件,该函数返回 JavaPairRDD< String,字符串>
和RDD的密钥是每个文件的完整路径,但我想要的是将密钥更改为文件名。
他们的东西是 mapValues(func)
但是对于密钥。
I am new to Spark and I have used sc.wholeTextFiles(path);
to read all files, the function returns JavaPairRDD<String, String>
and Key of RDD is the full path to each file however what I want is to change the key to the name of the file.
Is their something like mapValues(func)
but for keys.
推荐答案
这里的关键假设是 JavaPairRDD< String,String>
与相同; JavaRDD< Tuple2< String,String>>
。
换句话说, JavaPairRDD
只是 RDD
元组。所以你可以使用常见的 map
来修改元组键并保持值不变。
In other words JavaPairRDD
is just RDD
of tuples. So you can use the the common map
to modify just the tuple key and leave the value untouched.
JavaPairRDD<String, String> input = sc.wholeTextFiles(path);
input.map(new Function<Tuple2<String,String>, Tuple2<String,String>>() {
@Override
public Tuple2<String, String> call(Tuple2<String, String> tuple) {
return new Tuple2<>(convertToFilename(tuple._1()), tuple._2());
}
});
这篇关于如何在spark中映射JavaPairRDD的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!