MCPcopy Create free account
hub / github.com/apache/kafka / Consumer

Class Consumer

examples/src/main/java/kafka/examples/Consumer.java:30–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29
30public class Consumer extends Thread
31{
32 private final ConsumerConnector consumer;
33 private final String topic;
34
35 public Consumer(String topic)
36 {
37 consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
38 createConsumerConfig());
39 this.topic = topic;
40 }
41
42 private static ConsumerConfig createConsumerConfig()
43 {
44 Properties props = new Properties();
45 props.put("zookeeper.connect", KafkaProperties.zkConnect);
46 props.put("group.id", KafkaProperties.groupId);
47 props.put("zookeeper.session.timeout.ms", "400");
48 props.put("zookeeper.sync.time.ms", "200");
49 props.put("auto.commit.interval.ms", "1000");
50
51 return new ConsumerConfig(props);
52
53 }
54
55 public void run() {
56 Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
57 topicCountMap.put(topic, new Integer(1));
58 Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
59 KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0);
60 ConsumerIterator<byte[], byte[]> it = stream.iterator();
61 while(it.hasNext())
62 System.out.println(new String(it.next().message()));
63 }
64}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected