如何将DataFrame转换为Java中Apache Spark中的数据集?

时间:2021-05-24 16:52:19

I can convert DataFrame to Dataset in Scala very easy:

我可以很容易地将DataFrame转换到Scala的数据集:

case class Person(name:String, age:Long)
val df = ctx.read.json("/tmp/persons.json")
val ds = df.as[Person]
ds.printSchema

but in Java version I don't know how to convert Dataframe to Dataset? Any Idea?

但是在Java版本中,我不知道如何将Dataframe转换为Dataset?任何想法?

my effort is:

我的工作是:

DataFrame df = ctx.read().json(logFile);
Encoder<Person> encoder = new Encoder<>();
Dataset<Person> ds = new Dataset<Person>(ctx,df.logicalPlan(),encoder);
ds.printSchema();

but the compiler say:

但编译器说:

Error:(23, 27) java: org.apache.spark.sql.Encoder is abstract; cannot be instantiated

Edited(Solution):

solution based on @Leet-Falcon answers:

基于@Leet-Falcon的解决方案:

DataFrame df = ctx.read().json(logFile);
Encoder<Person> encoder = Encoders.bean(Person.class);
Dataset<Person> ds = new Dataset<Person>(ctx, df.logicalPlan(), encoder);

2 个解决方案

#1


11  

Official Spark docs suggest in Dataset API the following:

星火官方文档在数据集API中建议如下:

Java Encoders are specified by calling static methods on Encoders.

Java编码器是通过调用编码器上的静态方法来指定的。

List<String> data = Arrays.asList("abc", "abc", "xyz");
Dataset<String> ds = context.createDataset(data, Encoders.STRING());

Encoders can be composed into tuples:

编码器可组成元组:

Encoder<Tuple2<Integer, String>> encoder2 = Encoders.tuple(Encoders.INT(), Encoders.STRING());
List<Tuple2<Integer, String>> data2 = Arrays.asList(new scala.Tuple2(1, "a");
Dataset<Tuple2<Integer, String>> ds2 = context.createDataset(data2, encoder2);

Or constructed from Java Beans by Encoders#bean:

或由Encoders#bean构建的Java bean:

Encoders.bean(MyClass.class);

#2


3  

If you want to convert a generic DF to a Dataset in Java, you can use RowEncoder class like below

如果您想在Java中将通用DF转换为数据集,可以使用RowEncoder类,如下所示

DataFrame df = sql.read().json(sc.parallelize(ImmutableList.of(
            "{\"id\": 0, \"phoneNumber\": 109, \"zip\": \"94102\"}"
    )));

    Dataset<Row> dataset = df.as(RowEncoder$.MODULE$.apply(df.schema()));

#1


11  

Official Spark docs suggest in Dataset API the following:

星火官方文档在数据集API中建议如下:

Java Encoders are specified by calling static methods on Encoders.

Java编码器是通过调用编码器上的静态方法来指定的。

List<String> data = Arrays.asList("abc", "abc", "xyz");
Dataset<String> ds = context.createDataset(data, Encoders.STRING());

Encoders can be composed into tuples:

编码器可组成元组:

Encoder<Tuple2<Integer, String>> encoder2 = Encoders.tuple(Encoders.INT(), Encoders.STRING());
List<Tuple2<Integer, String>> data2 = Arrays.asList(new scala.Tuple2(1, "a");
Dataset<Tuple2<Integer, String>> ds2 = context.createDataset(data2, encoder2);

Or constructed from Java Beans by Encoders#bean:

或由Encoders#bean构建的Java bean:

Encoders.bean(MyClass.class);

#2


3  

If you want to convert a generic DF to a Dataset in Java, you can use RowEncoder class like below

如果您想在Java中将通用DF转换为数据集,可以使用RowEncoder类,如下所示

DataFrame df = sql.read().json(sc.parallelize(ImmutableList.of(
            "{\"id\": 0, \"phoneNumber\": 109, \"zip\": \"94102\"}"
    )));

    Dataset<Row> dataset = df.as(RowEncoder$.MODULE$.apply(df.schema()));