Slide 1

Slide 1 text

Stateful & Reactive Stream Processing Applications without a Database Apache Kafka Streams ❤ Spring Boot 2.0 @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland

Slide 2

Slide 2 text

$ whoami • Hans-Peter Grahsl • working & living in Graz • technical trainer at • independent consultant & engineer • associate lecturer • " irregular conference speaker @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 2

Slide 3

Slide 3 text

challenges in today's data architectures • rising number of apps producing + consuming data • need to integrate ever more data sources • heterogeneous environments all over the place • traditional technologies may struggle to cope with this @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 3

Slide 4

Slide 4 text

challenges may lead to a GIANT MESS @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 4

Slide 5

Slide 5 text

Apache Kafka @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 5

Slide 6

Slide 6 text

STREAMING PLATFORM @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 6

Slide 7

Slide 7 text

much more than messaging • Apache Kafka is offering 3 key capabilities • publish / subscribe to streams of records • (permanently) store streams of records • process streams of records in near real-time fault-tolerance & horizontal scalability @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 7

Slide 8

Slide 8 text

Producer API @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 8

Slide 9

Slide 9 text

Consumer API @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 9

Slide 10

Slide 10 text

Connect API @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 10

Slide 11

Slide 11 text

Streams API @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 11

Slide 12

Slide 12 text

Kafka Streams API • stream processing with a library only approach • lightweight applications • build however & deploy wherever you like • NO(!) additional clusters or frameworks e.g. • Processor API & Streams DSL • configurable delivery guarantees @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 12

Slide 13

Slide 13 text

writing applications NOT (!) managing clusters @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 13

Slide 14

Slide 14 text

Meet KSQL for skyrocketing productivity @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 14

Slide 15

Slide 15 text

KSQL • SQL streaming engine for Kafka • concise & expressive • SQL-like language and semantics • NO(!) coding required • extremely low entry barrier • joins, aggregations, windowing • UD(A)Fs - UDTFs pending... • built on top of KStream API @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 15

Slide 16

Slide 16 text

@hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 16

Slide 17

Slide 17 text

central nervous system @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 17

Slide 18

Slide 18 text

example ? hmmmm... @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 18

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

example: near real-time Emoji Tracking

Slide 21

Slide 21 text

HOW TO build this? @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 21

Slide 22

Slide 22 text

emoji tracking | step 1 store ingest subset of public live tweets from Twitter @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 22

Slide 23

Slide 23 text

emoji tracking | step 2 process extract emojis - group & count them - maintain top N @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 23

Slide 24

Slide 24 text

emoji tracking | step 3 query single emoji count - all emoji counts - top N emojis @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 24

Slide 25

Slide 25 text

emoji tracking | step 4 notify consumable near-realtime change streams of updates @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 25

Slide 26

Slide 26 text

Let's do it! @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 26

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

example: step 1 ingest tweets • using Kafka Connect • e.g. this community connector https://github.com/jcustenborder/kafka- connect-twitter • configure the connector (JSON) • manage connector via REST-like API create | pause | resume | delete | status @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 28

Slide 29

Slide 29 text

{ "name": "tweets-twitter-source", "config": { "connector.class": "c.g.j.k.c.t.TwitterSourceConnector", "twitter.oauth.accessToken": "...", "twitter.oauth.consumerSecret": ...", "twitter.oauth.consumerKey": "...", "twitter.oauth.accessTokenSecret": "...", "kafka.status.topic": "tweets", "process.deletes": false, "key.converter": "org.apache.kafka.connect.json.JsonConverter", "key.converter.schemas.enable": false, "value.converter": "org.apache.kafka.connect.json.JsonConverter", "value.converter.schemas.enable": false, "filter.keywords": "..." } } @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 29

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

! ! ! ! NO CODE! ! ! ! ! @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 31

Slide 32

Slide 32 text

example: step 2 process tweets • using Kafka Streams high-level DSL • grouping and counting emojis • updating top N emoji counts • map tweets to emoji occurrences • only a few lines of Java @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 32

Slide 33

Slide 33 text

calculate emoji counts • It all starts with tweets like this... ! this is a twitter status ⛰ text with ## five emojis @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 33

Slide 34

Slide 34 text

calculate emoji counts Key Value raw input ID !this is a twitter! status ⛰ text with ## five emojis extract emoji list ID [!,!,⛰,#,#] @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 34

Slide 35

Slide 35 text

calculate emoji counts Key Value flatten the list ID ! ID ! ID ⛰ ID # ID # @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 35

Slide 36

Slide 36 text

calculate emoji counts Key Value set keys to values ! "" ! "" ⛰ "" # "" # "" finally group & count by key @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 36

Slide 37

Slide 37 text

result: continuously updated KTable with emoji counts Key Value ! 2 ⛰ 1 # 2 ... ... @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 37

Slide 38

Slide 38 text

1:1 mapping to KStreams API KTable emojiCounts = tweets.map((id,tweet) -> KeyValue.pair(id, EmojiUtils...)) .flatMapValues(emojis -> emojis) .map((id,emoji) -> KeyValue.pair(emoji, "")) .groupByKey(...).count(...); @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

example: step 3 query results • access to state stores with interactive queries • KStreams offers all needed metadata • ! RPC integration left for developers > Reactive WebAPI powered by Spring Boot 2.0 < @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 40

Slide 41

Slide 41 text

REST controller @RestController @RequestMapping("interactive/queries/") @CrossOrigin(origins = "*") public class StateStoreController { private final StateStoreService service; [...] } @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 41

Slide 42

Slide 42 text

REST controller methods @GetMapping("emojis/{code}") public Mono> getEmoji(@PathVariable String code) { return service.querySingleEmojiCount(code); } @GetMapping("emojis") public Flux getEmojis() { return service.queryAllEmojiCounts(); } @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 42

Slide 43

Slide 43 text

state store access in service StreamsMetadata metadata = kafkaStreams.metadataForKey( "your-store-name", emoji, Serializer... ); @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 43

Slide 44

Slide 44 text

state store access in service if(itsMe.equals(metadata.hostInfo())) { ReadOnlyKeyValueStore kvStoreEmojiCounts = kafkaStreams.store("your-store-name", QueryableStoreTypes.keyValueStore()); Long count = kvStoreEmojiCounts.get(emoji); return Mono.just( new ResponseEntity<>(new EmojiCount(...),HttpStatus.OK) ); } @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 44

Slide 45

Slide 45 text

state store access in service String location = String.format("http://%s:%d/.../%s", metadata.host(),metadata.port(),emoji); return Mono.just( ResponseEntity.status(HttpStatus.FOUND) .location(URI.create(location)).build() ); @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 45

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

example: step 4 real-time notifications • reactively consume from changelog topics • stream any changes to clients using SSE > Project Reactor's reactor-kafka < @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 48

Slide 49

Slide 49 text

notifications via SSE @GetMapping(path = "emojis/updates/notify", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux getEmojiCountsStream() { return service.consumeEmojiCountsStream(); } @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 49

Slide 50

Slide 50 text

! LIVE ! DASHBOARD

Slide 51

Slide 51 text

mission accomplished @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 51

Slide 52

Slide 52 text

try it yourself! source https://github.com/hpgrahsl/voxxed-days-ticino-2018 slides https://speakerdeck.com/hpgrahsl/stateful-and-reactive-stream- processing-applications-without-a-database-at- voxxedticino-2018 @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 52

Slide 53

Slide 53 text

THANK YOU Q&A ? @hpgrahsl | #VDT18 #VoxxedDays Ticino, 20th October 2018, Switzerland 53

Slide 54

Slide 54 text

No content