kafka生产者
This commit is contained in:
		| @@ -0,0 +1,43 @@ | ||||
| package com.heibaiying.producers; | ||||
|  | ||||
| import org.apache.kafka.clients.producer.*; | ||||
|  | ||||
| import java.util.Properties; | ||||
| import java.util.concurrent.ExecutionException; | ||||
|  | ||||
| /* | ||||
|  * Kafka生产者示例——异步发送消息 | ||||
|  */ | ||||
| public class ProducerASyn { | ||||
|  | ||||
|     public static void main(String[] args) { | ||||
|  | ||||
|         String topicName = "Hello-Kafka"; | ||||
|  | ||||
|         Properties props = new Properties(); | ||||
|         props.put("bootstrap.servers", "192.168.200.226:9092"); | ||||
|         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 < 10; i++) { | ||||
|             /*异步发送消息*/ | ||||
|             ProducerRecord<String, String> record = new ProducerRecord<>(topicName, "k" + i, "world" + i); | ||||
|             producer.send(record, new Callback() { | ||||
|                 @Override | ||||
|                 public void onCompletion(RecordMetadata metadata, Exception exception) { | ||||
|                     if (exception != null) { | ||||
|                         System.out.println("进行异常处理"); | ||||
|                     } else { | ||||
|                         System.out.printf("topic=%s, partition=%d, offset=%s \n", | ||||
|                                 metadata.topic(), metadata.partition(), metadata.offset()); | ||||
|                     } | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
|  | ||||
|         /*关闭生产者*/ | ||||
|         producer.close(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,42 @@ | ||||
| package com.heibaiying.producers; | ||||
|  | ||||
| import org.apache.kafka.clients.producer.KafkaProducer; | ||||
| import org.apache.kafka.clients.producer.Producer; | ||||
| import org.apache.kafka.clients.producer.ProducerRecord; | ||||
| import org.apache.kafka.clients.producer.RecordMetadata; | ||||
|  | ||||
| import java.util.Properties; | ||||
| import java.util.concurrent.ExecutionException; | ||||
|  | ||||
| /* | ||||
|  * Kafka生产者示例——同步发送消息 | ||||
|  */ | ||||
| public class ProducerSyn { | ||||
|  | ||||
|     public static void main(String[] args) { | ||||
|  | ||||
|         String topicName = "Hello-Kafka"; | ||||
|  | ||||
|         Properties props = new Properties(); | ||||
|         props.put("bootstrap.servers", "192.168.200.226:9092"); | ||||
|         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 < 10; i++) { | ||||
|             /*同步发送消息*/ | ||||
|             try { | ||||
|                 ProducerRecord<String, String> record = new ProducerRecord<>(topicName, "k" + i, "world" + i); | ||||
|                 RecordMetadata metadata = producer.send(record).get(); | ||||
|                 System.out.printf("topic=%s, partition=%d, offset=%s \n", | ||||
|                         metadata.topic(), metadata.partition(), metadata.offset()); | ||||
|             } catch (InterruptedException | ExecutionException e) { | ||||
|                 e.printStackTrace(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /*关闭生产者*/ | ||||
|         producer.close(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,37 @@ | ||||
| package com.heibaiying.producers; | ||||
|  | ||||
| import org.apache.kafka.clients.producer.*; | ||||
|  | ||||
| import java.util.Properties; | ||||
|  | ||||
| /* | ||||
|  * Kafka生产者示例——异步发送消息 | ||||
|  */ | ||||
| public class ProducerWithPartitioner { | ||||
|  | ||||
|     public static void main(String[] args) { | ||||
|  | ||||
|         String topicName = "Kafka-Partitioner-Test"; | ||||
|  | ||||
|         Properties props = new Properties(); | ||||
|         props.put("bootstrap.servers", "192.168.200.226:9092"); | ||||
|         props.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer"); | ||||
|         props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); | ||||
|  | ||||
|         props.put("partitioner.class", "com.heibaiying.producers.utils.CustomPartitioner"); | ||||
|         props.put("pass.line", 6); | ||||
|  | ||||
|         Producer<Integer, String> producer = new KafkaProducer<>(props); | ||||
|  | ||||
|         for (int i = 0; i <= 10; i++) { | ||||
|             /*异步发送消息*/ | ||||
|             String score = "score:" + i; | ||||
|             ProducerRecord<Integer, String> record = new ProducerRecord<>(topicName, i, score); | ||||
|             producer.send(record, (metadata, exception) -> | ||||
|                     System.out.printf("%s, partition=%d, \n", score, metadata.partition())); | ||||
|         } | ||||
|  | ||||
|         /*关闭生产者*/ | ||||
|         producer.close(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,34 @@ | ||||
| package com.heibaiying.producers; | ||||
|  | ||||
| import org.apache.kafka.clients.producer.KafkaProducer; | ||||
| import org.apache.kafka.clients.producer.ProducerRecord; | ||||
|  | ||||
| import java.util.Properties; | ||||
|  | ||||
| /* | ||||
|  * Kafka生产者示例 | ||||
|  */ | ||||
|  | ||||
| public class SimpleProducer { | ||||
|  | ||||
|     public static void main(String[] args) { | ||||
|  | ||||
|         String topicName = "Hello-Kafka"; | ||||
|  | ||||
|         Properties props = new Properties(); | ||||
|         props.put("bootstrap.servers", "192.168.200.226:9092"); | ||||
|         props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); | ||||
|         props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); | ||||
|         /*创建生产者*/ | ||||
|         org.apache.kafka.clients.producer.Producer<String, String> producer = new KafkaProducer<>(props); | ||||
|  | ||||
|         for (int i = 0; i < 10; i++) { | ||||
|             /* 发送消息*/ | ||||
|             ProducerRecord<String, String> record = new ProducerRecord<>(topicName, "hello" + i, "world" + i); | ||||
|             producer.send(record); | ||||
|         } | ||||
|  | ||||
|         /*关闭生产者*/ | ||||
|         producer.close(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,29 @@ | ||||
| package com.heibaiying.producers.utils; | ||||
|  | ||||
| import org.apache.kafka.clients.producer.Partitioner; | ||||
| import org.apache.kafka.common.Cluster; | ||||
|  | ||||
| import java.util.Map; | ||||
|  | ||||
|  | ||||
| public class CustomPartitioner implements Partitioner { | ||||
|  | ||||
|     private int passLine; | ||||
|  | ||||
|     @Override | ||||
|     public void configure(Map<String, ?> configs) { | ||||
|         passLine = (Integer) configs.get("pass.line"); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) { | ||||
|         return (Integer) key >= passLine ? 1 : 0; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void close() { | ||||
|         System.out.println("分区器关闭"); | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user