gpt4 book ai didi

java - 如何在 Vertx Kafka 客户端中使用自定义序列化器?

转载 作者:行者123 更新时间:2023-12-02 10:17:32 24 4
gpt4 key购买 nike

我有以下 kafka 生产者属性。

value.serializer=MyEventSerializer
value.deserializer=MyEventDeserializer
default.value.serde=MyEventSerde

我已经在Vertx site中经历了序列化器并使用创建了生产者

KafkaProducer<String, MyEvent> producer = KafkaProducer.create(vertx, configProperties, String.class, MyEvent.class);

但我收到以下错误:

SEVERE: Unknown class for built-in 
serializer. Supported types are: String, Short, Integer, Long, Float, Double, ByteArray, ByteBuffer, Bytes
java.lang.IllegalArgumentException: Unknown class for built-in serializer. Supported types are: String, Short, Integer, Long, Float, Double, ByteArray, ByteBuffer, Bytes

有没有办法在 Vertx kafka 客户端中拥有自定义序列化器?

最佳答案

我必须手动执行 KafkaProducer.create() 的操作。

Serializer<String> keySerializer = VertxSerdes.serdeFrom(String.class).serializer();
Serializer<MyEvent> valueSerializer = new MyEventSerializer();
KafkaWriteStream<String, MyEvent> stream = new KafkaWriteStreamImpl(vertx.getOrCreateContext(), new org.apache.kafka.clients.producer.KafkaProducer(configProperties, keySerializer, valueSerializer));
KafkaProducer<String,MyEvent> producer=(new KafkaProducerImpl(stream)).registerCloseHook();

然后使用..写入记录

KafkaProducerRecord producerRecord= KafkaProducerRecord.create(topicName,key,value);

producer.write(producerRecord, done -> {
if (done.succeeded()) {
// TODO if succeeded
} else {
// TODO if failed
}

});

关于java - 如何在 Vertx Kafka 客户端中使用自定义序列化器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54575690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com