Encoder for Row Type Spark Datasets

后端 未结 2 1222
臣服心动
臣服心动 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:39

    I had the same problem... Encoders.kryo(Row.class)) worked for me.

    As a bonus, the Apache Spark tuning docs refer to Kryo it since it’s faster at serialization "often as much as 10x":

    https://spark.apache.org/docs/latest/tuning.html

    0 讨论(0)
  • 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<Row> encoder = RowEncoder.apply(structType);
    
        Dataset<Row> output = join.flatMap(new FlatMapFunction<Row, Row>() {
            @Override
            public Iterator<Row> call(Row row) throws Exception {
                // a static map operation to demonstrate
                List<Object> data = new ArrayList<>();
                data.add(1l);
                data.add(2l);
                ArrayList<Row> list = new ArrayList<>();
                list.add(RowFactory.create(data.toArray()));
                return list.iterator();
            }
        }, encoder);
    
    0 讨论(0)
提交回复
热议问题