无法从Python客户端读取Kafka主题

时间:2018-06-15 21:43:47

标签: python-2.7 apache-kafka

我使用Maxwell的守护进程复制数据库日志。目前,当我对目标表进行更改时,它们会显示在Kafka主题中,我可以通过控制台使用者查看它们,但不能在我的python Kafka使用者中查看。

from kafka import KafkaConsumer
from ata import realm
from kafka_libraries.graceful_dier import GracefulDier
from kafka_libraries.utils import load_config

import cjson

if realm.REALM == realm.PROD:
    SHARED_KAFKA_CONFIG = load_config('prod_data')
else:
    SHARED_KAFKA_CONFIG = load_config(realm.REALM)

class KafkaMonitor(GracefulDier):
    def __init__(self, database, table, group_id=None):
        super(KafkaMonitor, self).__init__()
        print(SHARED_KAFKA_CONFIG['bootstrap_servers'])
        self.kconsumer = KafkaConsumer(
            self._get_consumer_topic(database, table),
            value_deserializer=lambda m: cjson.decode(m),
            group_id=group_id,
            bootstrap_servers=SHARED_KAFKA_CONFIG['bootstrap_servers'],
        )

    def _get_consumer_topic(self, database, table):
        return 'db_changes_%s_%s' % (database, table)

    def cleanup(self):
        self.kconsumer.close()

    def listen(self):
        while True:
            for message_list in self.kconsumer.poll(timeout_ms=5000).values():
                for raw_message in message_list:
                    msg = raw_message.value
                    print(msg)
            if self.kill_now:
                break
        self.cleanup()

if __name__ == '__main__':
    km = KafkaMonitor('squid_metrics', 'bq_pipeline_monitor')
    km.listen()

打印订阅时,字符串与我提供给控制台消费者的字符串相同。有没有人知道问题是什么,我很难过?

使用maxwell-1.15.0和kafka_2.11-1.1.0与python kafka和python 2.7

0 个答案:

没有答案