我在pojo类中使用Flink和自定义kryo类。但是得到

Caused by: java.lang.NullPointerException
    at MyTreeSerializer.read(MyTreeSerializer.java:36)
    at MyTreeSerializer.read(MyTreeSerializer.java:11)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:657)
    at org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer.copy(KryoSerializer.java:172)
    at org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.collect(OperatorChain.java:414)
    ... 16 more


这是详细信息-


Kryo 2.24.0
我的Pojo课堂


```

public class MyTree extends TreeMap<String, Object> {
    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}



Pojo序列化器


```

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.MapSerializer;


public class MyTreeSerializer extends Serializer<MyTree> {


    public MyTreeSerializer() {
    }


    @Override
    public void write(Kryo kryo, Output output, MyTree object) {
        output.writeString(object.getId());
        kryo.writeObject(output, object, new MapSerializer());

    }

    @Override
    public MyTree read(Kryo kryo, Input input, Class<MyTree> type) {
        String id = input.readString();
        System.out.println("Serialized Id " + id);
        MyTree myTree = kryo.readObject(input, type, new MapSerializer());
        System.out.println("Serialized Object " + myTree);
        myTree.setId(id);
        return myTree;
    }
}


```


Flink流式主程序


```

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;


public class MultiSinkTest {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // Setting Serializer
        env.getConfig().addDefaultKryoSerializer(MyTree.class, MyTreeSerializer.class);

        DataStreamSource<String> data = env.fromElements("1", "2");

        DataStream<MyTree> returns = data.map(new MapFunction<String, MyTree>() {
            @Override
            public MyTree map(String s) throws Exception {
                MyTree myTree = new MyTree();
                myTree.setId(s);
                myTree.put("name", "sohi");
                return myTree;
            }
        }).returns(MyTree.class);


        returns.addSink(new SinkFunction<MyTree>() {
            @Override
            public void invoke(MyTree myTree) throws Exception {
                System.out.println("==> " + myTree.toString());
            }
        });

        env.execute();
    }
}


通过使用所有提到的代码,只有id被序列化,而不是MyTree的map部分。

但是如果我更换

env.getConfig().addDefaultKryoSerializer(MyTree.class, MyTreeSerializer.class);




env.getConfig().addDefaultKryoSerializer(MyTree.class, MapSerializer.class);


然后id未序列化,但是map正在序列化。

只需要帮助,为什么在使用MyTreeSerializer.class时它不起作用。

提前致谢 。

最佳答案

MyTreeSerializer中的以下行导致null:

MyTree myTree = kryo.readObject(input, type, new MapSerializer());


这也是为什么myTree.setId(id)导致


  空指针异常。


当您使用MapSerializer时,它可以正常工作(当然,对id的反序列化除外),因为MyTree从实现了TreeMapMap扩展而来。

MyTreeSerializer的实现中,您尝试从MyTree对象反序列化类MyTree的成员。就像MyTreeSerializer期望一个对象,如下面的示例代码中所示:



    公共类MyTree扩展TreeMap {
        私有字符串ID;
        私人MyTree myTree;

        公共字符串getId(){
            返回ID;
        }
        公共无效setId(String id){
            this.id = id;
        }

        公共MyTree getMyTree(){
            返回myTree;
        }

        公共无效setMyTree(MyTree myTree){
            this.myTree = myTree;
        }
    }



它认为您需要查看MapSerializer并对其进行扩展,或者将其用作您自己的实现的基础,以便对MyTree对象进行序列化和反序列化。

10-02 11:07