KAFKA0.10版本消费者示例

 之前公司一直使用0.8版本的kafka,前段时间客户现场使用0.10版本,因api有许多修改的地方,所以把0.10版本消费者代码记录一下。代码详见:

public class KafkaSource {
  
    public void start() {
        //获取topic名称
        String testTopic = "topic_name";
        //获取基本配置信息,zookeeper,kafka和消费者组
        String zookeeper = "192.168.168.168:2181";
        String consumerGroup = "consumer_group_name";
        String bootstrapServers = "192.168.168.168:9092";
        //获取kafka配置
        Properties properties = getKafkaProperties(zookeeper, consumerGroup, bootstrapServers);
        //0.10新特性
        TestConsumerRebalanceListener rebalanceListener = new TestConsumerRebalanceListener();
        //初始化consumer
        KafkaConsumer<Object, Map<String, Object>> testConsumer = new KafkaConsumer<>(properties);
        testConsumer.subscribe(Collections.singletonList(testTopic), rebalanceListener);
      
        //从kafka提取数据,放到对应queue中
        while (true) {
        ConsumerRecords<Object, Map<String, Object>> records = testConsumer.poll(1000);
        for (ConsumerRecord<Object, Map<String, Object>> record : records) {
            Map<String, Object> value = record.value();
            //value即为获取的数据,此处根据业务进行具体操作
              System.out.println("提取到的数据==========="+ value );
        }
        kafkaConsumer.commitSync();
        }
    }

    public void stop() {
        if (consumer != null) {
            consumer.stop();
        }
    }

    private static class TestConsumerRebalanceListener implements ConsumerRebalanceListener {
        @Override
        public void onPartitionsRevoked(Collection partitions) {
            System.out.println("Called onPartitionsRevoked with partitions:" + partitions);
        }

        @Override
        public void onPartitionsAssigned(Collection partitions) {
            System.out.println("Called onPartitionsAssigned with partitions:" + partitions);
        }
    }

    //---获取kafka配置,必须指定的参数,否则可能会报错,具体参考kafka0.10版api---
    private Properties getKafkaProperties(String zookeeper, String consumerGroup, String bootstrapServers) {
        if (StringUtils.isAnyBlank(zookeeper, consumerGroup)) {
            throw new RuntimeException("zookeeper and consumer group configuration can not be null");
        }
        Properties props = new Properties();
        props.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        //自定义的值反序列化类,无特殊需要可与key的配置相同
        props.put("value.deserializer", MapDecoder.class.getName());
        props.put("group.id", consumerGroup);
        //offset设置模式,earliest和lastest两种
        props.put("auto.offset.reset", "earliest");
        props.put("security.protocol", "PLAINTEXTSASL");
        props.put("bootstrap.servers", bootstrapServers);
        return props;
    }
}

以上内容仅供参考!

本文作者为竹子君,转发请注明出处!~

你可能感兴趣的:(KAFKA0.10版本消费者示例)