KStream send record to multiple streams (not Branc

2020-04-29 15:56发布

Is there a way to make branch-like operation but to place record in each output stream which predicate evaluates to true? Brach puts record to first match (documentation: A record is placed to one and only one output stream on the first match).

2条回答
Anthone
2楼-- · 2020-04-29 16:36

I think you can use something like this:

KStream<String, String> inputStream = builder.stream("input");
List<Predicate<String, String>> predicates = new ArrayList<>(); // <-- list of predicates
List<KStream<String, String>> kStreams = predicates.stream()
        .map(inputStream::branch)
        .map(Arrays::asList)
        .map(listOfOneElementKStreams -> listOfOneElementKStreams.get(0)).collect(Collectors.toList());
查看更多
爷、活的狠高调
3楼-- · 2020-04-29 16:57

You can "broadcast" and filter each stream individually:

KStream stream = ...

stream1 = stream.filter(...);
stream2 = stream.filter(...);
// and so on...

If you use stream variable multiple times, all records are broadcasted to all downstream filters (or any other operator), ie, each filter is executed for each record.

查看更多
登录 后发表回答