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

 

posted @ 2020-11-28 14:13  kpwong  阅读(319)  评论(0编辑  收藏  举报