获取Kafka每个分区最新Offset的几种方法
脚本方法
./bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test
test:0:1522
test:1:1020
test:2:1258
和Java程序比起来,运行得有点慢。
Java 程序
更详细的代码工程,可以参考我的GitHub 消费者获取分区列表,并获取分区最新的OFFSET
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.LongDeserializer;
import org.apache.kafka.common.serialization.StringDeserializer;
// import kafka.api.OffsetRequest;
public class KafkaConsumerDemo {
private final static String TOPIC = "test";
private final static String BOOTSTRAP_SERVERS = "localhost:9092";
private static Consumer<Long, String> createConsumer() {
final Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS);
props.put(ConsumerConfig.GROUP_ID_CONFIG, "KafkaExampleConsumer");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, LongDeserializer.class.getName());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
final Consumer<Long, String> consumer = new KafkaConsumer<>(props);
return consumer;
}
// 获取某个Topic的所有分区以及分区最新的Offset
public static void getPartitionsForTopic() {
final Consumer<Long, String> consumer = createConsumer();
Collection<PartitionInfo> partitionInfos = consumer.partitionsFor(TOPIC);
System.out.println("Get the partition info as below:");
List<TopicPartition> tp =new ArrayList<TopicPartition>();
partitionInfos.forEach(str -> {
System.out.println("Partition Info:");
System.out.println(str);
tp.add(new TopicPartition(TOPIC,str.partition()));
consumer.assign(tp);
consumer.seekToEnd(tp);
System.out.println("Partition " + str.partition() + " 's latest offset is '" + consumer.position(new TopicPartition(TOPIC, str.partition())));
});
}
// 持续不断的消费数据
public static void run() throws InterruptedException {
final Consumer<Long, String> consumer = createConsumer();
consumer.subscribe(Collections.singletonList(TOPIC));
final int giveUp = 100; int noRecordsCount = 0;
while(true){
final ConsumerRecords<Long, String> consumerRecords = consumer.poll(1000);
if(consumerRecords.count()==0){
noRecordsCount++;
if(noRecordsCount > giveUp) break;
else continue;
}
// int i = 0;
consumerRecords.forEach(record -> {
// i = i + 1;
System.out.printf("Consumer Record:(%d, %s, %d, %d)\n",
record.key(), record.value(),
record.partition(), record.offset());
});
// System.out.println("Consumer Records " + i);
consumer.commitAsync();
}
consumer.close();
System.out.println("Kafka Consumer Exited");
}
}

参考资料
- How can I get the LATEST offset of a kafka topic?
- Kafka 源码解析之 Consumer Poll 模型(七)
- Case Study to Understand Kafka Consumer and Its Offsets
获取Kafka每个分区最新Offset的几种方法的更多相关文章
- PHP获取MySql新增记录ID值的3种方法
From: http://www.jb51.net/article/51473.htm 这篇文章主要介绍了PHP获取MySql新增记录ID值的3种方法,一般使用PHP自带函数mysql_insert_ ...
- python获取字母在字母表对应位置的几种方法及性能对比较
python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...
- javascript获取json对象的key名称的两种方法
javascript获取json对象的key名称的两种方法 数据处理中,你可能接收到一个不确定内容格式的json对象,然后要把key的值提取出来.今天试过两种可以提取json key的方法,均可以正常 ...
- VS编程,WPF中,获取鼠标相对于当前屏幕坐标的一种方法
原文:VS编程,WPF中,获取鼠标相对于当前屏幕坐标的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/det ...
- Java代码中获取配置文件(config.properties)中内容的两种方法
方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...
- 使用java传参调用exe并且获取程序进度和返回结果的一种方法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 在某个项目中需要考虑使用java后台调用由C#编写的切图程序( ...
- 获取JAVA[WEB]项目相关路径的几种方法
在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getPro ...
- Struts2 在Action中获取request、session、servletContext的三种方法
首页message.jsp: <body> ${requestScope.req }<br/> ${applicationScope.app }<br/> ${se ...
- 获取Map的key和value的两种方法
//使用迭代器,获取key; /*Iterator<String> iter = map.keySet().iterator(); while(iter.hasNext()){ Strin ...
随机推荐
- IT兄弟连 Java语法教程 流程控制语句 循环结构语句3
while循环 Java中的另外一种循环是while循环.while循环的语法格式如下: while(条件表达式){ 循环体; } 其中条件表达式定义了控制循环的条件,可以使任何有效的boolean表 ...
- HTML5新特性——自定义滑动条(input[type="range"])
HTML 4.01 与 HTML5之间的差异 以下 input 的 type属性值是 HTML5 中新增的: color.date.datetime.datetime-local.month.week ...
- 使用vue组件需要注意的4个细节
细节1:table(表格)中直接引用自定义组件出现的bug 如上图,tr本应在tbody中面,现在却是同级.造成的原因是h5规定table里必须有tbody,tbody中必须有tr, 当tbody中引 ...
- pandas.read_sql_query()读取数据库数据用chunksize的坑
最近一项工作需要读取数据库中1500万条数据,考虑到数据量太大,不方便直接一次性读取,不然会内存爆炸.想到用pandas.read_sql_query()里有一个chunksize可以分批返回chun ...
- 多线程学习二:线程池 ExecutorService
创建线程池的2种方式: 使用线程池方式1--Runnable接口: 通常,线程池都是通过线程池工厂创建,再调用线程池中的方法获取线程,再通过线程去执行任务方法. Executors:线程池创建工厂类: ...
- JS基础语法---函数练习part1---5个练习
练习1:求两个数字的和:获取任意的两个数字的和 function getSum(x, y) { return x + y; } console.log(getSum(10, 20)); 练习2:求1- ...
- vue学习笔记(三): 启动说明
1.启动页面:index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- HTML中html元素的lang属性的说明
HTML中html元素的lang属性的说明 我在刚开始学习HTML的时候,关于基本的HTML格式中有一点不明白的地方,基本格式如下 <!DOCTYPE html> <html lan ...
- Runtime 类初探
Runtime类 认识 Runtime类 在每一个JVM进程中都会存在一个Runtime类,这个类的主要功能是取得一些与运行时有关的环境属性或创建进程等操作. 在Runtime类定义的时候,它的构造方 ...
- Java 方法引用_特性
JAVA8 方法引用:(四种方法引用的使用) 对象引用的特点:不同的对象可以操作同一块的内容:而方法引用就是指为一个方法设置别名,相当于一个方法定义了不同的名字. 引用静态方法: 类名称 :: sta ...