2024-10-28 14:47:20 +04:00
|
|
|
from kafka import KafkaProducer, KafkaConsumer
|
|
|
|
import kafka
|
|
|
|
import socket
|
2024-10-28 19:43:13 +04:00
|
|
|
from json import dumps, loads
|
|
|
|
import time
|
2024-10-28 14:47:20 +04:00
|
|
|
|
2024-10-28 19:43:13 +04:00
|
|
|
consumer = KafkaConsumer(
|
|
|
|
'commands',
|
|
|
|
bootstrap_servers=['localhost:9092'],
|
|
|
|
auto_offset_reset='earliest',
|
|
|
|
enable_auto_commit=True,
|
|
|
|
group_id='my-group',
|
|
|
|
value_deserializer=lambda x: loads(x.decode('utf-8')))
|
2024-10-28 14:47:20 +04:00
|
|
|
|
|
|
|
print(consumer.topics())
|
2024-10-28 19:43:13 +04:00
|
|
|
consumer.subscribe(['commands'])
|
|
|
|
producer = KafkaProducer(bootstrap_servers = ['localhost:9092'],
|
|
|
|
value_serializer=lambda x:
|
|
|
|
dumps(x).encode('utf-8'))
|
|
|
|
|
|
|
|
data = {'message' : 'hello'}
|
|
|
|
producer.send('commands', value=data)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
for message in consumer:
|
|
|
|
print(message)
|
|
|
|
time.sleep(1)
|