Skip to content
Advertisement

How to split the string into different kafka topic based on some conditions

I am trying to split the string into different kafka topic based on conditions.
Here is the topology.

  1. Split the string into words.
  2. Match every words with conditions (here set of Good words and set of Bad words)
  3. If atleast 1 words from Bad words set found in the string, it will be sent to Bad-string
    topic otherwise it will be sent to Good-string topic.

Problem:

Every string is going to only one topic. (Bad-string topic)

Input:

  1. Your service was good.

  2. He was angry and sad.

  3. Your service was bad but still I am happy.

Output:
good-string (topic)

Your service was good. ( It contains Good words, “good” )

bad-string (topic)

  1. He was angry and sad ( It contains Bad words, “angry” and “sad” )
  2. Your service was bad but still I am happy. (Though, there is a Good word “happy” but there is atleast one Bad word “bad” )

Here is the code:

JavaScript

CountingDemo.java

JavaScript

Where am I wrong ?
Is there any better logic for this ?

Advertisement

Answer

The if statement is always false because the .toString() of a KStream object is the metadata of it, and never empty.

And if you want the full original string split between the two topics, you should not flatmap at all.

That being said, seems like you want

JavaScript

Where the two functions get the full input message and compare against the sets, rather than be given individual words.
Although, I think you only need one function to capture all messages with a good word to good-string, and all other messages (no good/bad, both good/bad, and some bad) to the bad-string topic

e.g.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement