Python測試Kafka叢集(kafka-python)

右介發表於2017-10-18

生產者程式碼:

import time
from kafka import SimpleProducer, KafkaClient
from kafka import KafkaProducer


producer = KafkaProducer(bootstrap_servers = ['10.200.1.X:9092', '10.200.1.X:9092', '10.200.1.X:9092'])
# Assign a topic
topic = 'my-topic'

def test():
    print('begin')
    n = 1
    while (n<=100):
        producer.send(topic, str(n))
        print "send" + str(n)
        n += 1
        time.sleep(0.5)
    print('done')

if __name__ == '__main__':
    test()

 

消費者程式碼:

from kafka import KafkaConsumer

#connect to Kafka server and pass the topic we want to consume
consumer = KafkaConsumer('my-topic', bootstrap_servers = ['10.200.1.X:9092', '10.200.1.X:9092', '10.200.1.X:9092'])

for msg in consumer:
    print msg

 

相關文章