-
Notifications
You must be signed in to change notification settings - Fork 1
/
producer.py
44 lines (33 loc) · 1012 Bytes
/
producer.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
import json
import time
from kafka import KafkaProducer
from faker import Faker
TOPIC_NAME = "user-registration"
SERVER_ADDRESS = '127.0.0.1:9092'
producer = KafkaProducer(
bootstrap_servers=SERVER_ADDRESS,
value_serializer=lambda v: json.dumps(v).encode('utf-8')
)
def get_user_information() -> dict:
"""Generate Fake User Data from Python Faker Library"""
fake = Faker()
return {
"name": fake.name(),
"address": fake.address(),
"email": fake.email(),
"country": fake.country(),
}
if __name__ == "__main__":
"""
1. Get Fake User Data
2. Send the fake data to the consumer topic
3. Sleep 10 seconds and repeat the process utill press Crontrol C
"""
try:
while(True):
registered_user_data = get_user_information()
print(registered_user_data)
producer.send(TOPIC_NAME, registered_user_data)
time.sleep(10)
except KeyboardInterrupt:
print("Quit")