Encoder for Row Type Spark Datasets

后端 未结 2 1221
臣服心动
臣服心动 2021-01-30 13:45

I would like to write an encoder for a Row type in DataSet, for a map operation that I am doing. Essentially, I do not understand how to write encoders.

Below is an exam

2条回答
  •  清酒与你
    2021-01-30 14:42

    The answer is to use a RowEncoder and the schema of the dataset using StructType.

    Below is a working example of a flatmap operation with Datasets:

        StructType structType = new StructType();
        structType = structType.add("id1", DataTypes.LongType, false);
        structType = structType.add("id2", DataTypes.LongType, false);
    
        ExpressionEncoder encoder = RowEncoder.apply(structType);
    
        Dataset output = join.flatMap(new FlatMapFunction() {
            @Override
            public Iterator call(Row row) throws Exception {
                // a static map operation to demonstrate
                List data = new ArrayList<>();
                data.add(1l);
                data.add(2l);
                ArrayList list = new ArrayList<>();
                list.add(RowFactory.create(data.toArray()));
                return list.iterator();
            }
        }, encoder);
    
        

    提交回复
    热议问题