java操做kafka很是的簡單,而後kafka也提供了不少缺省值,通常狀況下咱們不須要修改太多的參數就能使用。下面我貼出代碼。java
pom.xmlapache
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>0.10.2.0</version> </dependency>
生產者:bootstrap
package cn.duanjt; import java.util.Properties; import java.util.Random; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.common.serialization.StringSerializer; public class Producer { public static String topic = "duanjt_test";//定義主題 public static void main(String[] args) throws InterruptedException { Properties p = new Properties(); p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.23.76:9092,192.168.23.77:9092");//kafka地址,多個地址用逗號分割 p.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); p.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(p); try { while (true) { String msg = "Hello," + new Random().nextInt(100); ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, msg); kafkaProducer.send(record); System.out.println("消息發送成功:" + msg); Thread.sleep(500); } } finally { kafkaProducer.close(); } } }
注意:dom
1.kafka若是是集羣,多個地址用逗號分割(,)
2.Properties的put方法,第一個參數能夠是字符串,如:p.put("bootstrap.servers","192.168.23.76:9092")
3.kafkaProducer.send(record)能夠經過返回的Future來判斷是否已經發送到kafka,加強消息的可靠性。同時也能夠使用send的第二個參數來回調,經過回調判斷是否發送成功。
4.p.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);設置序列化類,能夠寫類的全路徑spa
消費者:code
package cn.duanjt; import java.util.Collections; import java.util.Properties; import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.common.serialization.StringDeserializer; public class Consumer { public static void main(String[] args) { Properties p = new Properties(); p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.23.76:9092"); p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); p.put(ConsumerConfig.GROUP_ID_CONFIG, "duanjt_test"); KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<String, String>(p); kafkaConsumer.subscribe(Collections.singletonList(Producer.topic));// 訂閱消息 while (true) { ConsumerRecords<String, String> records = kafkaConsumer.poll(100); for (ConsumerRecord<String, String> record : records) { System.out.println(String.format("topic:%s,offset:%d,消息:%s", // record.topic(), record.offset(), record.value())); } } } }
注意:orm
1.訂閱消息能夠訂閱多個主題
2.ConsumerConfig.GROUP_ID_CONFIG表示消費者的分組,kafka根據分組名稱判斷是否是同一組消費者,同一組消費者去消費一個主題的數據的時候,數據將在這一組消費者上面輪詢。
3.主題涉及到分區的概念,同一組消費者的個數不能大於分區數。由於:一個分區只能被同一羣組的一個消費者消費。出現分區小於消費者個數的時候,能夠動態增長分區。
4.注意和生產者的對比,Properties中的key和value是反序列化,而生產者是序列化。server