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();
        }
    }
}

 

转载于:https://www.cnblogs.com/makailong/p/9954064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值