1.maven依赖
1<dependency> 2 <groupId>org.apache.kafka</groupId> 3 <artifactId>kafka_2.11</artifactId> 4 <version>1.0.0</version> 5 </dependency>
2.生产者
1import org.apache.kafka.clients.producer.Callback; 2import org.apache.kafka.clients.producer.KafkaProducer; 3import org.apache.kafka.clients.producer.ProducerRecord; 4import org.apache.kafka.clients.producer.RecordMetadata; 5 6import java.util.Properties; 7 8/** 9 * Created by zhangpeiran on 2018/9/30. 10 */ 11public class MyProducer { 12 private static class DemoProducerCallback implements Callback{ 13 public void onCompletion(RecordMetadata recordMetadata,Exception e){ 14 if(e != null) 15 e.printStackTrace(); 16 else 17 System.out.print("send message success"); 18 } 19 } 20 21 public static void main(String[] args){ 22 Properties kafkaProps = new Properties(); 23 kafkaProps.put("bootstrap.servers","ip1:9092,ip2:9092"); 24 kafkaProps.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer"); 25 kafkaProps.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer"); 26 27 KafkaProducer producer = new KafkaProducer<String,String>(kafkaProps); 28 29 ProducerRecord<String,String> record = new ProducerRecord<String, String>("DemoTopic","DemoKey","DemoValue"); 30 31 //async 32 producer.send(record,new DemoProducerCallback()); 33 producer.close(); 34 } 35}
3.自定义分区
1import org.apache.kafka.clients.producer.Partitioner;import org.apache.kafka.common.Cluster;import org.apache.kafka.common.record.InvalidRecordException;import org.apache.kafka.common.utils.Utils;import java.util.Map;/** * Created by zhangpeiran on 2018/10/8. */public class MyPartitioner implements Partitioner { public void configure(Map<String,?> configs){} public void close(){} public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) { if((keyBytes == null) || !(key instanceof String)) throw new InvalidRecordException("Message should has a key"); int numPartitions = cluster.partitionsForTopic(topic).size(); return Math.abs(Utils.murmur2(keyBytes)) % numPartitions; }} 2 3需要在Producer中加入配置: 4 5kafkaProps.put("partitioner.class","MyPartitioner");
4.消费者
1import org.apache.kafka.clients.consumer.ConsumerRecord; 2import org.apache.kafka.clients.consumer.ConsumerRecords; 3import org.apache.kafka.clients.consumer.KafkaConsumer; 4 5import java.util.Collections; 6import java.util.Properties; 7 8/** 9 * Created by zhangpeiran on 2018/10/9. 10 */ 11public class MyConsumer { 12 13 public static void main(String[] args){ 14 Properties properties = new Properties(); 15 properties.put("bootstrap.servers","ip1:9092,ip2:9092,ip3:9092"); 16 properties.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer"); 17 properties.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer"); 18 properties.put("group.id","DemoConsumerGroup"); 19 20 //默认值为latest,当消费者读取的分区没有偏移量或偏移量无效时,消费者将从最新的记录开始读 21 //当一个消费group第一次订阅主题时,符合这种情况,在Consumer启动之前,Producer生产的数据不会被读取 22 //置为earliest,表示从分区起始位置读取消息 23 properties.put("auto.offset.reset","earliest"); 24 25 KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(properties); 26 27 consumer.subscribe(Collections.singletonList("DemoTopic")); 28 29 try { 30 while (true){ 31 ConsumerRecords<String,String> records = consumer.poll(100); 32 for(ConsumerRecord<String ,String> record : records){ 33 System.out.print(record.topic() + "," + record.partition() + "," + record.offset() + "," + record.key() + "," + record.value()); 34 } 35 } 36 } finally { 37 consumer.close(); 38 } 39 } 40}