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; +}