The environment of this article is as follows:
Operating system: CentOS 6 32-bit
JDK version: 1.8.0_77 32-bit
Kafka version: 0.9.0.1 (Scala 2.11)
1. Maven Dependency Package
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>0.9.0.1</version></dependency>
2. Producer Code
Packagecom. Lnho. Example. Kafka;import org. Apache. Kafka. Clients. Producer. Kafkaproducer;import org. Apache. Kafka. Clients. Producer. Producer;import org. Apache. Kafka. Clients. Producer. Producerrecord;Import Java. Util. Properties;public class Kafkaproducerexample {public static void main (string[] args) {Properties props = new Properties ();Props. Put("Bootstrap.servers","master:9092");Props. Put("ACKs","All");Props. Put("Retries",0);Props. Put("Batch.size",16384);Props. Put("linger.ms",1);Props. Put("Buffer.memory",33554432);Props. Put("Key.serializer","Org.apache.kafka.common.serialization.StringSerializer");Props. Put("Value.serializer","Org.apache.kafka.common.serialization.StringSerializer");producer<string, string> Producer = new kafkaproducer<> (props);for (int i =0; i <; i++)Producer. Send(New Producerrecord<> ("Topic1", Integer. toString(i), Integer. toString(i)));Producer. Close();}}
3. Consumer Code
Packagecom. Lnho. Example. Kafka;import org. Apache. Kafka. Clients. Consumer. Consumerrecord;import org. Apache. Kafka. Clients. Consumer. Consumerrecords;import org. Apache. Kafka. Clients. Consumer. Kafkaconsumer;Import Java. Util. Arrays;Import Java. Util. Properties;public class Kafkaconsumerexample {public static void main (string[] args) {Properties props = new Properties ();Props. Put("Bootstrap.servers","master:9092");Props. Put("Group.id","Test");Props. Put("Enable.auto.commit","true");Props. Put("auto.commit.interval.ms","+");Props. Put("session.timeout.ms","30000");Props. Put("Key.deserializer","Org.apache.kafka.common.serialization.StringDeserializer");Props. Put("Value.deserializer","Org.apache.kafka.common.serialization.StringDeserializer");kafkaconsumer<string, string> consumer = new Kafkaconsumer<> (props);Consumer. Subscribe(Arrays. Aslist("Topic1"));while (true) {consumerrecords<string, string> records = Consumer. Poll( -);For (consumerrecord<string, string> record:records) System. out. printf("offset =%d, key =%s, value =%s\n", record. Offset(), Record. Key(), Record. Value());} }}
4. Execution procedures
Under Lib you need to have: kafka-clients-0.9.0.1.jar log4j-1.2.17.jar slf4j-api-1.7.6.jar slf4j-log4j12-1.7.6.jar
Producers:
-classpath kafka-example-1.0-SNAPSHOT.jar:lib/* com.lnho.example.kafka.KafkaProducerExample
Consumers:
-classpath kafka-example-1.0-SNAPSHOT.jar:lib/* com.lnho.example.kafka.KafkaConsumerExample
Kafka Access using a Java client