kafka使用范例

生产者

public class ProducerDemo {
	
	public static void main(String[] args) throws Exception {
		//配置文件
		Properties properties = new Properties();
		properties.put("zk.connect", "lp5:2181,lp6:2181,lp7:2181");
		properties.put("metadata.broker.list","lp5:9092,lp6:9092,lp7:9092");
		properties.put("serializer.class", "kafka.serializer.StringEncoder");
		ProducerConfig config = new ProducerConfig(properties);
		Producer<String, String> producer = new Producer<String, String>(config);
		
		//处理业务
		//读取文件 读取数据库 读取内存
		for(int i=0;i<100;i++){
			Thread.sleep(500);
			KeyedMessage<String, String> message = new KeyedMessage<String, String>("order", "" + i, "10086" + i*i);
			producer.send(message);
		}
	}
}

消费者

public class ComsumerDemo {

	private static final String topic = "order";
	private static final Integer threads = 1;
	
	public static void main(String[] args) {
		//配置文件
		Properties properties = new Properties();
		properties.put("zookeeper.connect", "lp5:2181,lp6:2181,lp7:2181");
		properties.put("group.id", "1111");//确定分组的id,因为会有不同的分区
		properties.put("auto.offset.reset", "smallest");
		
		ConsumerConfig config = new ConsumerConfig(properties);
		ConsumerConnector consumer =Consumer.createJavaConsumerConnector(config);
		Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
		topicCountMap.put(topic, 1);
		Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
		List<KafkaStream<byte[], byte[]>> streams = consumerMap.get("order");
		
		for(final KafkaStream<byte[], byte[]> kafkaStream : streams){
			new Thread(new Runnable() {
				@Override
				public void run() {
					for(MessageAndMetadata<byte[], byte[]> mm : kafkaStream){
						String msg = new String(mm.message());
						System.out.println(msg);
					}
				}
			
			}).start();
		}
		
	}

}


你可能感兴趣的:(kafka)