-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-2CreateTopic.py
95 lines (73 loc) · 2.71 KB
/
2-2CreateTopic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import asyncio
from confluent_kafka import Consumer, Producer
from confluent_kafka.admin import AdminClient, NewTopic
BROKER_URL = "PLAINTEXT://localhost:9092"
def topic_exists(client, topic_name):
topic_metadata = client.list_topics(timeout=5)
return topic_metadata.topics.get(topic_name) is not None
def create_topic(client, topic_name):
# See: https://docs.confluent.io/current/clients/confluent-kafka-python/#confluent_kafka.admin.NewTopic
# See: https://docs.confluent.io/current/installation/configuration/topic-configs.html
futures = client.create_topics(
[
NewTopic(
topic=topic_name,
num_partitions=5,
replication_factor=1,
config={
"cleanup.policy": "compact",
"compression.type": "lz4",
"delete.retention.ms": 100,
"file.delete.delay.ms": 100,
}
)
]
)
for topic, future in futures.items():
try:
future.result()
print("topic created")
except Exception as e:
print(f"failed to create topic {topic_name}: {e}")
raise
def main():
"""Checks for topic and creates the topic if it does not exist"""
client = AdminClient({"bootstrap.servers": BROKER_URL})
topic_name = "first_topic"
exists = topic_exists(client, topic_name)
print(f"Topic {topic_name} exists: {exists}")
if exists is False:
create_topic(client, topic_name)
try:
asyncio.run(produce_consume(topic_name))
except KeyboardInterrupt as e:
print("shutting down")
async def produce_consume(topic_name):
"""Runs the Producer and Consumer tasks"""
t1 = asyncio.create_task(produce(topic_name))
t2 = asyncio.create_task(consume(topic_name))
await t1
await t2
async def produce(topic_name):
"""Produces data into the Kafka Topic"""
p = Producer({"bootstrap.servers": BROKER_URL})
curr_iteration = 0
while True:
p.produce(topic_name, f"iteration {curr_iteration}".encode("utf-8"))
curr_iteration += 1
await asyncio.sleep(0.5)
async def consume(topic_name):
"""Consumes data from the Kafka Topic"""
c = Consumer({"bootstrap.servers": BROKER_URL, "group.id": "0"})
c.subscribe([topic_name])
while True:
message = c.poll(1.0)
if message is None:
print("no message received by consumer")
elif message.error() is not None:
print(f"error from consumer {message.error()}")
else:
print(f"consumed message {message.key()}: {message.value()}")
await asyncio.sleep(2.5)
if __name__ == "__main__":
main()