62 lines
2.0 KiB
Java
62 lines
2.0 KiB
Java
package com.thinker.util;
|
||
|
||
import com.thinker.model.Metric;
|
||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||
import com.alibaba.fastjson.JSON;
|
||
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
import java.util.Properties;
|
||
|
||
/**
|
||
* @author zeekling [lingzhaohui@zeekling.cn]
|
||
* @version 1.0
|
||
* @apiNote 往 kafka 中写数据工具类:
|
||
* @since 2020-05-05
|
||
*/
|
||
public class KafkaUtils {
|
||
|
||
private static final String broker_list = "localhost:9092";
|
||
private static final String topic = "metric"; // kafka topic,Flink 程序中需要和这个统一
|
||
|
||
private static void writeToKafka() throws InterruptedException {
|
||
Properties props = new Properties();
|
||
props.put("bootstrap.servers", broker_list);
|
||
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); //key 序列化
|
||
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); //value 序列化
|
||
KafkaProducer producer = new KafkaProducer<String, String>(props);
|
||
|
||
Metric metric = new Metric();
|
||
metric.setTimestamp(System.currentTimeMillis());
|
||
metric.setName("mem");
|
||
Map<String, String> tags = new HashMap<>();
|
||
Map<String, Object> fields = new HashMap<>();
|
||
|
||
tags.put("cluster", "zhisheng");
|
||
tags.put("host_ip", "101.147.022.106");
|
||
|
||
fields.put("used_percent", 90d);
|
||
fields.put("max", 27244873d);
|
||
fields.put("used", 17244873d);
|
||
fields.put("init", 27244873d);
|
||
|
||
metric.setTags(tags);
|
||
metric.setFields(fields);
|
||
|
||
ProducerRecord record = new ProducerRecord<String, String>(topic, null, null, JSON.toJSONString(metric));
|
||
producer.send(record);
|
||
System.out.println("发送数据: " + JSON.toJSONString(metric));
|
||
|
||
producer.flush();
|
||
}
|
||
|
||
public static void main(String[] args) throws InterruptedException {
|
||
while (true) {
|
||
Thread.sleep(300);
|
||
writeToKafka();
|
||
}
|
||
}
|
||
|
||
}
|