Kafka java Production and Consumption program demo, kafkademo
Kafka is a message system with a huge throughput. It is written in scala and is different from the production and consumption of common messages. It has written a demo program for your reference. For more information about kafka installation, see the official documentation.
First, we need to create a maven project and then reference the kafka jar package in pom. Reference dependencies are as follows:
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.10</artifactId> <version>0.8.0</version> </dependency>
The version we use is 0.8. Let's take a look at the code for producing messages:
Package cn. outofmemory. kafka; import java. util. properties; import kafka. java API. producer. producer; import kafka. producer. keyedMessage; import kafka. producer. producerConfig;/*** Hello world! **/Public class KafkaProducer {private final Producer <String, String> producer; public final static String TOPIC = "TEST-TOPIC"; private KafkaProducer () {Properties props = new Properties (); // The props of the kafka port is configured here. put ("metadata. broker. list "," 192.168.193.148: 9092 "); // configure the serialization class props of value. put ("serializer. class "," kafka. serializer. stringEncoder "); // configure the serialization class props of the key. put ("key. serializer. class "," kafka. serializer. stringEncoder "); // request. required. acks // 0, which means that the producer never waits for an acknowledgement from the broker (the same behavior as 0.7 ). this option provides the lowest latency but the weakest durability guarantees (some data will be lost when a server fails ). // 1, which means that the producer gets an acknowledgement after the leader replica has ed the data. this option provides better durability as the client waits until the server acknowledges the request as successful (only messages that were written to the now-dead leader but not yet replicated will be lost ). //-1, which means that the producer gets an acknowledgement after all in-sync replicas have received the data. this option provides the best durability, we guarantee that no messages will be lost as long as at least one in sync replica remains. props. put ("request. required. acks ","-1 "); producer = new Producer <String, String> (new ProducerConfig (props);} void produce () {int messageNo = 1000; final int COUNT = 10000; while (messageNo <COUNT) {String key = String. valueOf (messageNo); String data = "hello kafka message" + key; producer. send (new KeyedMessage <String, String> (TOPIC, key, data); System. out. println (data); messageNo ++;} public static void main (String [] args) {new KafkaProducer (). produce ();}}
The following is the consumption code implementation:
Package cn. outofmemory. kafka; 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. java API. consumer. consumerConnector; import kafka. serializer. stringDecoder; import kafka. utils. verifiableProperties; public class KafkaConsumer {private final consumerconneconsumer; private KafkaConsumer () {Properties props = new Properties (); // zookeeper configures props. put ("zookeeper. connect "," 192.168.193.148: 2181 "); // group represents a consumer group props. put ("group. id "," jd-group "); // zk connection timeout 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 class 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 (); while (it. hasNext () System. out. println (it. next (). message ();} public static void main (String [] args) {new KafkaConsumer (). consume ();}}
Note that the consumer must be configured with the zk address, while the production end is configured with the kafka ip address and port.
Source Code address acquisition:Mingli
If you are interested, you can go to the ball ~ Sharing learning technologies: 2042849237