CheckOffsetAndCommit.java
public class CheckOffsetAndCommit {
private static final String brokerList = "192.168.131.130:9092";
private static final String topic = "heilu";
private static final String groupId = "group.demo";
public static Properties initConfig() {
Properties properties = new Properties();
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
// 设置值的 反序列化器
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
properties.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
// 关闭自动提交
properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
return properties;
}
public static void main(String[] args) {
Properties properties = initConfig();
KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(properties);
TopicPartition tp = new TopicPartition(topic, 0);
consumer.assign(Arrays.asList(tp));
long lastConsumedOffset = -1;
while (true){
ConsumerRecords<String, String> records = consumer.poll(1000);
if (records.isEmpty()){
break;
}
List<ConsumerRecord<String, String>> partitonRecords = records.records(tp);
lastConsumedOffset = partitonRecords.get(partitonRecords.size()-1).offset();
consumer.commitSync(); // 同步提交消费位移
}
System.out.println("consumed offset is " + lastConsumedOffset);
OffsetAndMetadata offsetAndMetadata = consumer.committed(tp);
System.out.println("commited offset is " + offsetAndMetadata.offset());
long position = consumer.position(tp);
System.out.println("the offset of the next record is " + position);
}
}