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);
}
}
Kafak生产数据Java代码
最新推荐文章于 2023-04-28 16:08:14 发布