1、异步消费
public static void demo1() { //异步消费 // 1.配置 Properties properties = new Properties(); // 1.1 连接集群 properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"master:9092"); // 1.2 设置key序列化 properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); // 1.3 设置value序列化 properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); // 2.生产数据 KafkaProducer kafaProducer = new KafkaProducer(properties); // 3.发送数据 for (int i = 0; i < 10; i++) { kafaProducer.send(new ProducerRecord("reback","哈哈哈" + i)); } // 4.关闭连接 kafaProducer.close(); }
2、同步发送
public static void demo2() throws ExecutionException, InterruptedException { //同步发送 Properties properties = new Properties(); properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"master:9092"); properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); KafkaProducer kafkaProducer = new KafkaProducer(properties); for (int i = 0; i < 10; i++) { kafkaProducer.send(new ProducerRecord("reback","嘤嘤嘤" + i)).get(); } kafkaProducer.close(); }
3、异步发送,带回调函数
public static void demo3() { //添加回调 Properties properties = new Properties(); properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"master:9092"); properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); KafkaProducer kafkaProducer = new KafkaProducer<>(properties); for (int i = 0; i < 10; i++) { kafkaProducer.send(new ProducerRecord("reback", "happpy" + i), new Callback() { @Override public void onCompletion(RecordMetadata recordMetadata, Exception e) { if (e == null) { System.out.println("主题:" + recordMetadata.topic() + "分区:" + recordMetadata.partition()); }else { e.printStackTrace(); } } }); } kafkaProducer.close(); }
4、指定分区
public static void demo4() { //指定分区 Properties properties = new Properties(); properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"master:9092"); properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); KafkaProducer kafkaProducer = new KafkaProducer<>(properties); for (int i = 0; i < 10; i++) { kafkaProducer.send(new ProducerRecord("reback", 5, "", "hahahhahahaha" + i), new Callback() { @Override public void onCompletion(RecordMetadata recordMetadata, Exception e) { System.out.println("主题:" + recordMetadata.topic() + "分区:" + recordMetadata.partition()); } }); } kafkaProducer.close(); }
5、自定义分区
public static void demo5() throws ExecutionException, InterruptedException { //自定义分区 Properties properties = new Properties(); properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"master:9092"); properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); properties.setProperty(ProducerConfig.PARTITIONER_CLASS_CONFIG,"com.kafka.producer.MyPartitioner"); KafkaProducer kafkaProducer = new KafkaProducer<>(properties); for (int i = 0; i < 10; i++) { kafkaProducer.send(new ProducerRecord<>("reback", "tipm" + i), new Callback() { @Override public void onCompletion(RecordMetadata recordMetadata, Exception e) { System.out.println("主题:" + recordMetadata.topic() + "分区:" + recordMetadata.partition()); }//添加回调 }).get();//.get()同步发送 } kafkaProducer.close(); }
MyPartitioner继承Partitioner
public class MyPartitioner implements Partitioner { @Override public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) { String msgValue = value.toString(); int partition; if (msgValue.contains("tipdm")) { partition = 0; }else { partition = 1; } return partition; } @Override public void close() { } @Override public void configure(Map<String, ?> map) { } }