Kafka Consumer Assignment returns Empty Set

rookie :

I have subscribed to a Kafka topic as below. I need to run some logic only after the consumer has been assigned a partition.

However consumer.assignment() comes back as an empty set no matter how long I wait. If I do not have the while loop and then do a consumer.poll() I do get the records from the topic.Can any one tell me why this is happening ?

    consumer.subscribe(topics);
    Set<TopicPartition> assigned=Collections.emptySet();
    while(isAssigned) {
          assigned = consumer.assignment();
          if(!assigned.isEmpty()) {
              isAssigned= false;
          }
    }

//consumer props
Properties props = new Properties();
        props.put("bootstrap.servers", "xxx:9092,yyy:9092");
        props.put("group.id", groupId);
        props.put("enable.auto.commit", "false");
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "io.confluent.kafka.serializers.KafkaAvroDeserializer");
        props.put("schema.registry.url", "http://xxx:8081");
        props.put(KafkaAvroDeserializerConfig.SPECIFIC_AVRO_READER_CONFIG, true);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        props.put("max.poll.records", "100");
Mickael Maison :

Until you call poll(), the consumer is just idling.

Only after poll() is invoked, it will initiate a connection to the cluster, get assigned partitions and attempt to fetch messages.

This is mentioned in the Javadoc:

After subscribing to a set of topics, the consumer will automatically join the group when poll(Duration) is invoked.

Once you start calling poll(), you can use assignment() or even register a ConsumerRebalanceListener to be notify when partitions are assigned or revoked from your consumer instance.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=148077&siteId=1