我尝试在GitHub(https://github.com/onurtokat/kafka-clickstream-enrich)上模拟Gwen(Chen)Shapira的kafka-clickstream-enrich kafka-stream项目。当我使用使用反序列化器的使用者类使用主题时,遇到错误。定制的Serde类具有序列化器和反序列化器。但是,我试图理解为什么将自定义Serde用于反序列化器,然后消费者API给出了错误,因为它不是org.apache.kafka.common.serialization.Deserializer的实例
可以将KTable与Serdes.Integer()序列化程序和新的ProfileSerde()反序列化程序一起使用,如下所示。
KTable<Integer, UserProfile> profiles = builder.table(Constants.USER_PROFILE_TOPIC,
Consumed.with(Serdes.Integer(), new ProfileSerde()),
Materialized.as("profile-store"));
自定义Serde定义为;
static public final class ProfileSerde extends WrapperSerde<UserProfile> {
public ProfileSerde() {
super(new JsonSerializer<UserProfile>(), new JsonDeserializer<UserProfile>(UserProfile.class));
}
}
通用Serde是自定义的,如下所示;
package com.onurtokat.serde;
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;
import java.util.Map;
public class WrapperSerde<T> implements Serde<T> {
final private Serializer<T> serializer;
final private Deserializer<T> deserializer;
public WrapperSerde(Serializer<T> serializer, Deserializer<T> deserializer) {
this.serializer = serializer;
this.deserializer = deserializer;
}
@Override
public void configure(Map<String, ?> configs, boolean isKey) {
serializer.configure(configs, isKey);
deserializer.configure(configs, isKey);
}
@Override
public void close() {
serializer.close();
deserializer.close();
}
@Override
public Serializer<T> serializer() {
return serializer;
}
@Override
public Deserializer<T> deserializer() {
return deserializer;
}
}
我的消费者是如此简单,可以在下面看到;
package com.onurtokat.consumers;
import com.onurtokat.ClickstreamEnrichment;
import com.onurtokat.Constants;
import com.onurtokat.model.UserProfile;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.IntegerDeserializer;
import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
public class ConsumeProfileData {
public static void main(String[] args) {
//prepare config
Properties config = new Properties();
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class.getName());
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ClickstreamEnrichment.ProfileSerde.class);
KafkaConsumer<Integer, UserProfile> consumerProfileTopic = new KafkaConsumer<>(config);
consumerProfileTopic.subscribe(Arrays.asList(Constants.USER_PROFILE_TOPIC));
while (true) {
ConsumerRecords<Integer, UserProfile> records = consumerProfileTopic.poll(Duration.ofMillis(100));
for (ConsumerRecord<Integer, UserProfile> record : records) {
System.out.println(record.key() + " " + record.value());
}
}
}
}
当我尝试与消费者一起使用主题时,错误是;
log4j:WARN No appenders could be found for logger (org.apache.kafka.clients.consumer.ConsumerConfig).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.apache.kafka.common.KafkaException: Failed to construct kafka consumer
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:811)
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:659)
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:639)
at com.onurtokat.consumers.ConsumeProfileData.main(ConsumeProfileData.java:25)
Caused by: org.apache.kafka.common.KafkaException: com.onurtokat.ClickstreamEnrichment$ProfileSerde is not an instance of org.apache.kafka.common.serialization.Deserializer
at org.apache.kafka.common.config.AbstractConfig.getConfiguredInstance(AbstractConfig.java:304)
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:712)
... 3 more
答案 0 :(得分:1)
您似乎误解了:
可以将KTable与Serdes.Integer()序列化程序和新的ProfileSerde()反序列化程序一起使用,如下所示。
您必须向Consumed.with()
提供KeySerde和ValueSerde。
关于异常:
非常清楚-您必须设置Deserializer的实现(不是 Serde)
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, //here);
答案 1 :(得分:1)
区别是:
Serde<T>
具有Serializer<T>
和Deserializer<T>
。您发布的第一个代码段(例如KTable
)是Kafka Streams代码段,这就是为什么它需要Serde
的原因。 Kafka Streams需要Serde
,因为它既产生数据(需要Serializer
)又读取数据(需要Deserializer
)。KafkaConsumer
)正在使用消费者客户端,因此需要Deserializer
,而不是Serde
。关于:
Caused by: org.apache.kafka.common.KafkaException: com.onurtokat.ClickstreamEnrichment$ProfileSerde is not an instance of org.apache.kafka.common.serialization.Deserializer
at org.apache.kafka.common.config.AbstractConfig.getConfiguredInstance(AbstractConfig.java:304)
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:712)
... 3 more
您的Kafka客户客户端代码在期望Serde
的地方收到了Deserializer
。