如何将自定义 Java 类转换为 Spark 数据集

Edu*_*Edu 5 java dataset apache-spark

我想不出一种方法将测试对象列表转换为 Spark 中的数据集这是我的课程:

public class Test {
    public String a;
    public String b;
    public Test(String a, String b){
        this.a = a;
        this.b = b;
    }

    public List getList(){
        List l = new ArrayList();
        l.add(this.a);
        l.add(this.b);

        return l;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ant*_*hyi 4

Your code in the comments to create a DataFrame is correct. However, there is a problem with the way you define Test. You can create DataFrames using your code only from Java Beans. Your Test class is not a Java Bean. Once you fix that, you can use the following code to create a DataFrame:

Dataset<Row> dataFrame = spark.createDataFrame(listOfTestClasses, Test.class);
Run Code Online (Sandbox Code Playgroud)

and these lines to create a typed Dataset:

Encoder<Test> encoder = Encoders.bean(Test.class);
Dataset<Test> dataset = spark.createDataset(listOfTestClasses, encoder);
Run Code Online (Sandbox Code Playgroud)