Last active
April 19, 2020 19:09
-
-
Save abrekken/4f6f7d57aaeefad4f8a8c703405af341 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Bean | |
| public KStream<?, ?> kstreamTombstone(StreamsBuilder streamsBuilder) { | |
| KStream<String, IotEvent> inbound = streamsBuilder.stream(inputTopic, Consumed.with(Serdes.String(), iotEventSerde())); | |
| inbound | |
| .peek((key, value) -> LOG.debug("incoming message: {} {}", key, value)) | |
| //check for a null value (tombstone) and set delete flag if necessary | |
| .mapValues((readOnlyKey, value) -> { | |
| if (value == null) { | |
| value = new IotEvent(); | |
| value.setDelete(true); | |
| } | |
| return value; | |
| }) | |
| //re-partition our data on a new key | |
| .groupBy((key, value) -> eventKey(value), Grouped.with(Serdes.String(), iotEventSerde())) | |
| //check for delete flag and return null if true | |
| .aggregate(IotEventMetric::new, | |
| (key, value, aggregate) -> { | |
| if (value.isDelete()) { | |
| return null; | |
| } | |
| return aggregate.add(value); | |
| }, | |
| Materialized.<String, IotEventMetric, KeyValueStore<Bytes, byte[]>>as("eventstore") | |
| .withValueSerde(iotEventMetricSerde()) | |
| ) | |
| .toStream() | |
| //publish our results to a topic | |
| .to("output-topic", Produced.with(Serdes.String(), iotEventMetricSerde())); | |
| return inbound; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found it out. Thanks a lot :-D