1. 程式人生 > >Kafka消費者JavaAPI Kafka生產者JavaAPI

Kafka消費者JavaAPI Kafka生產者JavaAPI

package cn.edu360.kafka;

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.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.message.MessageAndMetadata;

public class ConsumerDemo {
private static final String topic = “test888”;
private static final Integer threads = 2;

public static void main(String[] args) {

    Properties props = new Properties();
    props.put("zookeeper.connect", "node-1.xiaoniu.com:2181,node-2.xiaoniu.com:2181,node-3.xiaoniu.com:2181");
    props.put("group.id", "vvvvv");
    //smallest重最開始消費,largest代表重消費者啟動後產生的資料才消費
    props.put("auto.offset.reset", "smallest");

    ConsumerConfig config = new ConsumerConfig(props);
    ConsumerConnector consumer =Consumer.createJavaConsumerConnector(config);
    Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
    topicCountMap.put(topic, threads);
    Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
    List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);

    for(final KafkaStream<byte[], byte[]> kafkaStream : streams){
        new Thread(new Runnable() {

            public void run() {
                for(MessageAndMetadata<byte[], byte[]> mm : kafkaStream){
                    String msg = new String(mm.message());
                    System.out.println(msg);
                }
            }
        }).start();
    }
}

}

————————————–生產者——————————————-
package cn.edu360.kafka;

import java.util.Properties;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

public class ProducerDemo {

public static void main(String[] args) {
    Properties props = new Properties();
    props.put("metadata.broker.list", "node-1.xiaoniu.com:9092,node-2.xiaoniu.com:9092,node-3.xiaoniu.com:9092");
    props.put("serializer.class", "kafka.serializer.StringEncoder");
    ProducerConfig config = new ProducerConfig(props);
    Producer<String, String> producer = new Producer<String, String>(config);
    for (int i = 1001; i <= 1100; i++)
        producer.send(new KeyedMessage<String, String>("test888", "xiaoniu" + i));
}

}