• Kafka


    Pom

    <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka-clients</artifactId>
                <version>0.11.0.0</version>
    </dependency>

    生产者

         Properties properties = new Properties();
            // properties.put("bootstrap.servers",
            // "192.168.2.200:9092,192.168.2.157:9092,192.168.2.233:9092,192.168.2.194:9092,192.168.2.122:9092");
            // properties.put("bootstrap.servers",
            // "192.168.2.200:9092,192.168.2.233:9092,192.168.2.122:9092");
            properties.put("bootstrap.servers", "127.0.0.1:9092");
            properties.put("acks", "all");
            properties.put("retries", 3);
            properties.put("batch.size", 16384);
            properties.put("linger.ms", 1);
            properties.put("buffer.memory", 33554432);
            properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            Producer<String, String> producer = null;
            Gson gson = new Gson();
    
            try {
                producer = new KafkaProducer<String, String>(properties);
                for (int i = 0; i < 2; i++) {// topID无所谓
                    producer.send(new ProducerRecord<String, String>("test1", gson.toJson(new obj("test", i + "age"))));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                producer.close();
            }
        }

    消费者

             Properties properties = new Properties();
    //         properties.put("bootstrap.servers", "192.168.2.200:9092,192.168.2.157:9092,192.168.2.233:9092,192.168.2.194:9092,192.168.2.122:9092");
    //         properties.put("bootstrap.servers", "192.168.2.200:9092,192.168.2.233:9092,192.168.2.122:9092");
             properties.put("bootstrap.servers", "127.0.0.1:9092");
             properties.put("group.id", "GroupB");
    //         properties.put("zookeeper.connect", "192.168.2.200:2181,192.168.2.157:2181,192.168.2.233:2181,192.168.2.194:2181,192.168.2.122:2181");
    //         properties.put("zookeeper.connect", "192.168.2.200:2181,192.168.2.233:2181,192.168.2.122:2181");
             properties.put("zookeeper.connect", "127.0.0.1:2181");
             properties.put("zookeeper.session.timeout.ms", "40000");
             properties.put("zookeeper.sync.time.ms", "20000");
             properties.put("auto.commit.interval.ms", "100000");
             properties.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
             properties.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
             KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
             consumer.subscribe(Arrays.asList("test1"));
             while(true) {
                 ConsumerRecords<String, String> records = consumer.poll(100);
                 for (ConsumerRecord<String,String> consumerRecord : records) {
                    System.out.println(consumerRecord.offset()+"	"+consumerRecord.key()+"	"+consumerRecord.value());
                }
             }
             
        }
  • 相关阅读:
    uoj110
    11.28模拟赛D题解
    AT1219 歴史の研究
    P5906 【模板】回滚莫队&不删除莫队
    P4175 [CTSC2008]网络管理
    SP32952 ADAFTBLL
    CF1479D Odd Mineral Resource
    SP10707 COT2
    P4074 [WC2013] 糖果公园
    P6134 [JSOI2015]最小表示
  • 原文地址:https://www.cnblogs.com/zhanchaohan/p/10231550.html
Copyright © 2020-2023  润新知