简体   繁体   English

KafkaConsumer assignment()返回空

[英]KafkaConsumer assignment() returns empty

I am using 我在用

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>2.0.1</version>
</dependency>

The following piece of codes returns the non-empty assigned partitions, but poll(0) deprecated. 以下代码返回非空的已分配分区,但不推荐使用poll(0)

val records = kafkaConsumer.poll(0) // <= deprecated
logInfo(s"Dummy call ${records.count()}")

val partitions = kafkaConsumer.assignment()
logInfo(s"partitions=${partitions}")

The following return empty partitions: 以下返回分区:

val records = kafkaConsumer.poll(Duration.ofMillis(0)) // <= not working
logInfo(s"Dummy call ${records.count()}")

val partitions = kafkaConsumer.assignment()
logInfo(s"partitions=${partitions}")

Why? 为什么? Any ideas? 有任何想法吗? Thanks 谢谢

The difference of these two calls is the way the metadata is fetched. 这两个调用的区别在于获取元数据的方式。 The deprecated poll waits indefinitely until the metadata is retrieved successfully, whereas the other poll only tries once, often failing to connect to the coordinator within a very shot time interval(0 for your case), and returns with nothing useful. 不推荐使用的poll会无限期地等待,直到成功检索元数据,而另一个poll只会尝试一次,通常无法在非常短的时间间隔内连接到协调器(对于您的情况为0),并且返回时没有任何用处。 That's why you see an empty assignment after calling poll(Duration.ofMillis(0)) once. 这就是为什么你在调用poll(Duration.ofMillis(0))一次之后看到一个空的赋值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM