Kafak生产数据Java代码

​
package kafka;

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

import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class KafkaProducerTest {
    public static final String TOPIC_NAME = "test";
    private static final String CONTENT = "1704197100,9800100,4321,192.168.76.202,iaucap,2017-06-28 13:33:32";
    static Integer times = 0;

    public static void main(String[] args) {
        //brokerIP
        times = 1;
        String broker = args[0];
        //port
        String port = args[1];
        //1表示使用ssl 其余表示不用
        String ssl = args[2];
        Properties props = new Properties();
//        props.put("acks", "all");
        props.put("bootstrap.servers", broker + ":" + port);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        if ("1".equals(ssl)) {
            props.put("security.protocol", "SSL");
            props.put("ssl.truststore.location", "/opt/keys_security/SSL/truststore.jks");
            props.put("ssl.truststore.password", "passSSLv1.0");
            props.put("ssl.keystore.location", "/opt/keys_security/SSL/keystore.jks");
            props.put("ssl.keystore.password", "passSSLv1.0");
            props.put("ssl.key.password", "passSSLv1.0");
            props.put("ssl.endpoint.identification.algorithm", "");
        }
        Producer<String, String> producer = new KafkaProducer<String, String>(props);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("msg:" +times+"   "+ CONTENT);
                times = times + 1;
                producer.send(new ProducerRecord<String, String>(TOPIC_NAME, Integer.toString(times), CONTENT));
            }
        };
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(runnable, 0, 3, TimeUnit.SECONDS);
    }


}

​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值