-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducer.py
More file actions
37 lines (28 loc) · 1009 Bytes
/
Producer.py
File metadata and controls
37 lines (28 loc) · 1009 Bytes
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
import json
import time
import logging
from confluent_kafka import Producer
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Define the Kafka topic
kafka_topic = "meetup_events"
# Create a Kafka producer instance
producer = Producer({'bootstrap.servers': 'localhost:9092'})
with open('resources/meetup.json', 'r') as file:
for line in file:
try:
# Parse the JSON message from each line
meetup_event = json.loads(line.strip())
# Convert the event to a JSON string
event_json = json.dumps(meetup_event)
# Send the JSON message to the Kafka topic
producer.produce(kafka_topic, event_json)
logger.info("Sent 1 message to Kafka")
time.sleep(1)
except json.JSONDecodeError as e:
logger.error("Error decoding JSON: %s", e)
except Exception as e:
logger.error("Error: %s", e)
producer.flush()
producer.close()