Json Flink Kafka-自定义类数据始终为空
自定义类 人Json Flink Kafka-自定义类数据始终为空,json,serialization,apache-kafka,deserialization,apache-flink,Json,Serialization,Apache Kafka,Deserialization,Apache Flink,自定义类 人 class Person { private Integer id; private String name; //getters and setters } 卡夫卡·弗林克连接器 TypeInformation<Person> info = TypeInformation.of(Person.class); TypeInformationSerializationSchema schema = new TypeInformationSerializati
class Person
{
private Integer id;
private String name;
//getters and setters
}
卡夫卡·弗林克连接器
TypeInformation<Person> info = TypeInformation.of(Person.class);
TypeInformationSerializationSchema schema = new TypeInformationSerializationSchema(info, new ExecutionConfig());
DataStream<Person> input = env.addSource( new FlinkKafkaConsumer08<>("persons", schema , getKafkaProperties()));
class PersonSchema implements DeserializationSchema<Person>{
private ObjectMapper mapper = new ObjectMapper(); //com.fasterxml.jackson.databind.ObjectMapper;
@Override
public Person deserialize(byte[] bytes) throws IOException {
return mapper.readValue( bytes, Person.class );
}
@Override
public boolean isEndOfStream(Person person) {
return false;
}
@Override
public TypeInformation<Person> getProducedType() {
return TypeInformation.of(new TypeHint<Person>(){});
}
}
通过Kafka控制台生成器,flink代码抛出空指针异常
但是如果我使用SimpleStringSchema
而不是前面定义的CustomSchema,那么流将被打印出来
上述设置中的错误是,
TypeInformationSerializationSchema
是一个反序列化模式,它使用Flink的序列化堆栈,因此也是其序列化程序。因此,当使用此SerializationSchema
Flink时,Flink希望已使用Flink的序列化程序对Person
类型的数据进行序列化
鉴于Person
类的摘录,Flink很可能会使用其PojoTypeSerializer
。此序列化程序无法理解馈送JSON输入数据
如果您想使用JSON作为输入格式,那么您必须定义自己的
反序列化模式
,该模式可以将JSON解析为人
回答有相同问题的人
自定义序列化程序
TypeInformation<Person> info = TypeInformation.of(Person.class);
TypeInformationSerializationSchema schema = new TypeInformationSerializationSchema(info, new ExecutionConfig());
DataStream<Person> input = env.addSource( new FlinkKafkaConsumer08<>("persons", schema , getKafkaProperties()));
class PersonSchema implements DeserializationSchema<Person>{
private ObjectMapper mapper = new ObjectMapper(); //com.fasterxml.jackson.databind.ObjectMapper;
@Override
public Person deserialize(byte[] bytes) throws IOException {
return mapper.readValue( bytes, Person.class );
}
@Override
public boolean isEndOfStream(Person person) {
return false;
}
@Override
public TypeInformation<Person> getProducedType() {
return TypeInformation.of(new TypeHint<Person>(){});
}
}
类PersonSchema实现反序列化模式{
私有对象映射器映射器=新对象映射器();//com.fasterxml.jackson.databind.ObjectMapper;
@凌驾
公共人物反序列化(字节[]字节)引发IOException{
返回mapper.readValue(字节,Person.class);
}
@凌驾
公共布尔值isEndOfStream(个人){
返回false;
}
@凌驾
公共类型信息getProducedType(){
返回TypeInformation.of(new TypeHint(){});
}
}
使用模式
DataStream<Person> input = env.addSource( new FlinkKafkaConsumer08<>("persons", new PersonSchema() , getKafkaProperties()));
DataStream input=env.addSource(新FlinkKafkaConsumer08(“persons”,新PersonSchema(),getKafkaProperties());
成功了!一份更好的文件可能会有所帮助