码迷,mamicode.com
首页 > 其他好文 > 详细

kafka ReadFile2Kafka

时间:2019-06-15 15:53:29      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:topic   row   org   shell 脚本   boot   serve   line   参数   value   

public class ReadFile2Kafka {
public static void main(String[] args) throws Exception {

if (args.length != 4) {
throw new RuntimeException("参数列表: bootstrap.servers、topic、sleep、input");
}

Properties props = new Properties();
props.put("bootstrap.servers", args[0]);
props.put("acks", "all");
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);

BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(args[3])));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
Thread.sleep(Integer.parseInt(args[2]));
producer.send(new ProducerRecord<String, String>(args[1], line));
}
producer.flush();
producer.close();
Thread.sleep(3000);
}
}


shell 脚本
java -jar readFile2Kafka.jar node01:9092 $1  1000 /root/maoxiangyi/streaming/data/$1

kafka ReadFile2Kafka

标签:topic   row   org   shell 脚本   boot   serve   line   参数   value   

原文地址:https://www.cnblogs.com/maoxiangyi/p/11027565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!