从jdbc连接器反序列化avro-kafka消息时出错

x6492ojm  于 2021-06-06  发布在  Kafka
关注(0)|答案(1)|浏览(269)

我试着听一个主题,我用合流的Kafka连接功能发布了这个主题。但是,我不能反序列化它。我相信,它的avro序列化,但无法找到正确的反序列化程序。
在控制台主题中,消息如下所示

null    {"c1":{"int":10},"c2":{"string":"foo"},"create_ts":1552598863000,"update_ts":1552598863000}

下面是反序列化程序

public class AvroDeserializer<T extends SpecificRecordBase> implements Deserializer<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(AvroDeserializer.class);

    protected final Class<T> targetType;

    public AvroDeserializer(Class<T> targetType) {
        this.targetType = targetType;
    }

    @Override
    public void close() {
        // No-op
    }

    @Override
    public void configure(Map<String, ?> arg0, boolean arg1) {
        // No-op
    }

    @SuppressWarnings("unchecked")
    @Override
    public T deserialize(String topic, byte[] data) {
        try {
            T result = null;

            if (data != null) {
                LOGGER.debug("data='{}'", DatatypeConverter.printHexBinary(data));

                DatumReader<GenericRecord> datumReader =
                        new SpecificDatumReader<>(targetType.newInstance().getSchema());
                Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);

                result = (T) datumReader.read(null, decoder);
                LOGGER.debug("deserialized data='{}'", result);
            }
            return result;
        } catch (Exception ex) {
            throw new SerializationException(
                    "Can't deserialize data '" + Arrays.toString(data) + "' from topic '" + topic + "'", ex);
        }
    }
}

例外

org.apache.kafka.common.errors.SerializationException: Error deserializing key/value for partition mysql-foobar-0 at offset 10. If needed, please seek past the record to continue consumption.
Caused by: org.apache.kafka.common.errors.SerializationException: Can't deserialize data '[0, 0, 0, 0, 21, 2, 20, 2, 6, 102, 111, 111, -80, -78, -44, -31, -81, 90, -80, -78, -44, -31, -81, 90]' from topic 'mysql-foobar'
Caused by: java.lang.InstantiationException: null
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) ~[na:1.8.0_131]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]
    at java.lang.Class.newInstance(Class.java:442) ~[na:1.8.0_131]
    at com.spring.kafkaexample.springbootkafkaconsumer.config.AvroDeserializer.deserialize(AvroDeserializer.java:48) ~[classes/:na]
    at com.spring.kafkaexample.springbootkafkaconsumer.config.AvroDeserializer.deserialize(AvroDeserializer.java:18) ~[classes/:na]
mspsb9vt

mspsb9vt1#

如下面控制台主题中所示
不清楚你是否用过 kafka-avro-console-consumer 或者只是 kafka-console-consumer . 了解数据是否为avro的方法是查看producer/connector配置。
不过,没有必要编写自己的反序列化程序。另外,confluent没有使用AVROSchema+消息的约定,而您的代码会这样做(这就是为什么会出现这种错误)。您需要首先从架构注册表中查找架构。
添加合流maven repo

<repositories>

  <repository>
    <id>confluent</id>
    <url>https://packages.confluent.io/maven/</url>
  </repository>

</repositories>

然后添加合流序列化程序依赖项

<dependency>
  <groupId>io.confluent</groupId>
  <artifactId>kafka-avro-serializer</artifactId>
  <version>${confluent.version}</version>
</dependency>

那么 import io.confluent.kafka.serializers.KafkaAvroDeserializer ,或在使用者配置中使用该类
https://docs.confluent.io/current/clients/install.html#java
或者,您可以将mysql连接器切换为不使用avro转换器

相关问题