无法挂起,无法获取KafkaConsumer消息

时间:2017-07-15 11:27:02

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

我已使用KafkaProducer将一些数据写入我的单分区主题,我试图通过循环访问消费者或{KafkaConsumer来查看此数据{1}}

poll()

两者似乎都没有正常工作,消费者循环确实打印出消息,但总是在import time from datetime import datetime, timedelta from kafka import KafkaProducer, KafkaConsumer, TopicPartition consumer = KafkaConsumer(bootstrap_servers='localhost:9092',group_id='my-group',enable_auto_commit=False) tp = TopicPartition(topic_name, 0) consumer.assign([tp]) consumer.seek_to_end(tp) last_offset = consumer.position(tp) producer = KafkaProducer(bootstrap_servers='localhost:9092') stopWriting = datetime.now() + timedelta(seconds=10) while datetime.now() < stopWriting: producer.send(topic='my-topic',value=str(datetime.now()).encode('utf-8')) time.sleep(1) producer.close() consumer.seek(tp, last_offset) #looping through the consumer for msg in consumer: print(msg) # or looping through the polled messages for msg in consumer.poll(): print(msg) 内以无限循环挂起。民意调查循环根本不打印任何东西。在没有挂起程序的情况下,是否有一些我没有读出所有新制作的消息?我使用的是Python 3.6.1和kafka/consumer/group.py(886)_message_generator版本1.3.4

1 个答案:

答案 0 :(得分:0)

我找到了{TopicParition:[ConsumerRecord]}的方法,首先你需要超时,因为没有消息在缓冲区中。接下来返回import sys records = consumer.poll(timeout_ms=sys.maxsize) for record in records[tp]: print(record) 的字典,因此您需要指定要从中读取消息的主题分区。

<VirtualHost *:80>
   ServerAdmin email@example.com
   UseCanonicalName Off
   ServerAlias *.localhost
   VirtualDocumentRoot /var/www/%-2
   <Directory /var/www/%-2>
      Options FollowSymLinks
      AllowOverride All
   </Directory>
   ErrorLog /var/log/apache2/localhost-error.log
   LogLevel warn
   CustomLog /var/log/apache2/localhost-access.log combined
</VirtualHost>