kafka消费者
This commit is contained in:
		| @@ -3,7 +3,6 @@ package com.heibaiying.producers; | ||||
| import org.apache.kafka.clients.producer.*; | ||||
|  | ||||
| import java.util.Properties; | ||||
| import java.util.concurrent.ExecutionException; | ||||
|  | ||||
| /* | ||||
|  * Kafka生产者示例——异步发送消息 | ||||
| @@ -15,15 +14,15 @@ public class ProducerASyn { | ||||
|         String topicName = "Hello-Kafka"; | ||||
|  | ||||
|         Properties props = new Properties(); | ||||
|         props.put("bootstrap.servers", "192.168.200.226:9092"); | ||||
|         props.put("bootstrap.servers", "hadoop001: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) { | ||||
|   | ||||
| @@ -18,16 +18,16 @@ public class ProducerSyn { | ||||
|         String topicName = "Hello-Kafka"; | ||||
|  | ||||
|         Properties props = new Properties(); | ||||
|         props.put("bootstrap.servers", "192.168.200.226:9092"); | ||||
|         props.put("bootstrap.servers", "hadoop001: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()); | ||||
|   | ||||
| @@ -14,24 +14,25 @@ public class ProducerWithPartitioner { | ||||
|         String topicName = "Kafka-Partitioner-Test"; | ||||
|  | ||||
|         Properties props = new Properties(); | ||||
|         props.put("bootstrap.servers", "192.168.200.226:9092"); | ||||
|         props.put("bootstrap.servers", "hadoop001: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("partitioner.class", "com.heibaiying.producers.partitioners.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(); | ||||
|     } | ||||
| } | ||||
| @@ -16,15 +16,15 @@ public class SimpleProducer { | ||||
|         String topicName = "Hello-Kafka"; | ||||
|  | ||||
|         Properties props = new Properties(); | ||||
|         props.put("bootstrap.servers", "192.168.200.226:9092"); | ||||
|         props.put("bootstrap.servers", "hadoop001: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); | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -1,11 +1,13 @@ | ||||
| package com.heibaiying.producers.utils; | ||||
| package com.heibaiying.producers.partitioners; | ||||
| 
 | ||||
| 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; | ||||
		Reference in New Issue
	
	Block a user