I have been checking Kafka streams. I have been testing the below code for Kafka streams
Producer topic: (this is the first producer topic - which sends the below json data)
KafkaProducer<String, String> producer = new KafkaProducer<>(
properties);
producer.send(new ProducerRecord<String,String>(topic, jsonobject.toString()));
producer.close();
JSON - Producer from topic:
{"UserID":"1","Address”:”XXX”,”AccountNo":"234234","MemberName”:”Stella”,”AccountType":"Savings"}
Stream Topic code: (this is the second Streaming code and topic)
builder.<String,String>stream(topic)
.filter(new Predicate <String, String>() {
@Override
public boolean test(String key, String value) {
// put you processor logic here
System.out.println("value : " + value);
return value.substring(0).equals(“1”);
}
})
.to(streamouttopic);
final KafkaStreams streams = new KafkaStreams(builder, props);
final CountDownLatch latch = new CountDownLatch(1);
// attach shutdown handler to catch control-c
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
@Override
public void run() {
streams.close();
latch.countDown();
}
});
try {
streams.start();
latch.await();
} catch (Throwable e) {
System.exit(1);
}
System.exit(0);
I want to filer if UserID value is “1”, then send that data to destination streaming topic.
When I use “.filter” and print System.out.println("value : " + value);, it throws the below error when executing.
Exception in thread "SampleStreamProducer-a6bb543e-bb92-48d0-8d9f-225046722d81-StreamThread-1" java.lang.ClassCastException: [B cannot be cast to java.lang.String
If i don’t use “.filter” and use simple code like this, builder.stream(topic).to(streamouttopic);
, it is working fine, but without filtering. But, I need to use that filter.
Can someone guide me to fix it?