简体   繁体   中英

Spark: JavaRDD<Tuple2> to JavaPairRDD<>

I have a JavaRDD<Tuple2<String, String>> and need to transform it to JavaPairRDD<String, String> . Currently I am doing it by simply writing map function that just returns the input tuple as is. But I wonder if there is a better way?

JavaPairRDD.fromJavaRDD(rdd)是解决方案之一

对于反向转换,这似乎有效:

JavaRDD.fromRDD(JavaPairRDD.toRDD(rdd), rdd.classTag());

Try this example:

JavaRDD<Tuple2<Integer, String>> mutate = mutateFunction(rdd_world); //goes to a method that generates the RDD with a Tuple2 from a rdd_world RDD
JavaPairRDD<Integer,  String> pairs = JavaPairRDD.fromJavaRDD(mutate);

Try this to transform JavaRDD into JavaPairRDD. For me It is working perfectly.

JavaRDD<Sensor> sensorRdd = lines.map(new SensorData()).cache();
// transform data into javaPairRdd
JavaPairRDD<Integer, Sensor> deviceRdd = sensorRdd.mapToPair(new PairFunction<Sensor, Integer, Sensor>() {   
    public Tuple2<Integer, Sensor> call(Sensor sensor) throws Exception {
        Tuple2<Integer, Sensor>  tuple = new Tuple2<Integer, Sensor>(Integer.parseInt(sensor.getsId().trim()), sensor);
        return tuple;
    }
});

或者,您可以在org.apache.spark.api.java.JavaRDD的实例上调用mapToPair(..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM