This commit is contained in:
LuoYe 2021-08-18 18:05:36 +08:00
parent bfba053599
commit 10479c3f9b
4 changed files with 117 additions and 29 deletions

View File

@ -176,13 +176,15 @@
<version>2.0.33</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/com.influxdb/influxdb-client-java -->
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId>
<version>2.0.0</version>
<version>3.1.0</version>
</dependency>
<!--webflux依赖-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -25,18 +25,20 @@ import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
@Measurement(name = "OutlookLog")
public class OutlookLog implements Serializable {
public class OutlookLog {
private static final long serialVersionUID = 1L;
/**
* github_id
*/
// private Integer githubId;
@Column(tag = true)
private String githubId;
/**
* outlook_id
*/
// private Integer outlookId;
@Column(tag = true)
private String outlookId;
/**
* 调用时间
@ -48,7 +50,7 @@ public class OutlookLog implements Serializable {
* 调用结果
*/
@Column
private Integer result;
private Number resultc;
/**
* 如果有错误原因则记录

View File

@ -1,6 +1,8 @@
package io.qyi.e5.outlook_log.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import io.qyi.e5.outlook_log.entity.OutlookLog;
import io.qyi.e5.outlook_log.service.IOutlookLogService;
import org.springframework.stereotype.Service;
@ -19,8 +21,9 @@ import java.util.List;
public class OutlookLogServiceImpl implements IOutlookLogService {
@Override
public void addLog(int githubId,int outlookId, String msg, int result,String original_msg) {
public void addLog(int githubId, int outlookId, String msg, int result, String original_msg) {
// OutlookLog outlookLog = new OutlookLog();
// outlookLog.setGithubId(githubId)
// .setOutlookId(outlookId)
@ -29,6 +32,8 @@ public class OutlookLogServiceImpl implements IOutlookLogService {
// .setOriginalMsg(original_msg);
// baseMapper.insert(outlookLog);
}
@Override

View File

@ -9,12 +9,10 @@ import org.junit.jupiter.api.Test;
import org.springframework.cglib.beans.BeanMap;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.*;
public class influxdb2Test {
InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://127.0.0.1:8086", "o-oFFLbRCFHmGFMMYR7kLFGb4jTUXkJkTCmPBeZxn32prCKxwVpS-FM3pLyCvv0gVao-Cm6c_s2Yl-7Ud_xH_Q==".toCharArray()
InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://127.0.0.1:8086", "ko6GtE_P5R2AlMkCBkEgBwW7rVBl46GYx0IoCrG-Dd5VFxTDSnFJ--BB2f8FRFcGd6Tb_yu6-MlMAD-lMSbH6A==".toCharArray()
);
private String org = "luoye";
@ -34,48 +32,129 @@ public class influxdb2Test {
List<OutlookLog> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
OutlookLog outlookLog = new OutlookLog();
outlookLog.setResult(1).setMsg(i + "- ok").setOriginalMsg("加入成功").setCallTime(Instant.now());
outlookLog.setMsg(i + "- ok").setOriginalMsg("加入成功").setCallTime(Instant.now());
list.add(outlookLog);
}
BeanMap beanMap = BeanMap.create(list.get(0));
try (WriteApi writeApi = influxDBClient.getWriteApi(writeOptions)) {
// writeApi.writeMeasurement();
// writeApi.writeMeasurements("e5", org ,WritePrecision.NS,list);
try (WriteApi writeApi = influxDBClient.getWriteApi()) {
// writeApi.writeMeasurement("e5", org ,WritePrecision.NS,outlookLog);
writeApi.writeMeasurements("e5", org ,WritePrecision.NS,list);
List<Point> list1 = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.forEach(outlookLog -> {
BeanMap beanMap = BeanMap.create(outlookLog);
Point point = Point
.measurement("githubId_123")
.addTag("githubId","123465")
.addFields(beanMap)
.time(Instant.now(), WritePrecision.NS);
.measurement("githubId_100")
.addTag("githubId", "123465")
// .addFields(beanMap)
.addFields(beanMap);
list1.add(point);
}
writeApi.writePoints("e5",org,list1);
});
System.out.println("list 大小:" + list1.size());
Map<String, Object> aa = new HashMap<>();
aa.put("a1", 1);
writeApi.writePoint("e5",org,list1.get(0));
}
influxDBClient.close();
}
@Test
public void saveLog(){
influxDBClient.setLogLevel(LogLevel.BASIC);
for (int i = 0; i < 100; i++) {
addLog(1002, 37,"error", 0, "检测到3次连续错误下次将不再自动调用请修正错误后再授权开启续订。");
}
}
public void addLog(int githubId, int outlookId, String msg, int result, String original_msg) {
try (WriteApi writeApi = influxDBClient.getWriteApi()) {
OutlookLog log = new OutlookLog();
log.setCallTime(Instant.now())
.setGithubId(String.valueOf(githubId) )
.setOutlookId(String.valueOf(outlookId))
.setMsg(msg)
.setOriginalMsg(original_msg).setResultc(result);
writeApi.writeMeasurement("e5",org, WritePrecision.NS, log);
}
}
public void addLog2(int githubId, int outlookId, String msg, int result, String original_msg) {
try (WriteApi writeApi = influxDBClient.getWriteApi()) {
Point point = Point.measurement("OutlookLog")
.addTag("githubId",String.valueOf(githubId))
.addTag("outlookId",String.valueOf(outlookId))
.addField("msg", msg)
.addField("resultc", result)
.addField("originalMsg", original_msg);
writeApi.writePoint("e5",org,point);
}
}
@Test
public void find(){
String flux = "from(bucket:\"e5\") |> range(start: 0)" +
"|> filter(fn: (r) => r[\"_measurement\"] == \"OutlookLog\")" +
// "|> filter(fn: (r) => r[\"_field\"] == \"aaaaaa1\")" +
"|> filter(fn: (r) => r[\"githubId\"] == \"1002\")" +
"|> filter(fn: (r) => r[\"outlookId\"] == \"37\")" +
"|> limit(n: 100)";
QueryApi queryApi = influxDBClient.getQueryApi();
List<FluxTable> tables = queryApi.query(flux,org);
for (FluxTable fluxTable : tables) {
List<FluxRecord> records = fluxTable.getRecords();
List<FluxRecord> records = fluxTable .getRecords();
for (FluxRecord fluxRecord : records) {
System.out.println(fluxRecord.getField());
System.out.println(fluxRecord.getTime() + " ->" + fluxRecord.getValueByKey("_value"));
// System.out.println(fluxRecord.getField());
System.out.println(fluxRecord.getField() + " ->" + fluxRecord.getValueByKey("_value"));
}
System.out.println("------------------------------------------");
}
influxDBClient.close();
}
@Test
public void findPojo(){
String flux = "from(bucket:\"e5\") |> range(start: 0)" +
"|> filter(fn: (r) => r[\"_measurement\"] == \"OutlookLog\")" +
"|> filter(fn: (r) => r[\"githubId\"] == \"1002\")" +
"|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")" +
"|> limit(n: 100)";
QueryApi queryApi = influxDBClient.getQueryApi();
List<OutlookLog> tables = queryApi.query(flux,org,OutlookLog.class);
for (OutlookLog table : tables) {
if (table.getMsg() == null) {
continue;
}
System.out.println("Msg: " + table.getMsg());
System.out.println("OriginalMsg: " + table.getOriginalMsg());
System.out.println("---------------");
}
System.out.println("tables 大小:" + tables.size());
influxDBClient.close();
}
@Test
public void findPojoAsync() throws InterruptedException {
String flux = "from(bucket:\"e5\") |> range(start: 0)" +
"|> filter(fn: (r) => r[\"_measurement\"] == \"OutlookLog\")" +
"|> filter(fn: (r) => r[\"githubId\"] == \"1002\")" +
"|> limit(n: 100)";
QueryApi queryApi = influxDBClient.getQueryApi();
queryApi.query(flux,org,OutlookLog.class,(cancellable, outlookLog) -> {
if (outlookLog.getMsg() != null) {
System.out.println(outlookLog);
}
});
System.out.println("查询完成");
Thread.sleep(5_000);
influxDBClient.close();
}
}