This commit is contained in:
caixiang 2022-10-18 09:15:23 +08:00
parent 13075f4507
commit 0134203b0c
6 changed files with 17 additions and 71 deletions

View File

@ -12,7 +12,6 @@ import com.qgs.dc.influx.param.PageInfo;
import com.qgs.dc.influx.param.QueryDataParam; import com.qgs.dc.influx.param.QueryDataParam;
import com.qgs.dc.influx.param.Range; import com.qgs.dc.influx.param.Range;
import com.qgs.dc.influx.template.Event; import com.qgs.dc.influx.template.Event;
import com.qgs.dc.influx.template.EventString;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -22,7 +21,7 @@ public enum InfluxClient {
/** /**
* influxdb 读写客户端如果write比较繁忙后续可以考虑维护 client一个线程池 * influxdb 读写客户端如果write比较繁忙后续可以考虑维护 client一个线程池
* */ * */
Client("http://192.168.0.170:8086","lkBsC27QZr1W50BSPlGxpTqNNpwuUk5uz1dZZRPSPbCG5VmNDDUo8P3UkZIhGWwfJwkuz6ZGZ7Et4_KBaG3gHw==","qgs","qgs-bucket"), Client("http://192.168.0.170:8086","lkBsC27QZr1W50BSPlGxpTqNNpwuUk5uz1dZZRPSPbCG5VmNDDUo8P3UkZIhGWwfJwkuz6ZGZ7Et4_KBaG3gHw==","qgs","qgs-bucket2"),
; ;
private String url; private String url;
@ -87,6 +86,18 @@ public enum InfluxClient {
//异步 批量写入数据 //异步 批量写入数据
//如果要批量插入的话一次也只能写入 //如果要批量插入的话一次也只能写入
// public void batchInsert(List<Event> events, String measurement){
// List<Point> list = new ArrayList<>();
// for(Event event:events){
// Point point = Point.measurement(measurement)
// .addTag("transationId", event.getTransationId())
// .addTag("argName", event.getArgName())
// .addField("argValue", event.getArgValue())
// .time(event.getTime().toEpochMilli(), WritePrecision.MS);
// list.add(point);
// }
// writeApi.writePoints(list);
// }
public void batchInsert(List<Event> events, String measurement){ public void batchInsert(List<Event> events, String measurement){
List<Point> list = new ArrayList<>(); List<Point> list = new ArrayList<>();
for(Event event:events){ for(Event event:events){
@ -99,18 +110,6 @@ public enum InfluxClient {
} }
writeApi.writePoints(list); writeApi.writePoints(list);
} }
public void batchInsertString(List<EventString> events, String measurement){
List<Point> list = new ArrayList<>();
for(EventString event:events){
Point point = Point.measurement(measurement)
.addTag("transationId", event.getTransationId())
.addTag("argName", event.getArgName())
.addField("argValue", event.getArgValue())
.time(event.getTime().toEpochMilli(), WritePrecision.MS);
list.add(point);
}
writeApi.writePoints(list);
}
public List<FluxTable> query(QueryDataParam param){ public List<FluxTable> query(QueryDataParam param){
String measurement = param.getMeasurement(); String measurement = param.getMeasurement();

View File

@ -7,7 +7,6 @@ import com.influxdb.query.FluxTable;
import com.qgs.dc.influx.config.InfluxClient; import com.qgs.dc.influx.config.InfluxClient;
import com.qgs.dc.influx.param.*; import com.qgs.dc.influx.param.*;
import com.qgs.dc.influx.template.Event; import com.qgs.dc.influx.template.Event;
import com.qgs.dc.influx.template.EventString;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -76,7 +75,7 @@ public class InfluxDemoController {
event.setTransationId("S1"); event.setTransationId("S1");
event.setTime(new Date().toInstant()); event.setTime(new Date().toInstant());
event.setArgName("A1"); event.setArgName("A1");
event.setArgValue((double)3); event.setArgValue("3");
Thread.sleep(100); Thread.sleep(100);
@ -84,7 +83,7 @@ public class InfluxDemoController {
event2.setTransationId("S1"); event2.setTransationId("S1");
event2.setTime(new Date().toInstant()); event2.setTime(new Date().toInstant());
event2.setArgName("A2"); event2.setArgName("A2");
event2.setArgValue((double)4); event2.setArgValue("4");
list.add(event); list.add(event);
list.add(event2); list.add(event2);
@ -94,10 +93,6 @@ public class InfluxDemoController {
} }
@PostMapping("/insertBatchString")
public void insertBatchString(@RequestBody InsertParamString insertParam) throws InterruptedException {
InfluxClient.Client.batchInsertString(insertParam.getList(),insertParam.getMeasurement());
}
public Point insert(Event event, String measurement){ public Point insert(Event event, String measurement){
Point point = Point.measurement(measurement) Point point = Point.measurement(measurement)
@ -116,7 +111,7 @@ public class InfluxDemoController {
event.setTime(Instant.now()); event.setTime(Instant.now());
event.setTransationId("asasd11"); event.setTransationId("asasd11");
event.setArgName("argName11"); event.setArgName("argName11");
event.setArgValue(99765d); event.setArgValue("898989");
Point asProcessCompleteEvent = insert(event, "ASProcessCompleteEvent"); Point asProcessCompleteEvent = insert(event, "ASProcessCompleteEvent");
InfluxClient.Client.insert(event,"ASProcessCompleteEvent"); InfluxClient.Client.insert(event,"ASProcessCompleteEvent");
} }

View File

@ -1,6 +1,5 @@
package com.qgs.dc.influx.param; package com.qgs.dc.influx.param;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.qgs.dc.influx.template.Event; import com.qgs.dc.influx.template.Event;
import lombok.Data; import lombok.Data;
@ -12,7 +11,6 @@ import java.util.List;
* @DATE: 2022/10/13 16:10 * @DATE: 2022/10/13 16:10
*/ */
@Data @Data
@JsonPropertyOrder(value = {"list","measurement"})
public class InsertParam { public class InsertParam {
List<Event> list; List<Event> list;
String measurement; String measurement;

View File

@ -1,19 +0,0 @@
package com.qgs.dc.influx.param;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.qgs.dc.influx.template.Event;
import com.qgs.dc.influx.template.EventString;
import lombok.Data;
import java.util.List;
/**
* @Desc: ""
* @Author: caixiang
* @DATE: 2022/10/13 16:10
*/
@Data
public class InsertParamString {
List<EventString> list;
String measurement;
}

View File

@ -1,8 +1,5 @@
package com.qgs.dc.influx.template; package com.qgs.dc.influx.template;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import lombok.Data; import lombok.Data;
import java.time.Instant; import java.time.Instant;
@ -13,7 +10,6 @@ import java.time.Instant;
* @DATE: 2022/6/25 11:13 * @DATE: 2022/6/25 11:13
*/ */
@Data @Data
@JsonPropertyOrder(value = {"time","transationId","argName","argValue"})
public class Event { public class Event {
private Instant time; private Instant time;
@ -22,5 +18,5 @@ public class Event {
private String argName; private String argName;
private Double argValue; private String argValue;
} }

View File

@ -1,23 +0,0 @@
package com.qgs.dc.influx.template;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;
import java.time.Instant;
/**
* @Desc: ""
* @Author: caixiang
* @DATE: 2022/6/25 11:13
*/
@Data
public class EventString {
private Instant time;
private String transationId;
private String argName;
private String argValue;
}