def kafka_python_producer_sync(producer, topic, size): for n in range(size): data = get_registered_user() data['serial'] = n future = producer.send('myfirst_topic', data) result = future.get(timeout=60) print(result) producer.flush()
import kafka as k import json from data import get_registered_user import time def data_serializer(data): return json.dumps(data).encode('utf-8') producer = k.KafkaProducer(bootstrap_servers=['10.152.21.186:9092'],value_serializer=data_serializer) if __name__ == '__main__': while True: data = get_registered_user() print(data_serializer(data)) producer.send('registered_user',data) time.sleep(5)
from kafka import KafkaProducer import json from data import get_registered_user import time def json_serializer(data): return json.dumps(data).encode("utf-8") producer = KafkaProducer(bootstrap_servers=['192.168.0.10:9092'], value_serializer=json_serializer) if __name__ == "__main__": while 1 == 1: registered_user = get_registered_user() print(registered_user) producer.send("registered_user", registered_user) time.sleep(4)
def kafka_python_producer_async(producer, topic, size): for n in range(size): data = get_registered_user() data['serial'] = n producer.send(topic, data).add_callback(success).add_errback(error) producer.flush()