Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import java.util.Properties;
Import Kafka.consumer.ConsumerConfig;
Import Kafka.consumer.ConsumerIterator;
Import Kafka.consumer.KafkaStream;
Import Kafka.javaapi.consumer.ConsumerConnector;
Import Kafka.serializer.StringDecoder;
Import kafka.utils.VerifiableProperties;
public class Kafkaconsumer {
Private final consumerconnector consumer;
Private Kafkaconsumer () {
Properties Props = new properties ();
Zookeeper configuration
Props.put ("Zookeeper.connect", "192.168.170.185:2181");
Consumer group
Props.put ("Group.id", "Testgroup");
ZK Connection timed out
Props.put ("zookeeper.session.timeout.ms", "4000");
Props.put ("zookeeper.sync.time.ms", "200");
Props.put ("auto.commit.interval.ms", "1000");
Props.put ("Auto.offset.reset", "smallest");
Serialization classes
Props.put ("Serializer.class", "Kafka.serializer.StringEncoder");
Consumerconfig config = new Consumerconfig (props);
Consumer = kafka.consumer.Consumer.createJavaConsumerConnector (config);
}
void consume () {
map<string, integer> topiccountmap = new hashmap<string, integer> ();
Topiccountmap.put (Kafkaproducer.topic, New Integer (1));
Stringdecoder Keydecoder = new Stringdecoder (new Verifiableproperties ());
Stringdecoder Valuedecoder = new Stringdecoder (new Verifiableproperties ());
Map<string, list<kafkastream<string, string>>> consumermap =
Consumer.createmessagestreams (Topiccountmap,keydecoder,valuedecoder);
kafkastream<string, string> stream = Consumermap.get (kafkaproducer.topic). Get (0);
Consumeriterator<string, string> it = Stream.iterator ();
int messagecount = 0;
while (It.hasnext ()) {
System.out.println (It.next (). message ());
messagecount++;
if (Messagecount = = 100) {
SYSTEM.OUT.PRINTLN ("Consumer end of the total consumption of" + Messagecount + "Message! ");
}
}
}
public static void Main (string[] args) {
New Kafkaconsumer (). consume ();
}
}
Kafka Java API Consumer