• kafka 0.10.2 消息消费者


    package cn.xiaojf.kafka.consumer;
    
    import org.apache.kafka.clients.consumer.ConsumerConfig;
    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.apache.kafka.clients.consumer.ConsumerRecords;
    import org.apache.kafka.clients.consumer.KafkaConsumer;
    import org.apache.kafka.common.serialization.StringDeserializer;
    import sun.applet.Main;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Properties;
    
    /**
     * 消息消费者
     * @author xiaojf 2017/3/22 15:50
     */
    public class MsgConsumer {
        private static final String GROUP = "MsgConsumer";
        private static final List TOPICS = Arrays.asList("my-replicated-topic");
    
        /**
         * 自动提交offset
         */
        public void autoCommit() {
            Properties properties = new Properties();
            properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getCanonicalName());//key反序列化方式
            properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getCanonicalName());//value反系列化方式
            properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,true);//自动提交
            properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.59.130:9092,192.168.59.131:9092,192.168.59.132:9092");//指定broker地址,来找到group的coordinator
            properties.put(ConsumerConfig.GROUP_ID_CONFIG,this.GROUP);//指定用户组
    
            KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(properties);
            consumer.subscribe(TOPICS);
    
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(100);//100ms 拉取一次数据
                for (ConsumerRecord<String, String> record : records) {
                    System.out.println("topic: "+record.topic() + " key: " + record.key() + " value: " + record.value() + " partition: "+ record.partition());
                }
            }
        }
    
        /**
         * 手动提交offset
         */
        public void consumer() {
            Properties properties = new Properties();
            properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getCanonicalName());//key反序列化方式
            properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getCanonicalName());//value反系列化方式
            properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,false);//手动提交
            properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.59.130:9092,192.168.59.131:9092,192.168.59.132:9092");//指定broker地址,来找到group的coordinator
            properties.put(ConsumerConfig.GROUP_ID_CONFIG,this.GROUP);//指定用户组
    
            KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(properties);
            consumer.subscribe(TOPICS);//指定topic消费
    
            long i = 0;
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(100);//100ms 拉取一次数据
                for (ConsumerRecord<String, String> record : records) {
                    System.out.println("topic: "+record.topic() + " key: " + record.key() + " value: " + record.value() + " partition: "+ record.partition());
                    i ++;
                }
    
                if (i >= 100) {
                    consumer.commitAsync();//手动commit
                    i = 0;
                }
            }
        }
    
        public static void main(String[] args) {
            new MsgConsumer().autoCommit();
        }
    }
    <dependency>
          <groupId>org.apache.kafka</groupId>
          <artifactId>kafka-clients</artifactId>
          <version>0.10.2.0</version>
        </dependency>
  • 相关阅读:
    波特率原理【转】
    求助大神!怎样批量删除数据库表中某个字段中同样的一段字符!
    1033. To Fill or Not to Fill (25)
    http协议
    【数据结构与算法】二叉树深度遍历(递归)
    2015届求职经历
    Codeforces Round #245 (Div. 1)——Working out
    泛泰A900 刷4.4专用中文TWRP2.7.1.1版 支持自己主动识别手机版本号(全球首创)
    实现简答LinkedList
    oracle的内存管理(之中的一个)
  • 原文地址:https://www.cnblogs.com/xiaojf/p/6602705.html
Copyright © 2020-2023  润新知