Using Java to create Kafka producers and consumers

Source: Internet
Author: User
Tags zookeeper

Create a Kafka theme, connect to the ZK cluster, copy factor 3, partition 3, subject name is test111
[Email protected] kafka]# bin/kafka-topics.sh--create--zookeeper h5:2181--topic test111--replication-factor 3--part Itions 3
View Kafka's topic details
[Email protected] kafka]# bin/kafka-topics.sh--describe--zookeeper h5:2181--topic test111
See All topics in Kafka
[Email protected] kafka]# bin/kafka-topics.sh--list--zookeeper h5:2181
Add the following dependent jar
Kafka_2.10-0.8.2.0.jar
Kafka-clients-0.8.2.0.jar
Metrics-core-2.2.0.jar
Scala-library-2.10.4.jar
Zkclient-0.3.jar
Zookeeper-3.4.6.jar

1. Producers
Package Storm.test.kafka;

Import java.util.Properties;

Import Kafka.javaapi.producer.Producer;
Import Kafka.producer.KeyedMessage;
Import Kafka.producer.ProducerConfig;
Import Kafka.serializer.StringEncoder;

public class Testproducer {

public static void Main (string[] args) throws Exception {
Properties prop = new properties ();
Prop.put ("Zookeeper.connect", "h5:2181,h6:2181,h7:2181");
Prop.put ("Metadata.broker.list", "h5:9092,h6:9092,h7:9092");
Prop.put ("Serializer.class", StringEncoder.class.getName ());
producer<string, string> Producer = new producer<string, string> (new Producerconfig (prop));
int i = 0;
while (true) {
Producer.send (New keyedmessage<string, string> ("test111", "msg:" +i++));
Thread.Sleep (1000);
}
}

}
2. Consumers
Package Storm.test.kafka;

Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import java.util.Properties;

Import Kafka.consumer.Consumer;
Import Kafka.consumer.ConsumerConfig;
Import Kafka.consumer.ConsumerIterator;
Import Kafka.consumer.KafkaStream;
Import Kafka.javaapi.consumer.ConsumerConnector;
Import Kafka.serializer.StringEncoder;

public class Testconsumer {

Static final String topic = "test111";

public static void Main (string[] args) {
Properties prop = new properties ();
Prop.put ("Zookeeper.connect", "h5:2181,h6:2181,h7:2181");
Prop.put ("Serializer.class", StringEncoder.class.getName ());
Prop.put ("Metadata.broker.list", "h5:9092,h6:9092,h7:9092");
Prop.put ("Group.id", "group1");
Consumerconnector consumer = consumer.createjavaconsumerconnector (new Consumerconfig (prop));
map<string, integer> topiccountmap = new hashmap<string, integer> ();
Topiccountmap.put (topic, 1);
Map<string, list<kafkastream<byte[], byte[]>>> messagestreams = Consumer.createmessagestreams ( TOPICCOUNTMAP);
Final kafkastream<byte[], byte[]> kafkastream = messagestreams.get (topic). get (0);
Consumeriterator<byte[], byte[]> iterator = Kafkastream.iterator ();
while (Iterator.hasnext ()) {
String msg = new String (Iterator.next (). message ());
System.out.println ("received message:" +msg);
}
}

}

Using Java to create Kafka producers and consumers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.