zoukankan      html  css  js  c++  java
  • kafka生产者 API Demo

    添加POM文件

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

    API生产者(注意:topic 要先创建: 本案例topic 为 two)

    package com.kpwong.producer;
    
    import org.apache.kafka.clients.producer.KafkaProducer;
    import org.apache.kafka.clients.producer.ProducerRecord;
    
    import java.util.Properties;
    
    public class MyProducer {
        public static void main(String[] args) {
    
            //Create kafka 生产者配置信息
            Properties properties = new Properties();
            //kafka 集群, broker list
            properties.put("bootstrap.servers", "hadoop202:9092");
            properties.put("acks", "all");
            //重试次数
            properties.put("retries", 1);
            //批次大小
            properties.put("batch.size", 16384);
            //等待时间
            properties.put("linger.ms", 1);
            //RecordAccumulator 缓冲区大小 32M
            properties.put("buffer.memory", 33554432);
            // key value 的序列化类
            properties.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
            properties.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
            //创建生产者对象
            KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
    
            //发送数据
            for(int i = 11 ;i <= 20;i++)
            {
                producer.send(new ProducerRecord<String, String>("two","kpwong--"+i));
            }
    
            //关闭连接
            producer.close();
    
        }
    }

    消费者接受消息.

    bin/kafka-console-consumer.sh  --bootstrap-server hadoop202:9092 --topic two

     备注:ProducerRecord:K,V值 的消息:

     回调函数:

            //发送数据
            for(int i = 11 ;i <= 20;i++)
            {
                producer.send(new ProducerRecord<String, String>("two", "kpwong--" + i), new Callback() {
                    @Override
                    public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                        if( e == null)
                        {
                            System.out.println(recordMetadata.partition() + "-----"+ recordMetadata.offset());
                        }
                        else
                        {
                            e.printStackTrace();
                        }
                    }
                });
            }

  • 相关阅读:
    app接口开发(php)
    eclipse JRE(unbound)问题
    HTTP状态码详解
    HTTP方法
    项目开发注意事项及技巧
    JavaWeb 路径问题
    POJ 3264 Balanced Lineup(RMQ_ST)
    了解NoSQL
    多表查询(章节摘要)
    ios UITableView 获取点击cell对象
  • 原文地址:https://www.cnblogs.com/kpwong/p/14052410.html
Copyright © 2011-2022 走看看