-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer_server.py
38 lines (31 loc) · 1.1 KB
/
producer_server.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
from kafka import KafkaProducer
import json
import time
import datetime
import logging
from confluent_kafka.admin import AdminClient, NewTopic
logger = logging.getLogger(__name__)
class ProducerServer(KafkaProducer):
def __init__(self, input_file, topic, **kwargs):
super().__init__(**kwargs)
self.input_file = input_file
self.topic = topic
def read_data_file(self):
"""Read json data file."""
with open(self.input_file, "r") as src:
data = json.load(src)
return data
# TODO fill this in to return the json dictionary to binary
@staticmethod
def dict_to_binary(json_dict):
return json.dumps(json_dict).encode("utf-8")
# TODO we're generating a dummy data
def generate_data(self):
"""Send messages to Kafka topic"""
with open(self.input_file, "r") as f:
calls = json.load(f)
for call in calls:
message = self.dict_to_binary(call)
# TODO send the correct data
self.send(topic=self.topic, value=message)
time.sleep(1)