kafka learning note 2

2020-03-08  本文已影响0人  简为2016

Consumer Group

rebalancd consumers

import java.util.Properties;
import java.util.Arrays;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.ConsumerRecord;

public class ConsumerGroup{
        public static void main(String[] args)throws Exception{
                if(args.length < 2){
                        System.out.println("Usage: consumer <topic> <groupname>");
                        return;
                }
                String topic = args[0].toString();
                String group = args[1].toString();
                Properties props = new Properties();
                props.put("bootstrap.servers","10.131.18.138:9092");
                props.put("group.id",group);
                props.put("enable.auto.commit","true");
                props.put("auto.commit.interval.ms","1000");
                props.put("session.timeout.ms","30000");
                props.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
                props.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserilaizer");
                KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
                consumer.subscribe(Arrays.asList(topic));
                System.out.println("Subscribed to topic");
                for(String item: Arrays.asList(topic)){
                        System.out.println(item);
                }
                while(true){
                        ConsumerRecords<String, String> records = consumer.poll(100);
                        for(ConsumerRecord<String, String> record : records){
                                System.out.printf("offset = %d, key = %s value = %s\n", record.offset(), record.key(), record.value());
                        }
                }
        }
}
上一篇下一篇

猜你喜欢

热点阅读