Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import kafka.consumer.ConsumerIterator;
- import kafka.consumer.KafkaStream;
- import kafka.javaapi.consumer.ConsumerConnector;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- public class Consumer {
- public static void main(String[] args) throws InterruptedException {
- final Properties props = new Properties();
- props.put("zookeeper.connect", "192.168.33.2:2181,192.168.33.11:2181,192.168.33.3:2181");
- props.put("group.id", "12345"); //required
- final kafka.consumer.ConsumerConfig consumerConfig = new kafka.consumer.ConsumerConfig(props);
- final ConsumerConnector consumerConnector = kafka.consumer.Consumer.createJavaConsumerConnector(consumerConfig);
- Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
- topicCountMap.put("receive.queue", new Integer(1));
- final Map<String, List<KafkaStream<byte[], byte[]>>> topicMessageStreams = consumerConnector.createMessageStreams(topicCountMap);
- final List<KafkaStream<byte[], byte[]>> streams = topicMessageStreams.get("receive.queue");
- ConsumerIterator<byte[], byte[]> it = streams.get(0).iterator();
- while (it.hasNext()) {
- System.out.println("Message " + new String(it.next().message()));
- Thread.sleep(3000);
- }
- System.out.println("done");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment