1、将日志数据收集并发送到Kafka topic中(循环周期CEL\Time)
Properties props = new Properties();
props.put("metadata.broker.list", "node1:9092,node2:9092,node3:9092");
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("request.required.acks", "1");
ProducerConfig config = new ProducerConfig(props);
Producer<String, String> producer = new Producer<String, String>(config);
Reader reader = new Reader();
double[] rtData;
String rtmsg;
while (true) {
long time = System.currentTimeMillis();
for (String tagName : tags) {
rtData = reader.getrtdata(tagName, DataPath);
rtmsg = tagName + " " + rtData[0] + " " + rtData[1] + " " + rtData[2] + " " + rtData[3] + " " + rtData[4] + " " + rtData[5];
String ip = "";
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
KeyedMessage<String, String> data = new KeyedMessage<String, String>("test2", ip, rtmsg);
producer.send(data);发送到topic
}
long _long = System.currentTimeMillis()-time;
System.out.println(_long);
Utils.sleep(CEL\Time-_long);
}