https://blog.csdn.net/learn_tech/article/details/81115996

(3) kafka-python生产者

import time
from kafka import KafkaProducer
 
 
producer = KafkaProducer(bootstrap_servers = ['192.168.17.64:9092', '192.168.17.65:9092', '192.168.17.68:9092'])
# Assign a topic
topic = 'test'
 
def test():
    print('begin')
    n = 1
    try:
        while (n<=100):
            producer.send(topic, str(n).encode())
            print("send" + str(n))
            n += 1
            time.sleep(0.5)
    except KafkaError as e:
        print(e)
    finally: 
        producer.close()
        print('done')
 
if __name__ == '__main__':
        test()

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注