Python 如何使用kafka.consumer.SimpleConsumer,seek()

Python 如何使用kafka.consumer.SimpleConsumer,seek(),python,apache-kafka,kafka-consumer-api,kafka-python,Python,Apache Kafka,Kafka Consumer Api,Kafka Python,API文档如下: 但是,当我运行以下代码时,例外情况是%d格式:需要数字,而不是NoneType client = KafkaClient("localhost:9092") consumer = SimpleConsumer(client, "test-group", "test") consumer.seek(0, whence=None)# (0,2) and (0,0) run = True while( run ): messag

API文档如下:

但是,当我运行以下代码时,例外情况是%d格式:需要数字,而不是NoneType

    client = KafkaClient("localhost:9092")
    consumer = SimpleConsumer(client, "test-group", "test")
    consumer.seek(0, whence=None)# (0,2) and (0,0)
    run = True
    while( run ):
        message = consumer.get_message(block=False, timeout=4000)

    except Exception as e:
        print "Exception while trying to read msg:", str(e)
当我使用下面的代码时,异常是seek()得到了一个意外的关键字参数“partition”

consumer.seek(0, whence=None, partition=None)# (0,2) and (0,0)

有什么想法吗?谢谢。

在《卡夫卡权威指南》中,有一个用Java编写的
seek()
示例代码(不是用Python编写的,但我希望您能理解其中的大意)

公共类SaveOffsetsOnRebalance实现ConsumerBalanceListener{
已撤销分区上的公共void(集合分区){
提交交易();
}
已签名的分区上的公共void(集合分区){
for(主题分区:分区)
seek(分区,getOffsetFromDB(分区));
}
}
}//这些括号与这本书完全相同。我没有改变任何事情。不过你可能会想。
consumer.subscribe(主题,新SaveOffsetOnRebalance(consumer));
消费者调查(0);
对于(TopicPartition分区:consumer.assignment())
seek(分区,getOffsetFromDB(分区));
while(true){
ConsumerRecords记录=consumer.poll(100);
对于(消费者记录:记录)
{ 
过程记录(记录);
storeRecordInDB(记录);
storeOffsetInDB(record.topic()、record.partition()、record.offset());
}
提交交易();
}