kafka offset查询、提交

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 ZooKeeper
			channel.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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值