-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer_server.py
30 lines (23 loc) · 956 Bytes
/
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
from kafka import KafkaProducer
import json
import time
class ProducerServer(KafkaProducer):
def __init__(self, input_file, topic, **kwargs):
super().__init__(**kwargs)
self.input_file = input_file
self.topic = topic
self.counter = 0
#Read data from json file and feed to topic
def generate_data(self):
with open(self.input_file) as f:
file_data = json.load(f)
for row in file_data:
message = self.dict_to_binary(row)
# TODO send the correct data
future_record = self.send(topic = self.topic,value=message)
self.counter = self.counter + 1
print(f"Record number : {self.counter}. Record : {future_record.get()}")
time.sleep(1)
# return the json dictionary to binary
def dict_to_binary(self, json_dict):
return json.dumps(json_dict).encode('utf8')