Apache Kafka: check for message in topic

I have a situation where I need to check if a particular post exists in a topic or not, I don't need absolutely any duplicates in a topic.

Can anyone suggest any neat way to do this, instead of consuming all messages and checking them.

+3


source to share


1 answer


I do not consider myself an expert in Kafka, but I think you are pretending to be "against" the essence of Kafka.

However, I am getting a solution using the Kafka Streams library for Java. Basically, the process is as follows:

  • Match each message with a new key value, where the key is a combination of the previous key and its value: (key1, message1) -> (key1-message1, message1)

  • Group messages using keys, as a result of this operation you get KGroupedStream .

  • Apply the decrement function by changing the value to some custom value, such as the Duplicate Value string.

  • Convert the resulting KTable after pruning to a KStream and paste it into a new Kafka theme.

There are so many assumptions in the previous explanation, I'm going to provide some code to give some light:



KStreamBuilder builder = new KStreamBuilder();
KStream<String, String> resources =  builder.stream("topic-where-the-messages-are-sent");

KeyValueMapper<String, String, KeyValue<String,String>> kvMapper = new KeyValueMapper<String, String, KeyValue<String,String>>() {
    public KeyValue<String, String> apply(String key, String value) {
        return new KeyValue<String, String>(key + "-" + value, value);
    }
};

Reducer<String> reducer = new Reducer<String>() {
    public String apply(String value1, String value2) {
        return "Duplicated message";
    }
};

resources.map(kvMapper)
    .groupByKey()
    .reduce(reducer, "test-store-name")
    .toStream()
    .to("unique-message-output");

KafkaStreams streams = new KafkaStreams(builder, props);
streams.start();

      

Keep in mind that this is probably not the optimal solution, and you may not consider it a "graceful" way to solve your problem.

Hope this helps.

0


source







All Articles