3

The API doc is here:http://kafka-python.readthedocs.org/en/latest/apidoc/kafka.consumer.html

But when I run the following code, the exception is %d format: a number is required, not 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)

When I used the following piece of code, the exception is seek() got an unexpected keyword argument 'partition'

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

Any idea? Thanks.

BAE
  • 8,550
  • 22
  • 88
  • 171

1 Answers1

0

In the Kafka Definitive Guide, there is a sample code of seek() written in Java (not in Python, but I hope you might get the general idea).

public class SaveOffsetsOnRebalance implements ConsumerRebalanceListener {

         public void onPartitionsRevoked (Collection <TopicPartition> partitions) {
                  commitDBTransaction();
         }

         public void onPartitionsAssigned(Collection <TopicPartiton> partitions) {
              for(TopicPartition partition : partitions)
                  consumer.seek(partition, getOffsetFromDB(partition));
         }

     }
}   // these brackets are exactly the same as the book. I didn't change anything. You might want to though.    

   consumer.subscribe (topics, new SaveOffsetOnRebalance(consumer));
   consumer.poll(0);

   for ( TopicPartition partition : consumer.assignment())
       consumer.seek(partition, getOffsetFromDB(partition));

   while (true) {
         ConsumerRecords <String, String> records = consumer.poll(100);
         for (ConsumerRecord <String, String> record : records)
         { 
               processRecord(record);
               storeRecordInDB(record);
               storeOffsetInDB(record.topic(), record.partition(), record.offset());
         }
         commitDBTransaction();
   }
Jin Lee
  • 3,194
  • 12
  • 46
  • 86