无法运行Python Kafka生产者和使用者

时间:2018-08-27 20:43:00

标签: python apache-kafka kafka-consumer-api

因此,我编写了一个简单的kafka程序,该程序向生产者发送一条消息,但是当我运行它时,出现一个错误,提示kafka在60.0秒后无法更新元数据。如果有人能指出我所缺少的东西,那就太好了。

from kafka import KafkaConsumer, KafkaProducer

class Producer():
    def __init__(self):
        pass

    def start(self):
        print 'In Producer Start'
        producer = KafkaProducer(bootstrap_servers='localhost:9092')

        for _ in range(10):
            producer.send(b"test-topic", b"test")
            producer.send(b"test-topic", b"\xc2Hola, mundo!")

        producer.close()


class Consumer():
    def __init__(self):
        pass

    def start(self):
        consumer = KafkaConsumer(bootstrap_servers='localhost:9092', auto_offset_reset='earliest')
        consumer.subscribe(b"test-topic")

        for message in consumer:
            print message



def main():
    threads = [
        Producer(),
        Consumer()
    ]

    for t in threads:
        t.start()

if __name__ == "__main__":
    main()

0 个答案:

没有答案