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
Delete a Kafka specified topic
[Email protected] kafka]# bin/kafka-topics.sh--delete--zookeeper h5:2181,h6:2181,h7:2181--topic test111
Prompt if deleted
Topic guowang1 is marked for deletion.
Note:this would have no impact if delete.topic.enable are not set to true.
Please modify kafka/config/server.properties new delete.topic.enable=true
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

 1 package Storm.test.kafka; 2 3 Import java.util.Properties; 4 5 Import Kafka.javaapi.producer.Producer; 6 Import Kafka.producer.KeyedMessage; 7 Import Kafka.producer.ProducerConfig; 8 Import Kafka.serializer.StringEncoder;                 9 public class Testproducer {one-to-ten public static void Main (string[] args) throws Exception {13 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.cl ", StringEncoder.class.getName ()); producer<string, string> Producer = new producer<string, String> (new Producerconfig (prop)); int i = 0;19 while (true) {PR    Oducer.send (New keyedmessage<string, string> ("test111", "msg:" +i++)); Thread.Sleep (1000); 22             }23}24 25} 


2. Consumers

 1 package Storm.test.kafka; 2 3 Import Java.util.HashMap; 4 Import java.util.List; 5 Import Java.util.Map; 6 Import java.util.Properties; 7 8 Import Kafka.consumer.Consumer; 9 Import kafka.consumer.consumerconfig;10 Import kafka.consumer.consumeriterator;11 import kafka.c Onsumer.  KAFKASTREAM;12 Import kafka.javaapi.consumer.consumerconnector;13 Import kafka.serializer.stringencoder;14             public class Testconsumer {18 static final String topic = "test111"; 19 public static void Main (string[] args) {Properties prop = new properties (); PROP.P UT ("Zookeeper.connect", "h5:2181,h6:2181,h7:2181"), Prop.put ("Serializer.class", StringEncoder.class.get Name ()), Prop.put ("Metadata.broker.list", "h5:9092,h6:9092,h7:9092"), and Prop.put ("group"). ID "," group1 "); ConsumercOnnector consumer = consumer.createjavaconsumerconnector (new Consumerconfig (prop)); Map<string, Integ                 er> Topiccountmap = new hashmap<string, integer> (); topiccountmap.put (topic, 1); 28 Map<string, list<kafkastream<byte[], byte[]>>> messagestreams = Consumer.createmessagestreams ( TOPICCOUNTMAP); final kafkastream<byte[], byte[]> kafkastream = messagestreams.get (topic). get (0); 3 0 consumeriterator<byte[], byte[]> iterator = Kafkastream.iterator (), while (Itera Tor.hasnext ()) {string msg = new String (Iterator.next (). message ()); System.ou T.println ("received message:" +msg); 34}35}36 37}

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.