kafka 往cdh 6的kafka写数据成功

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package org.myorg.quickstart.kafka;

import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

public class SendDataToKafka {
    public static void main(String[] args) {
        SendDataToKafka sendDataToKafka = new SendDataToKafka();
//        sendDataToKafka.send("topic1", "", "roi rpi this is a test data too");
        sendDataToKafka.send("topic1", "", "The preacher was vexed because a certain member of his congregation always fell asleep during the sermon.\r\n" + 
                "\r\n" + 
                "");
    }

    public void send(String topic, String key, String data) {
        Properties props = new Properties();
//        props.put("bootstrap.servers", "127.0.0.1:9092");
        props.put("bootstrap.servers", "192.168.7.131:9092");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("batch.size", 16384);
        props.put("linger.ms", 1);
        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");

        KafkaProducer producer = new KafkaProducer(props);
        for (int i = 1; i < 20; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            producer.send(new ProducerRecord(topic, "" + i, data));
        }
        producer.close();
    }
}

转载于:https://my.oschina.net/thomas2/blog/3008979

你可能感兴趣的:(kafka 往cdh 6的kafka写数据成功)