kafka-json-producer_2.py 369 B

12345678910111213
  1. def run(self):
  2. producer = KafkaProducer(bootstrap_servers='localhost:9092', value_serializer=lambda m: json.dumps(m).encode('ascii'))
  3. with open('credit_test.json') as json_file:
  4. data = json.load(json_file)
  5. for p in data:
  6. print (p)
  7. producer.send('credit-card-tx', p)
  8. time.sleep(5)
  9. producer.close()