0

Below is the code for branching, it streams to only one topic (the first one). As I understand, it should stream to all three topics?

Anyway I can stream to three topics using branch?

@Bean
        public Function<KStream<String, Usesr>, KStream<String, User>[]> testprocess() {

            Predicate<String, User> stream1 = (k, v) -> v != null;
            Predicate<String, User> stream2 = (k, v) -> v != null;
            Predicate<String, User> stream3 = (k, v) -> v != null;

            return input -> input.map(
                    (key, user) -> new KeyValue<String, User>(user.getId(), user))
                    .branch(stream1, stream2, stream3);

Configuration for the processor

        testprocess-in-0:
          destination: input.users
        testprocess-out-0:
          destination: users.test.out.0
        testprocess-out-1:
          destination: users.test.out.1
        testprocess-out-2:
          destination: users.test.out.2
jka
  • 77
  • 2
  • 8

1 Answers1

1

By looking at your predicates, it appears that the first predicate always wins and the others do not get a chance. In Kafka Streams branching, the first predicate that evaluates to true succeeds and the corresponding topic receives the data. You need to change the logic in the predicates so that the correct topic is appropriately mapped. Here is an example.

sobychacko
  • 5,099
  • 15
  • 26
  • Thank you @sobychacko for the response. To clarify, my use case is to copy the input stream to 3 different stream without any filter. Not sure if I can do this in the configuration: testprocess-in-0: destination: input.users testprocess-out-0: destination: users.test.out.0,users.test.out.1, users.test.out.2 – jka Jan 05 '20 at 04:03
  • 1
    Branching is mainly used when you have to split the input stream based on some particular filter logic expressed through the predicate. If that is not the case, and simply want to pass through the stream as itself to an output topic, then you might want to consider multiple processors or manually sending to the output topic using the `to` method on `KStream`. – sobychacko Jan 06 '20 at 16:40