question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KafkaStreamsWordCountApplication failing with java.lang.ClassCastException: kafka.streams.word.count.KafkaStreamsWordCountApplication$WordCount cannot be cast to java.lang.String

See original GitHub issue

After the latest upgrade of the libraries to Spring Boot 2.2.0-SNAPSHOT and <spring-cloud-stream.version>Horsham.BUILD-SNAPSHOT</spring-cloud-stream.version> the KafkaStreamsWordCountApplication fails, as can be seen in the tests:

Caused by: java.lang.ClassCastException: kafka.streams.word.count.KafkaStreamsWordCountApplication$WordCount cannot be cast to java.lang.String
        at org.apache.kafka.common.serialization.StringSerializer.serialize(StringSerializer.java:28)
        at org.apache.kafka.common.serialization.Serializer.serialize(Serializer.java:60)
        at org.apache.kafka.streams.processor.internals.RecordCollectorImpl.send(RecordCollectorImpl.java:162)
        at org.apache.kafka.streams.processor.internals.RecordCollectorImpl.send(RecordCollectorImpl.java:102)
        at org.apache.kafka.streams.processor.internals.SinkNode.process(SinkNode.java:89)
        ... 32 more

The error happens after this map operation, before writing to the sink:

.map((key, value) -> new KeyValue<>(null, new WordCount(key.key(), value, new Date(key.window().start()), new Date(key.window().end()))));

I’ve tried with a previous version that worked and the topology included a mapvalues node that converted the WordCount object value to a byte array,:

image

The current topology doesn’t show that mapvalues processor anymore:

image

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
sobychackocommented, Jun 18, 2019

@codependent New changes went into core binder for native Serde changes and inferring Serdes on input/output as much as possible. I cleaned up all the Kafka Streams samples to reflect the changes. I ran your Kotlin sample locally and it runs as expected.

Here is the gist of the Serde changes that went into the master. These changes will only be available on the 3.0 (Horsham) line of Spring Cloud Stream and won’t be backported.

  1. Binder now uses native Serde as the default serialization and deserialization strategy for input and output bindings.
  2. Applications have to explicitly set useNativeDecoding and useNativeEncoding to false to force the conversion done by the framework.
  3. When using the default native conversion by Kafka Streams, applications don’t need to set Serdes used on key/value types if those types map to one of the following:
  • Integer
  • Float
  • Double
  • Long
  • Short
  • byte[]
  • String

If none of these matches, it defaults to JsonSerde provided by spring-kafka. If the application explicitly provides a Serde on the binding, then that gets precedence always. If the application uses Serde objects like Confluent’s Avro Serde, then that has to be set explicitly on the configuration.

These changes will also significantly reduce the topology depth of Spring Cloud Stream/Kafka Streams applications that you once reported on SO.

Once you confirm that things are working as expected, we can close this issue.

0reactions
sobychackocommented, Jun 20, 2019

That sounds like a good plan. Feel free to close this and open another one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kafka Streams tip: java.lang.ClassCastException
ClassCastException in your Kafka Streams app which goes something like [B cannot be cast to java.lang.String , just double check if you've specified...
Read more >
Kafka Streams - java.lang.ClassCastException: - Stack Overflow
I have written a Kafka streams application to calculate sum up count of transaction per person. Definition of Transaction POJO:
Read more >
java.lang.String cannot be cast to kafka.streams.KeyValue
Suppose the KTable is aq. I do something of this sort:- KTable aggregatedStream = aq.groupBy((key, value)->value).count();
Read more >
Kafka Streams Interactive Queries | Confluent Documentation
WordCountInteractiveQueriesExample: This application continuously counts the occurrences of words based on text data that is consumed in real-time from a Kafka ...
Read more >
[#KAFKA-8317] ClassCastException using KTable.suppress()
ClassCastException : org.apache.kafka.streams.kstream.Windowed cannot be cast to java.lang.String at org.apache.kafka.common.serialization.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found