开发者社区> 问答> 正文

FlinkKafkaConsumer消费kafka中avro格式的消息,直接用AvroDeseria

我使用avro代码生成工具基于一个schema生成了UserView类,又实现了kafka的Serializer接口,代码如下: import org.apache.kafka.common.serialization.Serializer;

import java.io.IOException;

public class UserViewSerializer implements Serializer { @Override public byte[] serialize(String topic, UserView data) { byte[] array = null; try { array = data.toByteBuffer().array(); } catch (IOException e) { e.printStackTrace(); } return array; } } 构造kafka的生产者,将UserView实例写入kafka队列,代码如下: KafkaProducer<String,UserView> producer = new KafkaProducer<>(props, new StringSerializer(), new UserViewSerializer()); 在Flink程序中用FlinkKafkaConsumer消费kafka队列中的avro格式的消息,就出现了主题中描述的问题,出现异常的代码如下: FlinkKafkaConsumer myConsumer = new FlinkKafkaConsumer<>("UserView", AvroDeserializationSchema.forGeneric(SCHEMA), properties); 导致运行失败的异常信息如下:

Caused by: java.io.EOFException at org.apache.avro.io.BinaryDecoder$InputStreamByteSource.readRaw (BinaryDecoder.java:827) at org.apache.avro.io.BinaryDecoder.doReadBytes (BinaryDecoder.java:349) at org.apache.avro.io.BinaryDecoder.readString (BinaryDecoder.java:263) at org.apache.avro.io.ResolvingDecoder.readString (ResolvingDecoder.java:201) at org.apache.avro.generic.GenericDatumReader.readString (GenericDatumReader.java:422) at org.apache.avro.generic.GenericDatumReader.readString (GenericDatumReader.java:414) at org.apache.avro.generic.GenericDatumReader.readWithoutConversion (GenericDatumReader.java:181) at org.apache.avro.generic.GenericDatumReader.read (GenericDatumReader.java:153) at org.apache.avro.generic.GenericDatumReader.readField (GenericDatumReader.java:232) at org.apache.avro.generic.GenericDatumReader.readRecord (GenericDatumReader.java:222) at org.apache.avro.generic.GenericDatumReader.readWithoutConversion (GenericDatumReader.java:175) at org.apache.avro.generic.GenericDatumReader.read (GenericDatumReader.java:153) at org.apache.avro.generic.GenericDatumReader.read (GenericDatumReader.java:145) at org.apache.flink.formats.avro.AvroDeserializationSchema.deserialize (AvroDeserializationSchema.java:135) at org.apache.flink.streaming.connectors.kafka.internals.KafkaDeserializationSchemaWrapper.deserialize (KafkaDeserializationSchemaWrapper.java:45) at org.apache.flink.streaming.connectors.kafka.internal.KafkaFetcher.runFetchLoop (KafkaFetcher.java:140) at org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumerBase.run (FlinkKafkaConsumerBase.java:718) at org.apache.flink.streaming.api.operators.StreamSource.run (StreamSource.java:100) at org.apache.flink.streaming.api.operators.StreamSource.run (StreamSource.java:63) at org.apache.flink.streaming.runtime.tasks.SourceStreamTask$LegacySourceFunctionThread.run (SourceStreamTask.java:200)

希望大神不吝赐教。

来自志愿者整理的flink邮件归档来自志愿者整理的FLINK邮件归档

展开
收起
小阿怪 2021-12-04 17:30:05 1126 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第16讲】:Spring Boot 2.0 实战Apache Kafka百万级高并发消息中间件与原理解析 立即下载
MaxCompute技术公开课第四季 之 如何将Kafka数据同步至MaxCompute 立即下载
消息队列kafka介绍 立即下载