🤖Building a Telegram bot with Apache Kafka, Go, and ksqlDB

A presentation at Devoxx Ukraine in November 2020 in by Robin Moffatt

Slide 1

Slide 1

🤖Building a Telegram bot with Apache Kafka and ksqlDB @rmoff Robin Moffatt

Slide 2

Slide 2

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

Slide 6

Slide 6

Where’s my nearest carpark with available spaces?

Slide 7

Slide 7

How many spaces are available in this car park?

Slide 8

Slide 8

💡Tell me when a car park with spaces is available

Slide 9

Slide 9

📈How does occupancy vary over time?

Slide 10

Slide 10

$ whoami > Robin Moffatt (@rmoff) > Senior Developer Advocate at Confluent (Apache Kafka, not Wikis 😉) > Working in data & analytics since 2001 > Oracle ACE Director (Alumnus) http://rmoff.dev/talks · http://rmoff.dev/blog · http://rmoff.dev/youtube @rmoff

Slide 11

Slide 11

Slide 12

Slide 12

Telegram @rmoff

Slide 13

Slide 13

Don’t just tell me… show me! Demo code: https://rmoff.dev/carparks

Slide 14

Slide 14

carparks HTTP Kafka @rmoff

Slide 15

Slide 15

What are the key pieces of the design? @rmoff

Slide 16

Slide 16

Event Driven Alerts carparks HTTP Kafka @rmoff

Slide 17

Slide 17

