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