kafka consumer 0.8.2.1範例程式碼,kafka0.8.2.1

來源:互聯網
上載者:User

kafka consumer 0.8.2.1範例程式碼,kafka0.8.2.1

package test_kafka;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.concurrent.atomic.AtomicInteger;import kafka.consumer.Consumer;import kafka.consumer.ConsumerConfig;import kafka.consumer.ConsumerIterator;import kafka.consumer.ConsumerTimeoutException;import kafka.consumer.KafkaStream;import kafka.javaapi.consumer.ConsumerConnector;public class KafkaConsumerApi {    public ConsumerConnector consumer;    private AtomicInteger sendCount;    public Properties initProperties(String broker, String groupId) {        Properties properties = new Properties();        properties.put("zookeeper.connect", broker);        properties.put("group.id", groupId);        properties.put("auto.offset.reset", "smallest");        properties.put("auto.commit.enable", "false");        properties.put("zookeeper.session.timeout.ms", "4000");        properties.put("zookeeper.sync.time.ms", "200");        properties.put("consumer.timeout.ms", "10000");        return properties;    }    public static void main(String[] args) throws Exception {        new KafkaConsumerApi().start();    }    public void start() {        String topic = "test_010_kafka";        String groupId = "test3";        Properties properties = initProperties("192.168.137.131:2181", groupId);        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();        topicCountMap.put(topic, new Integer(1));        ConsumerConfig config = new ConsumerConfig(properties);        sendCount = new AtomicInteger(0);        while (true) {            System.out.println("start to consumer Messages.");            List<byte[]> bytes = new ArrayList<byte[]>();            try {                consumer = Consumer.createJavaConsumerConnector(config);                Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer                        .createMessageStreams(topicCountMap);                KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0);                ConsumerIterator<byte[], byte[]> it = stream.iterator();                while (true) {                    try {                        while (it.hasNext()) {                            bytes.add(it.next().message());                            if (bytes.size() >= 8) {                                submitMessage(bytes);                            }                        }                    } catch (ConsumerTimeoutException e) {                        System.out.println("-------------------------ConsumerTimeoutException---------------------------");                        if (bytes.size() > 0) {                            submitMessage(bytes);                        }                    }                }            } catch (Exception e) {                e.printStackTrace();                sleepSeconds(60);            }        }    }    public void submitMessage(List<byte[]> bytes) {        int size = bytes.size();        if (exceute(bytes)) {            consumer.commitOffsets();            System.out.println("consumer message size:" + size + ", and consumer total count:" + sendCount.addAndGet(size));            bytes.clear();        }    }    public boolean exceute(List<byte[]> bytes) {        for (byte[] str : bytes) {            System.out.println(new String(str));        }        return true;    }    public void sleepSeconds(int seconds) {        try {            Thread.sleep(seconds * 1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.