如何重新消费kafka之 python脚本读取kafka

因为一次上线事故,导致kafka里的数据取出后被丢弃,于是写了一个直接消费kafka的脚本

python2.7+kafka0.8+zookeeper

直接上代码

from pykafka import KafkaClient
from pykafka.common import OffsetType

class NewK_Client(object):
    def __init__(self, topic, host):
        self.topic = topic
        self.client = KafkaClient(hosts=host)

    def HaddleConsum(self, zookeeper_connect):
        topic = self.client.topics[self.topic]
        consum = topic.get_balanced_consumer(
            # set consumer_group
            consumer_group='my_group',
            auto_commit_enable=True,
            zookeeper_connect=zookeeper_connect,
            reset_offset_on_start=True,
            # set offset
            auto_offset_reset=OffsetType.EARLIEST,
        )
        return consum

class NewStoreEvent(object):
    def __init__(self, event_type=(), start_time="", stop_time="", event_id=""):
        self.kafka_addr = CONF.get("kafka", "kafka_addr")
        self.kafka_topic = CONF.get("kafka", "kafka_topic")
        self.kafka_zp = CONF.get("kafka", "kafka_zp")

    def connection(self):
        kc = NewK_Client(self.kafka_topic, self.kafka_addr)
        consums = kc.HaddleConsum(self.kafka_zp)
        return consums

    def main(self):
        consums = self.connection()
        kfk_rds = kafka2redis()
        while 1:
            try:
                if consums is not None:
                    for data in consums:
                        try:
                            print(data)
                        except Exception as e:
                            LOG.error(e, exc_info=True)
            except Exception as e:
                LOG.error(e, exc_info=True)

关键在于 auto_offset_reset=OffsetType.EARLIEST这行配置,实现了从zookper记录的第一条(未过期的)开始读取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值