Newer
Older
docker-analytics / examples / sample_producer.py
Nigel Stanger on 17 May 2019 462 bytes Added examples
#!/usr/local/bin/python

import time
from datetime import datetime, timedelta

terminate = datetime.now() + timedelta(seconds=20)

from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='kafka:9092')

count = 1
while True: #datetime.now() < terminate:
    text = "Hello {c}".format(c=count)
    print(text)
    producer.send("sample", bytes(text, encoding="utf-8"))
    # producer.send("sample", b"foobar")
    time.sleep(0.5)
    count += 1