K/V Lookups (materialised views) SELECT SPACES_AVAILABLE How many spaces are free at “ FROM CARPARK WHERE NAME=’WESTGATE’; Westgate carpark right now? ksqlDB ” Kafka CARPARK_EVENTS 42 CREATE TABLE CARPARK AS SELECT LATEST(… GROUP BY NAME “ There are 42 spaces free ” @rmoff

Slide 18

Slide 18

A schema… carparks HTTP @rmoff

Slide 19

Slide 19

A schema… 2020-10-14,12:28,Broadway,1132,921 2020-10-14,12:28,Kirkgate Centre,611,474 2020-10-14,12:28,Sharpe Street,98,63 ?! @rmoff

Slide 20

Slide 20

My kingdom for a schema! 2020-10-14,12:28,Broadway,1132,921 2020-10-14,12:28,Kirkgate Centre,611,474 2020-10-14,12:28,Sharpe Street,98,63 😍 { “ts”: “2020-10-14T12:28 UTC+1”, “name”: “Broadway”, “capacity”: 1132, “empty”: 921 } … @rmoff

Slide 21

Slide 21

Applying a schema to streams of data source_topic ksqlDB CREATE STREAM mySource (date VARCHAR , time VARCHAR , name VARCHAR , capacity INT ) WITH (KAFKA_TOPIC=’source_topic’, VALUE_FORMAT=’DELIMITED’); Kafka @rmoff

Slide 22

Slide 22

Applying a schema to streams of data source_topic ksqlDB Kafka derived_topic CREATE STREAM mySource (date VARCHAR , time VARCHAR , name VARCHAR , capacity INT ) WITH (KAFKA_TOPIC=’source_topic’, VALUE_FORMAT=’DELIMITED’); CREATE STREAM myTargetStream WITH (VALUE_FORMAT=’PROTOBUF’, KAFKA_TOPIC=’derived_topic’) AS SELECT * FROM mySource; @rmoff

Slide 23

Slide 23

Integration carparks HTTP Kafka @rmoff

Slide 24

Slide 24

Streaming Integration with Kafka Connect syslog Sources Tasks Workers Kafka Connect Kafka Brokers @rmoff

Slide 25

Slide 25

Streaming Integration with Kafka Connect Amazon S3 Google BigQuery Sinks Tasks Workers Kafka Connect Kafka Brokers @rmoff

Slide 26

Slide 26

Streaming Integration with Kafka Connect Amazon S3 syslog Google BigQuery Tasks Workers Kafka Connect Kafka Brokers @rmoff

Slide 27

Slide 27

Streaming Analytics @rmoff

Slide 28

Slide 28

Why build it this way? @rmoff

Slide 29

Slide 29

Events @rmoff

Slide 30

Slide 30

Streams of Events @rmoff

Slide 31

Slide 31

We want to react to them as they happen @rmoff

Slide 32

Slide 32

We want to build state from a stream of events @rmoff

Slide 33

Slide 33

We want to provide the latest data in our analytics @rmoff

Slide 34

Slide 34

Apache Kafka - an Event Streaming Platform Producer Connectors Consumer The Log Connectors Streaming Engine @rmoff

Slide 35

Slide 35

Why Kafka? @rmoff

Slide 36

Slide 36

Distributed, Immutable, Event Log Old New Events are added at the end of the log @rmoff

Slide 37

Slide 37

Consumers can seek to any point Read to offset & scan Old New @rmoff

Slide 38

Slide 38

Data is not deleted once read New Old Sally is here Scan @rmoff

Slide 39

Slide 39

Consumers are independent of each other New Old Fred is here Scan Sally is here Scan @rmoff

Slide 40

Slide 40

Consumers can be added later Rick is here Scan New Old Fred is here Scan Sally is here Scan @rmoff

Slide 41

Slide 41

Stream Processing with ksqlDB Source stream @rmoff

Slide 42

Slide 42

Stream Processing with ksqlDB Source stream @rmoff

Slide 43

Slide 43

Stream Processing with ksqlDB Source stream @rmoff

Slide 44

Slide 44

Stream Processing with ksqlDB Source stream Analytics @rmoff

Slide 45

Slide 45

Stream Processing with ksqlDB Source stream Applications / Microservices @rmoff

Slide 46

Slide 46

Stream Processing with ksqlDB …SUM(TXN_AMT) GROUP BY AC_ID AC _I D= 42 BA LA NC AC E= _I 94 D= .0 42 0 Source stream Applications / Microservices @rmoff

Slide 47

Slide 47

Under the covers of ksqlDB Photo by @rmoff on

Slide 48

Slide 48

Kafka cluster consume produce ksqlDB @rmoff

Slide 49

Slide 49

JVM Kafka cluster consume produce ksqlDB Kafka Streams RocksDB @rmoff

Slide 50

Slide 50

Slide 51

Slide 51

k & ^ Kafka Fully Managed as a Service B D l q s

Slide 52

Slide 52

Running ksqlDB - self-managed DEB, RPM, ZIP, TAR downloads http://confluent.io/download Docker images ksqlDB Server confluentinc/ksqldb-server (JVM process) …and many more… @rmoff

Slide 53

Slide 53

Why Kafka? @rmoff

Slide 54

Slide 54

Stream Store Process Integrate @rmoff

Slide 55

Slide 55

Stream Store Process Integrate @rmoff

Slide 56

Slide 56

Stream Store Process Integrate @rmoff

Slide 57

Slide 57

Stream Store Process Integrate @rmoff

Slide 58

Slide 58

Stream Store Process Integrate @rmoff

Slide 59

Slide 59

Flexible, event-driven applications Event-driven alerts Key/Value lookups Streaming ETL @rmoff

Slide 60

Slide 60

on Photo by Want to learn more? CTAs, not CATs (sorry, not sorry) @rmoff

Slide 61

Slide 61

Try it out for yourself https://rmoff.dev/carparks

Slide 62

Slide 62

60 DE VA DV $200 USD off your bill each calendar month for the first three months when you sign up https://rmoff.dev/ccloud Free money! (additional $60 towards your bill 😄 ) Fully Managed Kafka as a Service * T&C: https://www.confluent.io/confluent-cloud-promo-disclaimer

Slide 63

Slide 63

Learn Kafka. Start building with Apache Kafka at Confluent Developer. developer.confluent.io

Slide 64

Slide 64

Confluent Community Slack group cnfl.io/slack @rmoff

Slide 65

Slide 65

Further reading / watching https://rmoff.dev/kafka-talks @rmoff

Slide 66

Slide 66

#EOF https://talks.rmoff.net @rmoff