maven依赖:
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.11</artifactId> <version>0.9.0.0</version> </dependency>
生产者:
package com.sniper.kafka.helloworld; import java.util.Properties; import kafka.javaapi.producer.Producer; import kafka.producer.KeyedMessage; import kafka.producer.ProducerConfig; import kafka.serializer.StringEncoder; public class KafkaProducer { public static void main(String[] args) { Properties prop = new Properties(); prop.setProperty("zookeeper.connect", "192.168.0.231:2181"); prop.setProperty("serializer.class", StringEncoder.class.getName()); prop.setProperty("metadata.broker.list", "192.168.0.231:9092"); Producer<Integer, String> producer = new Producer<Integer, String>(new ProducerConfig(prop)); for(int i=0; i<3; i++) { KeyedMessage<Integer, String> msg = new KeyedMessage<Integer, String>("test", "hi-" + i); producer.send(msg); } producer.close(); } }
消费者:
package com.sniper.kafka.helloworld; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import kafka.consumer.Consumer; import kafka.consumer.ConsumerConfig; import kafka.consumer.ConsumerIterator; import kafka.consumer.KafkaStream; import kafka.javaapi.consumer.ConsumerConnector; public class KafkaConsumer { public static void main(String[] args) { Properties prop = new Properties(); prop.setProperty("zookeeper.connect", "192.168.0.231:2181"); prop.put("zk.connectiontimeout.ms", "1000000"); prop.setProperty("group.id", "group1"); ConsumerConnector consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(prop)); Map<String, Integer> map = new HashMap<String, Integer>(); map.put("test", 1); Map<String, List<KafkaStream<byte[], byte[]>>> msgStreams = consumer.createMessageStreams(map); KafkaStream<byte[], byte[]> kafkaStreams = msgStreams.get("test").get(0); ConsumerIterator<byte[], byte[]> iterator = kafkaStreams.iterator(); while(iterator.hasNext()) { String msg = new String(iterator.next().message()); System.out.println(msg); } } }