Когда я имитирую случай подсчета слов с использованием функции агрегирования, я сталкиваюсь с проблемой приведения Серде.
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);
}
}
Для первой записи в качестве «Джона Смита» на консоли производителя, я ожидаю, что тема вывода (agg-output-topic) должен иметь
John 1
Smith 1
И если я введу тот же вход для источника (agg-table-source-topic), то выходной раздел должен иметь агрегацию, а результат должен быть
John 2
Smith 2
Я ценю вашу помощь.