How to convert a JavaPairRDD to Dataset?

2019-06-11 02:47发布

SparkSession.createDataset() only allows List, RDD, or Seq - but it doesn't support JavaPairRDD.

So if I have a JavaPairRDD<String, User> that I want to create a Dataset from, would a viable workround for the SparkSession.createDataset() limitation to create a wrapper UserMap class that contains two fields: String and User.

Then do spark.createDataset(userMap, Encoders.bean(UserMap.class));?

1条回答
仙女界的扛把子
2楼-- · 2019-06-11 03:14

If you can convert the JavaPairRDD to List<Tuple2<K, V>> then you can use createDataset method which takes List. See below sample code.

JavaPairRDD<String, User> pairRDD = ...;
Dataset<Row> df = spark.createDataset(pairRDD.collect(), Encoders.tuple(Encoders.STRING(),Encoders.bean(User.class))).toDF("key","value");

or you can convert to RDD

Dataset<Row> df = spark.createDataset(JavaPairRDD.toRDD(pairRDD), Encoders.tuple(Encoders.STRING(),Encoders.bean(User.class))).toDF("key","value");
查看更多
登录 后发表回答