zoukankan      html  css  js  c++  java
  • Kafka生产者demo

    Kafka生产者demo

    依赖包:

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

    源码:

    package com.kafka.jdbc;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.kafka.clients.producer.KafkaProducer;
    import org.apache.kafka.clients.producer.Producer;
    import org.apache.kafka.clients.producer.ProducerRecord;
    
    import java.io.File;
    import java.util.List;
    import java.util.Properties;
    
    public class CustomProducerTest {
    
        public static void main(String[] args) throws Exception {
            Properties props = new Properties();
            //kafka 集群,broker-list
            props.put("bootstrap.servers", "10.168.4.76:9093");
            props.put("acks", "0");
            //重试次数
            props.put("retries", 1);
            //批次大小
            props.put("batch.size", 16384);
            //等待时间
            props.put("linger.ms", 1);
            //RecordAccumulator 缓冲区大小
            props.put("buffer.memory", 33554432);
            props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            Producer<String, String> producer = new KafkaProducer<>(props);
            List<String> listJson = FileUtils.readLines(new File("src/main/resources/sample2.txt"),"UTF-8");
    
            try {
                for(String json:listJson) {
                    ProducerRecord<String, String> record = new ProducerRecord<String, String>("topic_gong", json);
                    producer.send(record);
                    System.out.println("消息发送成功:" + json);
                    Thread.sleep(100);
                }
                    
            } finally {
                producer.close();
            }
        }
    
    }

    json文件:

    {"id":1,"name":"张三","age":32,"brithday":"1988-03-04","gender":"男","height":176.4,"weight":69.32,"remarks":"备注","createTime":1593470606000}
    {"id":2,"name":"李四","age":24,"brithday":"1996-06-04","gender":"男","height":188.4,"weight":78.53,"remarks":"备注","createTime":1593470856000}
    {"id":3,"name":"曾华","age":25,"brithday":"1995-07-30","gender":"男","height":183.46,"weight":65.5,"remarks":"备注","createTime":1593530323000}
    {"id":4,"name":"张旭","age":30,"brithday":"1990-12-25","gender":"女","height":165.56,"weight":51.52,"remarks":"备注","createTime":1593530410000}
    {"id":5,"name":"刘冰","age":26,"brithday":"1994-12-02","gender":"女","height":170.25,"weight":55.36,"remarks":"备注","createTime":1593530475000}
    {"id":6,"name":"张丹","age":23,"brithday":"1997-02-20","gender":"女","height":166.56,"weight":53.54,"remarks":"备注","createTime":1593530565000}
    {"id":7,"name":"赵伟伟","age":33,"brithday":"1987-05-10","gender":"男","height":190.55,"weight":90.45,"remarks":"备注","createTime":1593530681000}
    {"id":8,"name":"希林娜依高","age":100,"brithday":"1920-06-30","gender":"女","height":175.56,"weight":60.65,"remarks":"备注","createTime":1593530759000}
    {"id":9,"name":"justin bieber","age":30,"brithday":"1990-06-30","gender":"男","height":180.0,"weight":65.0,"remarks":"备注","createTime":1593530866000}
    {"id":10,"name":"J.X.W.","age":6,"brithday":"2014-06-30","gender":"女","height":85.5,"weight":28.5,"remarks":"备注","createTime":1593531051000}
    {"id":11,"name":"kafka","age":20,"brithday":"2000-07-06","gender":"男","height":185.5,"weight":75.5,"remarks":"好人","createTime":1593570344000}
  • 相关阅读:
    Reverse Integer
    First Bad Version
    Heaters
    Number of Segments in a String
    RPI学习--环境搭建_串口连接
    关键字const
    main函数参数的使用
    Redis 简单使用 and 连接池(python)
    南宁AI项目
    Go part 1 初探
  • 原文地址:https://www.cnblogs.com/gongxr/p/13268315.html
Copyright © 2011-2022 走看看