kafka生产消费者demo

生产者代码:

package cn.itcast.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("zk.connect", "shizhan1:2181,shizhan2:2181,shizhan3:2181");
        props.put("metadata.broker.list", "shizhan1:9092,shizhan2:9092,shizhan3: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 = 1; i <= 2; i++)
            producer.send(new KeyedMessage<String, String>("firstTopic", "{\"project_id\":\"92\",\"user_id\":\"52b034c6-ceef-2551-a951-e3993da599bd\",\"anonymous_id\":\"8f8ab9b0-3e2c-5414-a87c-f85052d248e0\",\"timestamp\":\"2018-07-19T02:20:36.747486+00:00\",\"properties\":{\"course_name\":\"\u4f4e\u566a\u58f0\u7535\u5b50\u7535\u8def\",\"is_active\":true,\"course_type_level1\":\"\u57fa\u7840\u6559\u7a0b\",\"cost_type\":\"\u514d\u8d39\",\"course_type_level2\":\"\u521d\u7ea7\u8bfe\u7a0b\",\"course_id\":\"4\",\"classdate\":\"2018-05-26\"},\"message_id\":\"d16f4b554b394a429b568af6efb736ef\",\"context\":{\"ip\":\"221.232.192.41\",\"library\":{\"version\":\"0.1.1\",\"name\":\"analytics-python\"},\"campaign\":{\"source\":\"\u5fae\u535a\",\"medium\":\"\u5fae\u535a\"}},\"received_at\":\"2018-08-01T11:50:57.831108\",\"customer_id\":\"91\",\"type\":\"track\",\"event\":\"course.enrollment.activated\"}"
            ));
    }
}

消费者代码:

package cn.itcast.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 = "firstTopic";
    private static final Integer threads = 2;

    public static void main(String[] args) {
        
        Properties props = new Properties();
        props.put("zookeeper.connect", "shizhan1:2181,shizhan2:2181,shizhan3:2181");
        props.put("group.id", "vvvvv");
        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();
        }
    }
}

 

posted @ 2018-11-13 19:12  马小纪  阅读(569)  评论(0编辑  收藏  举报