Skip to content

Commit 7bedf05

Browse files
committed
Added ksqlDB queries
1 parent 7c7a649 commit 7bedf05

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,95 @@ Detailed view of all microservices and to what Kafka topics their produce and ar
6161
Confluent Cloud Stream Lineage view:
6262
![image](static/images/docs/cc-stream-lineage.png)
6363

64+
### ksqlDB queries
65+
#### Collections
66+
Streams and tables are the two primary abstractions, they are referred to as collections. There are two ways of creating collections in ksqlDB:
67+
- directly from Kafka topics (source collections)
68+
- derived from other streams and tables (derived collections)
69+
70+
**Source Collections**: The topics produced/consumed by the microservices need to be ingested by ksqlDB so they can be stream processed:
71+
```
72+
CREATE STREAM IF NOT EXISTS PIZZA_ORDERED (
73+
order_id VARCHAR KEY,
74+
status INT,
75+
timestamp BIGINT,
76+
order STRUCT<
77+
extra_toppings ARRAY<STRING>,
78+
username STRING,
79+
customer_id STRING,
80+
sauce STRING,
81+
cheese STRING,
82+
main_topping STRING
83+
>
84+
) WITH (
85+
KAFKA_TOPIC = 'pizza-ordered',
86+
VALUE_FORMAT = 'JSON',
87+
TIMESTAMP = 'timestamp'
88+
);
89+
90+
CREATE STREAM IF NOT EXISTS PIZZA_ASSEMBLED (
91+
order_id VARCHAR KEY,
92+
status INT,
93+
baking_time INT,
94+
timestamp BIGINT
95+
) WITH (
96+
KAFKA_TOPIC = 'pizza-assembled',
97+
VALUE_FORMAT = 'JSON',
98+
TIMESTAMP = 'timestamp'
99+
);
100+
101+
CREATE STREAM IF NOT EXISTS PIZZA_BAKED (
102+
order_id VARCHAR KEY,
103+
status INT,
104+
timestamp BIGINT
105+
) WITH (
106+
KAFKA_TOPIC = 'pizza-baked',
107+
VALUE_FORMAT = 'JSON',
108+
TIMESTAMP = 'timestamp'
109+
);
110+
111+
STREAM_DELIVERED: f"""CREATE STREAM IF NOT EXISTS PIZZA_DELIVERED (
112+
order_id VARCHAR KEY,
113+
status INT,
114+
timestamp BIGINT
115+
) WITH (
116+
KAFKA_TOPIC = 'pizza-delivered',
117+
VALUE_FORMAT = 'JSON',
118+
TIMESTAMP = 'timestamp'
119+
);
120+
121+
CREATE STREAM IF NOT EXISTS PIZZA_PENDING (
122+
order_id VARCHAR KEY,
123+
status INT,
124+
timestamp BIGINT
125+
) WITH (
126+
KAFKA_TOPIC = 'pizza-pending',
127+
VALUE_FORMAT = 'JSON',
128+
TIMESTAMP = 'timestamp'
129+
);
130+
131+
CREATE STREAM IF NOT EXISTS PIZZA_STATUS (
132+
order_id VARCHAR KEY,
133+
status INT,
134+
timestamp BIGINT
135+
) WITH (
136+
KAFKA_TOPIC='pizza-status',
137+
VALUE_FORMAT='JSON',
138+
TIMESTAMP='timestamp'
139+
);
140+
```
141+
142+
**Derived Collections**: With the source collections created (streams) we can now extract the status field of each event and have them merged into a single topic/stream by creating persistent queries:
143+
```
144+
INSERT INTO PIZZA_STATUS SELECT order_id, status, timestamp FROM PIZZA_ORDERED EMIT CHANGES;
145+
INSERT INTO PIZZA_STATUS SELECT order_id, status, timestamp FROM PIZZA_ASSEMBLED EMIT CHANGES;
146+
INSERT INTO PIZZA_STATUS SELECT order_id, status, timestamp FROM PIZZA_BAKED EMIT CHANGES;
147+
INSERT INTO PIZZA_STATUS SELECT order_id, status, timestamp FROM PIZZA_DELIVERED EMIT CHANGES;
148+
INSERT INTO PIZZA_STATUS SELECT order_id, status, timestamp FROM PIZZA_PENDING EMIT CHANGES;
149+
```
150+
64151
### Start now!
65-
You can setup your own environment to do the tests, or go straight to the public demo by clicking <a href="http://confluent-pizza-demo-1570877491.eu-west-1.elb.amazonaws.com" title="Start demo" target="_blank">here</a>.
152+
You can setup your own environment to do the tests, or go straight to the <a href="http://confluent-pizza-demo-1570877491.eu-west-1.elb.amazonaws.com" title="Start demo" target="_blank"><b>public demo by clicking here</b></a>.
66153
- Enter your username
67154
- No need for password
68155
- You will only be able to see your own orders, you cannot see someone else's

0 commit comments

Comments
 (0)