Java 为什么对卡夫卡的seektobegining和seekToEnd API的调用永远挂起?
我的环境:Java 为什么对卡夫卡的seektobegining和seekToEnd API的调用永远挂起?,java,apache-kafka,Java,Apache Kafka,我的环境: 卡夫卡版本:Kafka_2.10-0.10.0.0 卡夫卡Java API版本(客户端):Kafka-clients-0.10.0.0 我的配置: 主题:事件通知 分区:20 客户端消费者线程:1 消费者组id:事件\u cg01 自动提交标志:false 根据要求,在启动我的应用程序期间,根据标志,我必须将偏移设置为开始,或结束。为此,我使用以下代码: final List<PartitionInfo> partitionsInfos = kafkaConsum
- 卡夫卡版本:
Kafka_2.10-0.10.0.0
- 卡夫卡Java API版本(客户端):
Kafka-clients-0.10.0.0
- 主题:
事件通知
- 分区:
20
- 客户端消费者线程:
1
- 消费者组id:
事件\u cg01
- 自动提交标志:
false
final List<PartitionInfo> partitionsInfos = kafkaConsumer.partitionsFor(this.topic);
final List<TopicPartition> assignedPartitions = FluentIterable
.from(partitionsInfos)
.filter(Predicates.notNull())
.transform(new Function<PartitionInfo, TopicPartition>() {
@Override
public TopicPartition apply(final PartitionInfo input) {
return new TopicPartition(topic, input.partition());
}
}).toList();
switch (listenMode) {
case OLDEST:
kafkaConsumer.seekToBeginning(assignedPartitions);
break;
case LATEST:
kafkaConsumer.seekToEnd(assignedPartitions);
break;
default:
break;
}
final List partitionsInfos=kafkaConsumer.partitions for(this.topic);
最终列表assignedPartitions=FluentIterable
.从(分区)开始
.filter(谓词.notNull())
.transform(新函数(){
@凌驾
公共主题分区应用(最终分区信息输入){
返回新的TopicPartition(主题,input.partition());
}
}).toList();
开关(listenMode){
最古老的案例:
kafkaConsumer.Seektobegining(指定分区);
打破
最新个案:
kafkaConsumer.seekToEnd(指定分区);
打破
违约:
打破
}
此代码未按预期工作。它永远挂在seektobegining
和seekToEnd
调用上
我遗漏了什么吗 在您可以
seek()
之前,您首先需要subscribe()
订阅主题或assign()
将主题的分区分配给消费者。请注意,subscribe()
和assign()
都是惰性调用,因此,在使用seek()或seektobegining()
或seekToEnd()
之前,您还需要对poll()执行一个“伪调用”,然后才能使用seek()或seektoBegining()
或seekToEnd()
对poll()进行伪调用?这是一个零微秒的轮询调用吗?为什么需要虚拟调用?@JonathanMpoll(0)
对我不起作用,但poll(1)
对我起作用。至于原因,arg doc给出了一个提示:如果为0,则立即返回缓冲区中当前可用的所有记录,否则返回空代码>