-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
47 lines (35 loc) · 1.2 KB
/
generate.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
import json
import random
import time
from kafka import KafkaProducer
words = [
"apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew", "imbe", "jackfruit", "kiwi", "lemon", "mango", "nectarine", "orange", "papaya", "quince", "raspberry", "strawberry", "tangerine", "ugli", "vanilla", "watermelon", "xigua", "yellow watermelon", "zucchini"
]
# Kafka producer
producer = KafkaProducer(bootstrap_servers='localhost:9092')
# generate random data
while True:
print('sending data')
producer.send('words', value=str.encode(random.choice(words)))
# time.sleep(1)
# from kafka import KafkaProducer
# from kafka.errors import KafkaError
# producer = KafkaProducer(
# bootstrap_servers = "localhost:9092"
# )
# topic = "words"
# def on_success(metadata):
# print(f"Message produced to topic '{metadata.topic}' at offset {metadata.offset}")
# def on_error(e):
# print(f"Error sending message: {e}")
# # Produce asynchronously with callbacks
# for i in range(1, 11):
# msg = f"Order with id #{i}"
# future = producer.send(
# topic,
# value=str.encode(msg)
# )
# future.add_callback(on_success)
# future.add_errback(on_error)
# producer.flush()
# producer.close()