Kafka 消费者API
2021-03-06 10:26
标签:reset led 保留 线程 关闭自动 apach value test res Kafka 消费者API 标签:reset led 保留 线程 关闭自动 apach value test res 原文地址:https://www.cnblogs.com/noyouth/p/12866005.html消费者api,自动提交offset
public class MyConsumer {
public static void main(String[] args) {
Properties props = new Properties();
//连接的集群
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop102:9092");
//开启自动提交(消费偏移量)
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,true);
//自动提交的延迟
props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,1000);
//KV的反序列化类
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
//消费者组
props.put(ConsumerConfig.GROUP_ID_CONFIG,"gc");
//消费者
KafkaConsumer
手动提交offset,同步提交
public class ConsumerOffsetSync {
public static void main(String[] args) {
Properties props = new Properties();
//连接的集群
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop102:9092");
//关闭自动提交(消费偏移量)
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,false);
//KV的反序列化类
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
//消费者组
props.put(ConsumerConfig.GROUP_ID_CONFIG,"gc1");
//重置offset。
//earliest:从头开始消费,触发的条件1,换组;条件2:保留的offset指向的数据已经不存在
//latest:默认值,消费最新的数据。
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
//消费者
KafkaConsumer
手动提交offset,异步提交
//异步提交
kafkaConsumer.commitAsync((offsets, exception) -> {
if (exception != null) {
System.err.println("Commit failed for" +
offsets);
}
});