First, import the package to create the jar bundle in the Libs in the Kafka directory using Maven
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients< /artifactid>
<version>0.10.0.0</version>
</dependency>
write the properties configuration file.
For the project structure
#kafka集群地址
bootstrap.servers = 192.168.222.131:9092,192.168.222.130:9092,192.168.222.132:9092,192.168.222.133:9092
client.id = testProducer
Key.serializer = Org.apache.kafka.common.serialization.IntegerSerializer
Value.serializer = Org.apache.kafka.common.serialization.StringSerializer
and then the code
Package kafka.producer;
Import java.io.IOException;
Import java.util.Properties;
Import java.util.concurrent.ExecutionException;
Import Org.apache.kafka.clients.producer.Callback;
Import Org.apache.kafka.clients.producer.KafkaProducer;
Import Org.apache.kafka.clients.producer.ProducerRecord;
Import Org.apache.kafka.clients.producer.RecordMetadata;
public class Producertest extends thread{private final Kafkaproducer<integer, string> producer;
Private final String topic;
Private final Boolean IsAsync; /*isasync synchronous, asynchronous/Public producertest (String topic, Boolean isasync) {Properties Properties = new Prop
Erties (); /* Load configuration File/try {properties.load (ProducerTest.class.getClassLoader). getResourceAsStream ("Conf/kafka.producer.
Properties "));
catch (IOException e) {e.printstacktrace ();
} producer = new Kafkaproducer<> (properties); This.topic = TopiC
This.isasync = IsAsync;
public void Run () {int messageno = 1;
while (true) {String messagestr = ' message_ ' + Messageno;
Long starttime = System.currenttimemillis ();
if (IsAsync) {//Send asynchronously Producer.send (new producerrecord<> (topic,
Messageno, Messagestr), New Democallback (StartTime, Messageno, messagestr)); else {//Send synchronously try {producer.send (new producerrecord<&
gt; (topic, Messageno, MESSAGESTR)). get ();
SYSTEM.OUT.PRINTLN ("Sent message: (" + Messageno + "," + Messagestr + ")"); catch (Interruptedexception |
Executionexception e) {e.printstacktrace ();
}
} ++messageno;
}} class Democallback implements Callback {private final long starttime;
private final int key;
Private final String message;
Public Democallback (long starttime, int key, String message) {this.starttime = StartTime;
This.key = key;
this.message = message; /** * @param metadata the metadata for the record, that is sent (i.e. the partition and offset).
Null If an error * occurred. * @param exception the exception thrown during processing of this record.
Null if no error occurred. */public void oncompletion (Recordmetadata metadata, Exception Exception) {Long elapsedtime = System.
Currenttimemillis ()-starttime; if (metadata!= null) {System.out.println ("+ Key +", "+ Message +") Sen T to partition ("+ MetadAta.partition () + ")," + "offset (" + metadata.offset () + ") in" + Elapse
DTime + "MS");
else {exception.printstacktrace ();
}
}
}
Test code
Package kafka.producer;
public class Main {public
static void Main (string[] args) {
producertest test = new Producertest ("TP_HW", true); C3/>test.start ();
}
Run Results
Message (51215, message_51215) sent to partition (0), offset (161830) in 3497 MS Message (51216, message_51216) sent to Partit Ion (0), offset (161831) in 3497 MS Message (51217, message_51217) sent to partition (0), offset (161832) in 3497 MS Message (51 218, message_51218) sent to partition (0), offset (161833) in 3497 MS Message (51219, message_51219) sent to partition (0), of Fset (161834) in 3497 MS Message (51220, message_51220) sent to partition (0), offset (161835) in 3497 MS Message (51221, Messa ge_51221) sent to partition (0), offset (161836) in 3497 MS Message (51222, message_51222) sent to partition (0), offset (16183 7 in 3497 MS Message (51223, message_51223) sent to partition (0), offset (161838) in 3497 MS Message (51224, message_51224) Sent to partition (0), offset (161839) in 3497 MS Message (51225, message_51225) sent to partition (0), offset (161840) in 3497 MS Message (51226, message_51226) sent to partition (0), offset (161841) in 3497 MS Message (51227, message_51227) sent to PA Rtition (0), offset (161842 in 3497 MS Message (51228, message_51228) sent to partition (0), offset (161843) in 3497 Ms .....