maven依赖

  1. <dependency>
  2. <groupId>org.apache.kafka</groupId>
  3. <artifactId>kafka-clients</artifactId>
  4. <version>0.11.0.0</version>
  5. </dependency>

不带回调函数的API

需要用到的类:
KafkaProducer:需要创建一个生产者对象,用来发送数据
ProducerConfig:获取所需的一系列配置参数
ProducerRecord:每条数据都要封装成一个ProducerRecord对象

  1. package com.producer;
  2. import org.apache.kafka.clients.producer.KafkaProducer;
  3. import org.apache.kafka.clients.producer.Producer;
  4. import org.apache.kafka.clients.producer.ProducerRecord;
  5. import java.util.Properties;
  6. import java.util.concurrent.ExecutionException;
  7. public class CustomProducer {
  8. public static void main(String[] args) throws ExecutionException, InterruptedException {
  9. Properties props = new Properties();
  10. //kafka集群,broker-list
  11. props.put("bootstrap.servers", "zjj101:9092");
  12. props.put("acks", "all");
  13. //重试次数
  14. props.put("retries", 1);
  15. //批次大小
  16. props.put("batch.size", 16384);
  17. //等待时间
  18. props.put("linger.ms", 1);
  19. //RecordAccumulator缓冲区大小
  20. props.put("buffer.memory", 33554432);
  21. props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  22. props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  23. Producer<String, String> producer = new KafkaProducer<>(props);
  24. for (int i = 0; i < 100; i++) {
  25. //往topic为false发送数据
  26. producer.send(new ProducerRecord<String, String>("first", Integer.toString(i), Integer.toString(i)));
  27. }
  28. producer.close();
  29. }
  30. }

带回调函数的API

回调函数会在producer收到ack时调用,为异步调用,该方法有两个参数,分别是RecordMetadata和Exception,如果Exception为null,说明消息发送成功,如果Exception不为null,说明消息发送失败。
注意:消息发送失败会自动重试,不需要我们在回调函数中手动重试。

因为是异步调用,Kafka发送的进度和接收回调的进度完全是不一致的,

  1. package com.producer;
  2. import org.apache.kafka.clients.producer.*;
  3. import java.util.Properties;
  4. import java.util.concurrent.ExecutionException;
  5. public class CustomProducer2 {
  6. public static void main(String[] args) throws ExecutionException, InterruptedException {
  7. Properties props = new Properties();
  8. props.put("bootstrap.servers", "zjj101:9092");//kafka集群,broker-list
  9. props.put("acks", "all");
  10. props.put("retries", 1);//重试次数
  11. props.put("batch.size", 16384);//批次大小
  12. props.put("linger.ms", 1);//等待时间
  13. props.put("buffer.memory", 33554432);//RecordAccumulator缓冲区大小
  14. props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  15. props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  16. Producer<String, String> producer = new KafkaProducer<>(props);
  17. for (int i = 0; i < 100; i++) {
  18. producer.send(new ProducerRecord<String, String>("first", Integer.toString(i), Integer.toString(i)), new Callback() {
  19. //回调函数,该方法会在Producer收到ack时调用,为异步调用
  20. @Override
  21. public void onCompletion(RecordMetadata metadata, Exception exception) {
  22. if (exception == null) {
  23. System.out.println("success->" + metadata.offset());
  24. } else {
  25. exception.printStackTrace();
  26. }
  27. }
  28. });
  29. }
  30. producer.close();
  31. }
  32. }