-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-3ProduceAsynchronous.py
70 lines (54 loc) · 2.03 KB
/
2-3ProduceAsynchronous.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
from dataclasses import dataclass, field, asdict
from datetime import datetime
import json
import random
from confluent_kafka import Consumer, Producer
from confluent_kafka.admin import AdminClient, NewTopic
from faker import Faker
faker = Faker()
BROKER_URL = "PLAINTEXT://localhost:9092"
TOPIC_NAME = "org.udacity.exercise3.purchases"
TOPIC_NAME = "first_topic"
@dataclass
class Purchase:
username: str = field(default_factory=faker.user_name)
currency: str = field(default_factory=faker.currency_code)
amount: int = field(default_factory=lambda: random.randint(100, 200000))
# Serializes the object in JSON string format
def serialize(self):
# See: https://docs.python.org/3/library/json.html#json.dumps
return json.dumps(asdict(self))
def produce_sync(topic_name):
"""Produces data synchronously into the Kafka Topic"""
p = Producer({"bootstrap.servers": BROKER_URL})
start_time = datetime.utcnow()
curr_iteration = 0
# See: https://docs.confluent.io/current/clients/confluent-kafka-python/#confluent_kafka.Producer.flush
while True:
p.produce(topic_name, Purchase().serialize())
# p.flush() # 同步生產
if curr_iteration % 1000 == 0:
elapsed = (datetime.utcnow() - start_time).seconds
print(f"Messages sent: {curr_iteration} | Total elapsed seconds: {elapsed}")
curr_iteration += 1
pass
def main():
"""Checks for topic and creates the topic if it does not exist"""
create_topic(TOPIC_NAME)
try:
produce_sync(TOPIC_NAME)
except KeyboardInterrupt as e:
print("shutting down")
def create_topic(client):
"""Creates the topic with the given topic name"""
client = AdminClient({"bootstrap.servers": BROKER_URL})
futures = client.create_topics(
[NewTopic(topic=TOPIC_NAME, num_partitions=5, replication_factor=1)]
)
for _, future in futures.items():
try:
future.result()
except Exception as e:
pass
if __name__ == "__main__":
main()