Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Map returned by defaultBranch/noDefaultBranch allows us to collect all the KStream branch objects in single scope. The branches collected are the results of transformations defined by `withChain` `chain` functions. If a function returns `null`, its result is omitted.

Simple Example: Direct Branch Consuming

In many cases we do not need to have a single scope for all the branches, each branch being processed completely independently from others. Then we can use 'consuming' lambdas or method references in Branched parameter:

Code Block
languagejava
source.split()
.branch((key, value) -> value.contains("A"), Branched.withJavaConsumerwith(ks->ks>{ks.to("A"); return ks;}))
.branch((key, value) -> value.contains("B"), Branched.withJavaConsumerwith(ks->ks>{ks.to("B"); return ks;}))
.defaultBranch(Branched.withJavaConsumer(ks->ks>{ks.to("C"); return ks;}));

More Complex Example: Merging Branches

In other cases we want to combine branches again after splitting. The map returned by defaultBranch /noDefaultBranch methods provides  access to the branches in the same scope:

Code Block
languagejava
Map<String, KStream<String, String>> branches = source.split()
  .branch((key, value) -> value == null, 
          Branched.withName("null").withChain(with(s->s.mapValues(v->"NULL"), "null")
  .defaultBranch(
          Branched.withNameas("non-null"));  
  
branches.get("non-null")
 .merge(branches.get("null"));

...