kafka offset查询、提交
来源:互联网 发布:淘宝开店必须要交钱吗 编辑:程序博客网 时间:2024/06/04 05:02
wiki地址https://cwiki.apache.org/confluence/display/KAFKA/Committing+and+fetching+consumer+offsets+in+Kafka
public class KafkaOffset {private String group = "gaia";private int correlationId = 0;final String clientId = "demoClientId";public void getOffset() {BlockingChannel channel = new BlockingChannel("192.168.40.28", 9092, BlockingChannel.UseDefaultBufferSize(), BlockingChannel.UseDefaultBufferSize(), 5000 /* read timeout in millis */);channel.connect();List<TopicAndPartition> partitions = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7).stream().map(i -> new TopicAndPartition("article_basic_info", i)).collect(Collectors.toList());/* version */// version 1 and above fetch from Kafka, version 0 fetches from ZooKeeper,测试为0程序正常执行,为1程序执行没有任何效果OffsetFetchRequest fetchRequest = new OffsetFetchRequest(group, partitions, (short) 0, correlationId, clientId);try {channel.send(fetchRequest.underlying());OffsetFetchResponse fetchResponse = OffsetFetchResponse.readFrom(channel.receive().buffer());for (TopicAndPartition partition : partitions) {OffsetMetadataAndError result = fetchResponse.offsets().get(partition);short offsetFetchErrorCode = result.error();if (offsetFetchErrorCode == ErrorMapping.NotCoordinatorForConsumerCode()) {channel.disconnect();// Go to step 1 and retry the offset fetch} else {long offset = result.offset();System.out.println(String.format("offset->%s->%d", partition.partition(), offset));}}}finally {channel.disconnect();}}public void commitOffset() {BlockingChannel channel = new BlockingChannel("192.168.40.28", 9092, BlockingChannel.UseDefaultBufferSize(), BlockingChannel.UseDefaultBufferSize(), 5000 /* read timeout in millis */);channel.connect();try {long now = System.currentTimeMillis();Map<TopicAndPartition, OffsetAndMetadata> offsets = new LinkedHashMap<>();offsets.put(new TopicAndPartition("article_basic_info", 0), new OffsetAndMetadata(10L, "associated metadata", now));offsets.put(new TopicAndPartition("article_basic_info", 1), new OffsetAndMetadata(20L, "more metadata", now));OffsetCommitRequest commitRequest = new OffsetCommitRequest(group, offsets, correlationId++, clientId, (short) 0 /* version */); // version 1 and above commit to Kafka, version 0 commits to ZooKeeperchannel.send(commitRequest.underlying());OffsetCommitResponse commitResponse = OffsetCommitResponse.readFrom(channel.receive().buffer());System.out.println(String.format("提交->%s", commitResponse.hasError()));} finally {channel.disconnect();}}public static void main(String[] args) {KafkaOffset kafkaOffset = new KafkaOffset();kafkaOffset.getOffset();kafkaOffset.commitOffset();kafkaOffset.getOffset();}}
0 1
- kafka offset查询、提交
- 八.Kafka Consumer和 offset提交
- kafka多线程消费和手动提交offset,新版java api
- kafka监控Kafka Offset Monitor
- kafka offset判断
- Kafka-Offset-Monitor安装
- 重置kafka的offset
- Kafka Offset Storage
- Kafka Offset Storage
- kafka offset判断
- kafka-offset 存储方式
- kafka中的offset
- kafka auto.offset.reset介绍
- kafka 设置auto.offset.reset
- kafka 的offset的重置
- kafka的partition和offset
- kafka offset 新存储机制
- kafka 0.8 0.9 offset 问题
- C++编程入门系列之四十七(多态性:运算符重载为类的友元函数)
- 八大排序 -- 交换排序之【冒泡排序】
- SQL(十四)-连接表
- JFreeChart使用
- INFORMATION_SCHEMA.INNODB_LOCKS
- kafka offset查询、提交
- java学习笔记之单链表的插入和删除
- 编码问题
- Shell脚本IF条件判断和判断条件总结
- Ambari学习17_Amabri 2.1安装HDP2.3.2 之 七、自定义HDP服务
- 因果图方法
- Java 序列化
- Java 编程风格指南--Google
- Java基础-集合框架2 Collection和Iterator接口