美文网首页爬不动的毛毛虫
从指定 offset 消费 Kafka

从指定 offset 消费 Kafka

作者: 焉知非鱼 | 来源:发表于2018-10-13 18:22 被阅读153次
    pip install kafka-python
    
    import gzip
    from kafka import KafkaConsumer
    from kafka import TopicPartition
    
    consumer = KafkaConsumer(bootstrap_servers='10.30.10.15:9092')
    partition = TopicPartition('dc-diagnostic-report', 0)
    start = 8833
    end = 8835
    consumer.assign([partition])
    consumer.seek(partition, start)
    
    i=start
    for msg in consumer:
        if msg.offset > end:
            break
        else:
            print msg
            try:
                outF = file( '/data/app/tar/' + str(i) + ".gz", 'wb')
                outF.write(msg.value)
                outF.close()
                i+=1
                print i
            except Exception,e:
                print e
    

    相关文章

      网友评论

        本文标题:从指定 offset 消费 Kafka

        本文链接:https://www.haomeiwen.com/subject/ettaaftx.html