Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a custom Java class to a Spark Dataset

I can't figure out a way to convert a List of Test objects to a Dataset in Spark This is my class:

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;
    }
}
like image 914
Edu Avatar asked Oct 17 '22 20:10

Edu


1 Answers

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);

and these lines to create a typed Dataset:

Encoder<Test> encoder = Encoders.bean(Test.class);
Dataset<Test> dataset = spark.createDataset(listOfTestClasses, encoder);
like image 166
Anton Okolnychyi Avatar answered Oct 21 '22 02:10

Anton Okolnychyi