Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取每个分区的当前最新偏移量,然后只消耗该偏移量?_Java_Apache Kafka_Kafka Consumer Api - Fatal编程技术网

Java 如何获取每个分区的当前最新偏移量,然后只消耗该偏移量?

Java 如何获取每个分区的当前最新偏移量,然后只消耗该偏移量?,java,apache-kafka,kafka-consumer-api,Java,Apache Kafka,Kafka Consumer Api,我试图在接收大量数据的主题中检查缺少的键。因为作业是按需运行的,所以它需要一些标准来知道何时搜索了它所关心的所有记录。我们确定这将是作业启动时每个分区的最新偏移量 我的问题首先是如何获取主题的所有分区信息,而不实际使用它(我需要使用它为每个分区创建单独的使用者,以跟踪它们的偏移量与最大偏移量) 其次,如何在消费者看到其已达到最大偏移量后停止消费 编辑: 我找到了一种获取分区的方法,那就是向单个消费者订阅主题,进行一次虚拟的轮询,然后为(…)使用分区。不确定这是否是“推荐”的方法。您可以使用con

我试图在接收大量数据的主题中检查缺少的键。因为作业是按需运行的,所以它需要一些标准来知道何时搜索了它所关心的所有记录。我们确定这将是作业启动时每个分区的最新偏移量

我的问题首先是如何获取主题的所有分区信息,而不实际使用它(我需要使用它为每个分区创建单独的使用者,以跟踪它们的偏移量与最大偏移量)

其次,如何在消费者看到其已达到最大偏移量后停止消费

编辑:
我找到了一种获取分区的方法,那就是向单个消费者订阅主题,进行一次虚拟的
轮询
,然后为(…)使用
分区。不确定这是否是“推荐”的方法。

您可以使用consumer.partitions for和consumer.endoffset获取分区和最后偏移量

 /*Get metadata about the partitions for a given topic. This method will issue a remote call to the server if it does  not already have any metadata about the given topic.*/ 
    public java.util.List<PartitionInfo> partitionsFor(java.lang.String topic)
/*获取给定主题的分区元数据。如果服务器上没有关于给定主题的任何元数据,则此方法将向服务器发出远程调用。*/
公共java.util.List分区(java.lang.String主题)
内偏移

/*Get the last offset for the given partitions. The last offset of a partition is the offset of the upcoming message, i.e. the offset of the last available message + 1*/
public java.util.Map<TopicPartition,java.lang.Long> endOffsets(java.util.Collection<TopicPartition> partitions)
/*获取给定分区的最后偏移量。分区的最后一个偏移量是即将到来的消息的偏移量,即最后一个可用消息的偏移量+1*/
公共java.util.Map内偏移集(java.util.Collection分区)

下面是示例代码

Properties consumerProperties = new Properties();
consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, "consumerid");
Consumer<String, byte[]> consumer = new KafkaConsumer<>(consumerProperties);    
List<PartitionInfo> parts = consumer.partitionsFor(topic);
consumer.assign(partitions);
Map<TopicPartition, Long> offsets = consumer.endOffsets(partitions);
for (TopicPartition tp : offsets.keySet()) {
    OffsetAndMetadata commitOffset = consumer.committed(new 
    TopicPartition(tp.topic(), tp.partition()));
    //Consumer offset for partition tp
    long offset=offsets.get(tp);
    //Consumed committed offset
    long consumedOffset=commitOffset.offset();
}
Properties consumerProperties=新属性();
consumerProperties.put(ConsumerConfig.BOOTSTRAP\u SERVERS\u CONFIG,“localhost:9092”);
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG,“consumerrid”);
消费者=新卡夫卡消费者(消费者财产);
列出部件=消费者。部件(主题);
消费者。分配(分区);
映射偏移=消费者内偏移(分区);
对于(TopicPartition tp:offsets.keySet()){
OffsetAndMetadata commitofset=消费者.committed(新)
主题分区(tp.topic(),tp.partition());
//分区tp的使用者偏移量
长偏移量=偏移量.get(tp);
//已消耗的已提交偏移量
long consumerdoffset=commitofset.offset();
}

您可以使用consumer.partitions for和consumer.endoffset获取分区和上次偏移量

 /*Get metadata about the partitions for a given topic. This method will issue a remote call to the server if it does  not already have any metadata about the given topic.*/ 
    public java.util.List<PartitionInfo> partitionsFor(java.lang.String topic)
/*获取给定主题的分区元数据。如果服务器上没有关于给定主题的任何元数据,则此方法将向服务器发出远程调用。*/
公共java.util.List分区(java.lang.String主题)
内偏移

/*Get the last offset for the given partitions. The last offset of a partition is the offset of the upcoming message, i.e. the offset of the last available message + 1*/
public java.util.Map<TopicPartition,java.lang.Long> endOffsets(java.util.Collection<TopicPartition> partitions)
/*获取给定分区的最后偏移量。分区的最后一个偏移量是即将到来的消息的偏移量,即最后一个可用消息的偏移量+1*/
公共java.util.Map内偏移集(java.util.Collection分区)

下面是示例代码

Properties consumerProperties = new Properties();
consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, "consumerid");
Consumer<String, byte[]> consumer = new KafkaConsumer<>(consumerProperties);    
List<PartitionInfo> parts = consumer.partitionsFor(topic);
consumer.assign(partitions);
Map<TopicPartition, Long> offsets = consumer.endOffsets(partitions);
for (TopicPartition tp : offsets.keySet()) {
    OffsetAndMetadata commitOffset = consumer.committed(new 
    TopicPartition(tp.topic(), tp.partition()));
    //Consumer offset for partition tp
    long offset=offsets.get(tp);
    //Consumed committed offset
    long consumedOffset=commitOffset.offset();
}
Properties consumerProperties=新属性();
consumerProperties.put(ConsumerConfig.BOOTSTRAP\u SERVERS\u CONFIG,“localhost:9092”);
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG,“consumerrid”);
消费者=新卡夫卡消费者(消费者财产);
列出部件=消费者。部件(主题);
消费者。分配(分区);
映射偏移=消费者内偏移(分区);
对于(TopicPartition tp:offsets.keySet()){
OffsetAndMetadata commitofset=消费者.committed(新)
主题分区(tp.topic(),tp.partition());
//分区tp的使用者偏移量
长偏移量=偏移量.get(tp);
//已消耗的已提交偏移量
long consumerdoffset=commitofset.offset();
}

当您消费时,您将收到新的消息。。。在轮询循环中,您可以检查迭代器是否为空,然后您已经到达末尾。在您消费时,您将收到新消息。。。在轮询循环中,您可以检查迭代器是否为空,然后就到达了末尾