• 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();
            }
        }
    }
  • 相关阅读:
    【洛谷5304】[GXOI/ZOI2019] 旅行者(二进制分组+最短路)
    【LOJ6485】LJJ 学二项式定理(单位根反演)
    【CF932E】Team Work(第二类斯特林数简单题)
    【CF960G】Bandit Blues(第一类斯特林数)
    【洛谷4689】[Ynoi2016] 这是我自己的发明(莫队)
    【洛谷5355】[Ynoi2017] 由乃的玉米田(莫队+bitset)
    【洛谷5268】[SNOI2017] 一个简单的询问(莫队)
    【洛谷4688】[Ynoi2016] 掉进兔子洞(莫队+bitset)
    【洛谷3653】小清新数学题(数论)
    【洛谷6626】[省选联考 2020 B 卷] 消息传递(点分治基础题)
  • 原文地址:https://www.cnblogs.com/makailong/p/9954064.html
Copyright © 2020-2023  润新知