From 9b9a6fe315ec635568b8a43955b2f7bb1a360301 Mon Sep 17 00:00:00 2001 From: caixiang <939387484@qq.com> Date: Mon, 17 Oct 2022 10:12:20 +0800 Subject: [PATCH 1/3] mark --- src/main/java/com/qgs/dc/influx/param/InsertParam.java | 2 ++ src/main/java/com/qgs/dc/influx/template/Event.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/com/qgs/dc/influx/param/InsertParam.java b/src/main/java/com/qgs/dc/influx/param/InsertParam.java index 58d462d..06696d7 100644 --- a/src/main/java/com/qgs/dc/influx/param/InsertParam.java +++ b/src/main/java/com/qgs/dc/influx/param/InsertParam.java @@ -1,5 +1,6 @@ package com.qgs.dc.influx.param; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.qgs.dc.influx.template.Event; import lombok.Data; @@ -11,6 +12,7 @@ import java.util.List; * @DATE: 2022/10/13 16:10 */ @Data +@JsonPropertyOrder(value = {"list","measurement"}) public class InsertParam { List list; String measurement; diff --git a/src/main/java/com/qgs/dc/influx/template/Event.java b/src/main/java/com/qgs/dc/influx/template/Event.java index 822df54..4a472c4 100644 --- a/src/main/java/com/qgs/dc/influx/template/Event.java +++ b/src/main/java/com/qgs/dc/influx/template/Event.java @@ -1,5 +1,6 @@ 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; @@ -12,6 +13,7 @@ import java.time.Instant; * @DATE: 2022/6/25 11:13 */ @Data +@JsonPropertyOrder(value = {"time","transationId","argName","argValue"}) public class Event { private Instant time; From 13075f4507f42cf0cc5d38de67ad1a1c0925ed9c Mon Sep 17 00:00:00 2001 From: caixiang <939387484@qq.com> Date: Tue, 18 Oct 2022 09:07:21 +0800 Subject: [PATCH 2/3] mark --- src/main/java/com/qgs/dc/influx/Main.java | 15 +++- .../qgs/dc/influx/config/InfluxClient.java | 14 +++- .../controller/InfluxDemoController.java | 79 ++++++++++++++++++- .../dc/influx/param/InsertParamString.java | 19 +++++ .../qgs/dc/influx/template/EventString.java | 23 ++++++ 5 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/qgs/dc/influx/param/InsertParamString.java create mode 100644 src/main/java/com/qgs/dc/influx/template/EventString.java diff --git a/src/main/java/com/qgs/dc/influx/Main.java b/src/main/java/com/qgs/dc/influx/Main.java index d31dcc8..efe87d6 100644 --- a/src/main/java/com/qgs/dc/influx/Main.java +++ b/src/main/java/com/qgs/dc/influx/Main.java @@ -170,7 +170,20 @@ public class Main { //两种写法都可以获取到前三天的日期 // calendar1.set(Calendar.DAY_OF_YEAR,calendar1.get(Calendar.DAY_OF_YEAR) -3); //在当前时间的基础上获取前三天的日期 - calendar1.add(Calendar.DATE, -3); + calendar1.add(Calendar.DATE, -999); + //add方法 参数也可传入 月份,获取的是前几月或后几月的日期 + //calendar1.add(Calendar.MONTH, -3); + Date day = calendar1.getTime(); + return day; + } + public static Date getDateAfter(){ + Date date = new Date();//获取当前日期 + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");//格式化一下 + Calendar calendar1 = Calendar.getInstance();//获取对日期操作的类对象 + //两种写法都可以获取到前三天的日期 + // calendar1.set(Calendar.DAY_OF_YEAR,calendar1.get(Calendar.DAY_OF_YEAR) -3); + //在当前时间的基础上获取前三天的日期 + calendar1.add(Calendar.DATE, 999); //add方法 参数也可传入 月份,获取的是前几月或后几月的日期 //calendar1.add(Calendar.MONTH, -3); Date day = calendar1.getTime(); diff --git a/src/main/java/com/qgs/dc/influx/config/InfluxClient.java b/src/main/java/com/qgs/dc/influx/config/InfluxClient.java index f437875..94a166c 100644 --- a/src/main/java/com/qgs/dc/influx/config/InfluxClient.java +++ b/src/main/java/com/qgs/dc/influx/config/InfluxClient.java @@ -12,6 +12,7 @@ import com.qgs.dc.influx.param.PageInfo; import com.qgs.dc.influx.param.QueryDataParam; import com.qgs.dc.influx.param.Range; import com.qgs.dc.influx.template.Event; +import com.qgs.dc.influx.template.EventString; import java.util.ArrayList; import java.util.List; @@ -98,7 +99,18 @@ public enum InfluxClient { } writeApi.writePoints(list); } - + public void batchInsertString(List events, String measurement){ + List 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 query(QueryDataParam param){ String measurement = param.getMeasurement(); diff --git a/src/main/java/com/qgs/dc/influx/controller/InfluxDemoController.java b/src/main/java/com/qgs/dc/influx/controller/InfluxDemoController.java index 63c0228..517703b 100644 --- a/src/main/java/com/qgs/dc/influx/controller/InfluxDemoController.java +++ b/src/main/java/com/qgs/dc/influx/controller/InfluxDemoController.java @@ -2,9 +2,12 @@ package com.qgs.dc.influx.controller; import com.influxdb.client.domain.WritePrecision; import com.influxdb.client.write.Point; +import com.influxdb.query.FluxRecord; +import com.influxdb.query.FluxTable; import com.qgs.dc.influx.config.InfluxClient; -import com.qgs.dc.influx.param.InsertParam; +import com.qgs.dc.influx.param.*; import com.qgs.dc.influx.template.Event; +import com.qgs.dc.influx.template.EventString; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.PostMapping; @@ -13,8 +16,12 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.time.Instant; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import static com.qgs.dc.influx.Main.getDate; +import static com.qgs.dc.influx.Main.getDateAfter; + @RestController @RequestMapping("/influx") public class InfluxDemoController { @@ -44,6 +51,53 @@ public class InfluxDemoController { InfluxClient.Client.batchInsert(insertParam.getList(),insertParam.getMeasurement()); } + public static void main(String[] args) throws InterruptedException { +// InsertParamString insertParam = new InsertParamString(); +// List list = new ArrayList<>(); +// EventString event = new EventString(); +// event.setTransationId("S1"); +// event.setTime(new Date().toInstant()); +// event.setArgName("A1"); +// event.setArgValue("v1"); +// +// EventString event2 = new EventString(); +// event2.setTransationId("S1"); +// event2.setTime(new Date().toInstant()); +// event2.setArgName("A2"); +// event2.setArgValue("v2"); +// list.add(event); +// list.add(event2); +// insertParam.setList(list); +// insertParam.setMeasurement("CSCSCSCCS"); +// InfluxClient.Client.batchInsertString(insertParam.getList(),insertParam.getMeasurement()); + InsertParam insertParam = new InsertParam(); + List list = new ArrayList<>(); + Event event = new Event(); + event.setTransationId("S1"); + event.setTime(new Date().toInstant()); + event.setArgName("A1"); + event.setArgValue((double)3); + + Thread.sleep(100); + + Event event2 = new Event(); + event2.setTransationId("S1"); + event2.setTime(new Date().toInstant()); + event2.setArgName("A2"); + event2.setArgValue((double)4); + list.add(event); + list.add(event2); + + insertParam.setList(list); + insertParam.setMeasurement("CSCSCSCCS"); + InfluxClient.Client.batchInsert(insertParam.getList(),insertParam.getMeasurement()); + } + + + @PostMapping("/insertBatchString") + public void insertBatchString(@RequestBody InsertParamString insertParam) throws InterruptedException { + InfluxClient.Client.batchInsertString(insertParam.getList(),insertParam.getMeasurement()); + } public Point insert(Event event, String measurement){ Point point = Point.measurement(measurement) @@ -67,7 +121,30 @@ public class InfluxDemoController { InfluxClient.Client.insert(event,"ASProcessCompleteEvent"); } + @PostMapping("/query") + public void query() throws InterruptedException { + QueryDataParam queryDataParam = new QueryDataParam(); + queryDataParam.setBucket("qgs-bucket"); + queryDataParam.setRange(new Range(getDate().toInstant(),getDateAfter().toInstant())); + queryDataParam.setMeasurement("ASDASD"); + queryDataParam.setTag(new Tag("argName","1233")); + queryDataParam.setDropedTagName("transationId"); + queryDataParam.setPageInfo(new PageInfo(1,100)); + + List tables = InfluxClient.Client.query(queryDataParam); + // List tables = queryApi.query(flux); + for (FluxTable fluxTable : tables) { + List records = fluxTable.getRecords(); + for (FluxRecord fluxRecord : records) { + Object value = fluxRecord.getValueByKey("_value"); + System.out.println("time: "+fluxRecord.getTime() +" key:"+fluxRecord.getField()+" value: " + fluxRecord.getValueByKey("_value")+" measurement: " + fluxRecord.getMeasurement()); + + } + } + + System.out.println(); + } } diff --git a/src/main/java/com/qgs/dc/influx/param/InsertParamString.java b/src/main/java/com/qgs/dc/influx/param/InsertParamString.java new file mode 100644 index 0000000..b668f28 --- /dev/null +++ b/src/main/java/com/qgs/dc/influx/param/InsertParamString.java @@ -0,0 +1,19 @@ +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 list; + String measurement; +} diff --git a/src/main/java/com/qgs/dc/influx/template/EventString.java b/src/main/java/com/qgs/dc/influx/template/EventString.java new file mode 100644 index 0000000..4bf1890 --- /dev/null +++ b/src/main/java/com/qgs/dc/influx/template/EventString.java @@ -0,0 +1,23 @@ +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; +} From 0134203b0c64cce2ae72fe32a42ca3e1bb346ae8 Mon Sep 17 00:00:00 2001 From: caixiang <939387484@qq.com> Date: Tue, 18 Oct 2022 09:15:23 +0800 Subject: [PATCH 3/3] mark --- .../qgs/dc/influx/config/InfluxClient.java | 27 +++++++++---------- .../controller/InfluxDemoController.java | 11 +++----- .../com/qgs/dc/influx/param/InsertParam.java | 2 -- .../dc/influx/param/InsertParamString.java | 19 ------------- .../com/qgs/dc/influx/template/Event.java | 6 +---- .../qgs/dc/influx/template/EventString.java | 23 ---------------- 6 files changed, 17 insertions(+), 71 deletions(-) delete mode 100644 src/main/java/com/qgs/dc/influx/param/InsertParamString.java delete mode 100644 src/main/java/com/qgs/dc/influx/template/EventString.java diff --git a/src/main/java/com/qgs/dc/influx/config/InfluxClient.java b/src/main/java/com/qgs/dc/influx/config/InfluxClient.java index 94a166c..cc8afe9 100644 --- a/src/main/java/com/qgs/dc/influx/config/InfluxClient.java +++ b/src/main/java/com/qgs/dc/influx/config/InfluxClient.java @@ -12,7 +12,6 @@ import com.qgs.dc.influx.param.PageInfo; import com.qgs.dc.influx.param.QueryDataParam; import com.qgs.dc.influx.param.Range; import com.qgs.dc.influx.template.Event; -import com.qgs.dc.influx.template.EventString; import java.util.ArrayList; import java.util.List; @@ -22,7 +21,7 @@ public enum InfluxClient { /** * 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; @@ -87,6 +86,18 @@ public enum InfluxClient { //异步 批量写入数据 //如果要批量插入的话,一次也只能写入 +// public void batchInsert(List events, String measurement){ +// List 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 events, String measurement){ List list = new ArrayList<>(); for(Event event:events){ @@ -99,18 +110,6 @@ public enum InfluxClient { } writeApi.writePoints(list); } - public void batchInsertString(List events, String measurement){ - List 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 query(QueryDataParam param){ String measurement = param.getMeasurement(); diff --git a/src/main/java/com/qgs/dc/influx/controller/InfluxDemoController.java b/src/main/java/com/qgs/dc/influx/controller/InfluxDemoController.java index 517703b..1f5bc18 100644 --- a/src/main/java/com/qgs/dc/influx/controller/InfluxDemoController.java +++ b/src/main/java/com/qgs/dc/influx/controller/InfluxDemoController.java @@ -7,7 +7,6 @@ import com.influxdb.query.FluxTable; import com.qgs.dc.influx.config.InfluxClient; import com.qgs.dc.influx.param.*; import com.qgs.dc.influx.template.Event; -import com.qgs.dc.influx.template.EventString; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.PostMapping; @@ -76,7 +75,7 @@ public class InfluxDemoController { event.setTransationId("S1"); event.setTime(new Date().toInstant()); event.setArgName("A1"); - event.setArgValue((double)3); + event.setArgValue("3"); Thread.sleep(100); @@ -84,7 +83,7 @@ public class InfluxDemoController { event2.setTransationId("S1"); event2.setTime(new Date().toInstant()); event2.setArgName("A2"); - event2.setArgValue((double)4); + event2.setArgValue("4"); list.add(event); 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){ Point point = Point.measurement(measurement) @@ -116,7 +111,7 @@ public class InfluxDemoController { event.setTime(Instant.now()); event.setTransationId("asasd11"); event.setArgName("argName11"); - event.setArgValue(99765d); + event.setArgValue("898989"); Point asProcessCompleteEvent = insert(event, "ASProcessCompleteEvent"); InfluxClient.Client.insert(event,"ASProcessCompleteEvent"); } diff --git a/src/main/java/com/qgs/dc/influx/param/InsertParam.java b/src/main/java/com/qgs/dc/influx/param/InsertParam.java index 06696d7..58d462d 100644 --- a/src/main/java/com/qgs/dc/influx/param/InsertParam.java +++ b/src/main/java/com/qgs/dc/influx/param/InsertParam.java @@ -1,6 +1,5 @@ package com.qgs.dc.influx.param; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.qgs.dc.influx.template.Event; import lombok.Data; @@ -12,7 +11,6 @@ import java.util.List; * @DATE: 2022/10/13 16:10 */ @Data -@JsonPropertyOrder(value = {"list","measurement"}) public class InsertParam { List list; String measurement; diff --git a/src/main/java/com/qgs/dc/influx/param/InsertParamString.java b/src/main/java/com/qgs/dc/influx/param/InsertParamString.java deleted file mode 100644 index b668f28..0000000 --- a/src/main/java/com/qgs/dc/influx/param/InsertParamString.java +++ /dev/null @@ -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 list; - String measurement; -} diff --git a/src/main/java/com/qgs/dc/influx/template/Event.java b/src/main/java/com/qgs/dc/influx/template/Event.java index 4a472c4..1ec6b76 100644 --- a/src/main/java/com/qgs/dc/influx/template/Event.java +++ b/src/main/java/com/qgs/dc/influx/template/Event.java @@ -1,8 +1,5 @@ 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 java.time.Instant; @@ -13,7 +10,6 @@ import java.time.Instant; * @DATE: 2022/6/25 11:13 */ @Data -@JsonPropertyOrder(value = {"time","transationId","argName","argValue"}) public class Event { private Instant time; @@ -22,5 +18,5 @@ public class Event { private String argName; - private Double argValue; + private String argValue; } diff --git a/src/main/java/com/qgs/dc/influx/template/EventString.java b/src/main/java/com/qgs/dc/influx/template/EventString.java deleted file mode 100644 index 4bf1890..0000000 --- a/src/main/java/com/qgs/dc/influx/template/EventString.java +++ /dev/null @@ -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; -}