gpt4 book ai didi

apache-kafka - 卡夫卡流 : How to fix Serde casting error

转载 作者:行者123 更新时间:2023-12-04 04:44:54 25 4
gpt4 key购买 nike

当我使用聚合函数模拟字数统计时,我遇到了 Serde 转换问题。

Exception in thread "aggregation-transformation-application-43485635-2d3c-4edc-b13c-c6505a793d18-StreamThread-1" org.apache.kafka.streams.errors.StreamsException: Deserialization exception handler is set to fail upon a deserialization error. If you would rather have the streaming pipeline continue after a deserialization error, please set the default.deserialization.exception.handler appropriately.
at org.apache.kafka.streams.processor.internals.RecordDeserializer.deserialize(RecordDeserializer.java:80)
at org.apache.kafka.streams.processor.internals.RecordQueue.maybeUpdateTimestamp(RecordQueue.java:160)
at org.apache.kafka.streams.processor.internals.RecordQueue.poll(RecordQueue.java:115)
at org.apache.kafka.streams.processor.internals.PartitionGroup.nextRecord(PartitionGroup.java:100)
at org.apache.kafka.streams.processor.internals.StreamTask.process(StreamTask.java:349)
at org.apache.kafka.streams.processor.internals.AssignedStreamsTasks.process(AssignedStreamsTasks.java:199)
at org.apache.kafka.streams.processor.internals.TaskManager.process(TaskManager.java:420)
at org.apache.kafka.streams.processor.internals.StreamThread.runOnce(StreamThread.java:890)
at org.apache.kafka.streams.processor.internals.StreamThread.runLoop(StreamThread.java:805)
at org.apache.kafka.streams.processor.internals.StreamThread.run(StreamThread.java:774)
Caused by: org.apache.kafka.common.errors.SerializationException: Size of data received by IntegerDeserializer is not 4

尽管我为每个任务定义了 Serdes,但它会抛出 SerializationException。

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.utils.Bytes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.*;
import org.apache.kafka.streams.state.KeyValueStore;

import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;

public class AggregationTransformation {
public static void main(String[] args) {
//prepare config
Properties config = new Properties();
config.put(StreamsConfig.APPLICATION_ID_CONFIG, "aggregation-transformation-application");
config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

StreamsBuilder builder = new StreamsBuilder();

KStream<String, String> kStream = builder.stream("agg-table-source-topic");
KStream<String, Integer> kStreamFormatted = kStream.flatMapValues((key, value) ->
Arrays.asList(value.split("\\W+"))).selectKey((key, value) -> value)
.mapValues(value -> 1);

kStreamFormatted.groupByKey(Grouped.<String,Integer>as(null)
.withValueSerde(Serdes.Integer()))
.aggregate(() -> 0,
(aggKey, newValue, aggValue) -> aggValue + newValue,
Materialized.<String, Integer, KeyValueStore<Bytes, byte[]>>
as("aggregated-stream-store")
.withKeySerde(Serdes.String())
.withValueSerde(Serdes.Integer())
).toStream().to("agg-output-topic", Produced.with(Serdes.String(), Serdes.Integer()));

Topology topology = builder.build();
KafkaStreams kafkaStreams = new KafkaStreams(topology, config);

CountDownLatch countDownLatch = new CountDownLatch(1);

// attach shutdown handler to catch control-c
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
@Override
public void run() {
kafkaStreams.close();
countDownLatch.countDown();
}
});

try {
kafkaStreams.start();
countDownLatch.await();
} catch (Throwable e) {
System.exit(1);
}
System.exit(0);
}
}

对于作为“John Smith”首次进入生产者控制台,我希望输出主题 (agg-output-topic) 应该有

John 1
Smith 1

如果我向生产者输入相同的输入(agg-table-source-topic),那么输出主题应该进行聚合并且结果应该是

John 2
Smith 2

感谢您的帮助。

最佳答案

SerializationException 表示您的Deserializer(在您的情况下为 IntegerDeserializer)无法反序列化消息 - 无法将字节转换为某个对象(Integer )

您如何在注释中将类型从 Long 更改为 Integer。我认为您首先使用 Long 类型启动您的应用程序并处理多条消息,然后将类型更改为 Integer。您的应用程序将中间结果保存在变更日志主题中,稍后使用类型和反序列化器 (Serdes),它无法反序列化并抛出异常。

如果您更改应用程序中的类型,您必须删除在处理过程中创建的所有更改日志主题。否则可能会发生 SerializationException

关于apache-kafka - 卡夫卡流 : How to fix Serde casting error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56037463/

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