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> <version>2.0.33</version>
</dependency>--> </dependency>-->
<!-- https://mvnrepository.com/artifact/com.influxdb/influxdb-client-java -->
<dependency> <dependency>
<groupId>com.influxdb</groupId> <groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId> <artifactId>influxdb-client-java</artifactId>
<version>2.0.0</version> <version>3.1.0</version>
</dependency> </dependency>
<!--webflux依赖--> <!--webflux依赖-->
<!--<dependency> <!--<dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

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

View File

@ -1,6 +1,8 @@
package io.qyi.e5.outlook_log.service.impl; package io.qyi.e5.outlook_log.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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.entity.OutlookLog;
import io.qyi.e5.outlook_log.service.IOutlookLogService; import io.qyi.e5.outlook_log.service.IOutlookLogService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -9,7 +11,7 @@ import java.util.List;
/** /**
* <p> * <p>
* 服务实现类 * 服务实现类
* </p> * </p>
* *
* @author 落叶 * @author 落叶
@ -19,8 +21,9 @@ import java.util.List;
public class OutlookLogServiceImpl implements IOutlookLogService { public class OutlookLogServiceImpl implements IOutlookLogService {
@Override @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 outlookLog = new OutlookLog();
// outlookLog.setGithubId(githubId) // outlookLog.setGithubId(githubId)
// .setOutlookId(outlookId) // .setOutlookId(outlookId)
@ -29,6 +32,8 @@ public class OutlookLogServiceImpl implements IOutlookLogService {
// .setOriginalMsg(original_msg); // .setOriginalMsg(original_msg);
// baseMapper.insert(outlookLog); // baseMapper.insert(outlookLog);
} }
@Override @Override

View File

@ -9,12 +9,10 @@ import org.junit.jupiter.api.Test;
import org.springframework.cglib.beans.BeanMap; import org.springframework.cglib.beans.BeanMap;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
public class influxdb2Test { 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"; private String org = "luoye";
@ -34,48 +32,129 @@ public class influxdb2Test {
List<OutlookLog> list = new ArrayList<>(); List<OutlookLog> list = new ArrayList<>();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
OutlookLog outlookLog = new OutlookLog(); 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); list.add(outlookLog);
} }
BeanMap beanMap = BeanMap.create(list.get(0)); try (WriteApi writeApi = influxDBClient.getWriteApi()) {
try (WriteApi writeApi = influxDBClient.getWriteApi(writeOptions)) { // writeApi.writeMeasurement("e5", org ,WritePrecision.NS,outlookLog);
// writeApi.writeMeasurement(); writeApi.writeMeasurements("e5", org ,WritePrecision.NS,list);
// writeApi.writeMeasurements("e5", org ,WritePrecision.NS,list);
List<Point> list1 = new ArrayList<>(); List<Point> list1 = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.forEach(outlookLog -> {
BeanMap beanMap = BeanMap.create(outlookLog);
Point point = Point Point point = Point
.measurement("githubId_123") .measurement("githubId_100")
.addTag("githubId","123465") .addTag("githubId", "123465")
.addFields(beanMap) // .addFields(beanMap)
.time(Instant.now(), WritePrecision.NS); .addFields(beanMap);
list1.add(point); list1.add(point);
} });
System.out.println("list 大小:" + list1.size());
writeApi.writePoints("e5",org,list1); Map<String, Object> aa = new HashMap<>();
aa.put("a1", 1);
writeApi.writePoint("e5",org,list1.get(0));
} }
influxDBClient.close(); 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 @Test
public void find(){ public void find(){
String flux = "from(bucket:\"e5\") |> range(start: 0)" + String flux = "from(bucket:\"e5\") |> range(start: 0)" +
"|> filter(fn: (r) => r[\"_measurement\"] == \"OutlookLog\")" + "|> 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)"; "|> limit(n: 100)";
QueryApi queryApi = influxDBClient.getQueryApi(); QueryApi queryApi = influxDBClient.getQueryApi();
List<FluxTable> tables = queryApi.query(flux,org); List<FluxTable> tables = queryApi.query(flux,org);
for (FluxTable fluxTable : tables) { for (FluxTable fluxTable : tables) {
List<FluxRecord> records = fluxTable.getRecords(); List<FluxRecord> records = fluxTable .getRecords();
for (FluxRecord fluxRecord : records) { for (FluxRecord fluxRecord : records) {
System.out.println(fluxRecord.getField()); // System.out.println(fluxRecord.getField());
System.out.println(fluxRecord.getTime() + " ->" + fluxRecord.getValueByKey("_value")); System.out.println(fluxRecord.getField() + " ->" + fluxRecord.getValueByKey("_value"));
} }
System.out.println("------------------------------------------");
} }
influxDBClient.close(); 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();
}
} }