This commit is contained in:
LuoYe 2021-08-18 18:40:06 +08:00
parent 10479c3f9b
commit 1006883e95

View File

@ -17,6 +17,10 @@ public class influxdb2Test {
private String org = "luoye"; private String org = "luoye";
WriteOptions writeOptions = WriteOptions.builder()
.batchSize(100)
.build();
@Test @Test
public void save(){ public void save(){
@ -61,23 +65,32 @@ public class influxdb2Test {
@Test @Test
public void saveLog(){ public void saveLog(){
influxDBClient.setLogLevel(LogLevel.BASIC); influxDBClient.setLogLevel(LogLevel.BASIC);
for (int i = 0; i < 100; i++) {
addLog(1002, 37,"error", 0, "检测到3次连续错误下次将不再自动调用请修正错误后再授权开启续订。"); addLog(1002, 37,"error", 0, "检测到3次连续错误下次将不再自动调用请修正错误后再授权开启续订。");
for (int i = 0; i < 10000; i++) {
} }
} }
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) {
try (WriteApi writeApi = influxDBClient.getWriteApi()) { try (WriteApi writeApi = influxDBClient.getWriteApi()) {
OutlookLog log = new OutlookLog(); List<OutlookLog> list = new ArrayList<>();
log.setCallTime(Instant.now()) for (int i = 0; i < 10000; i++) {
.setGithubId(String.valueOf(githubId) ) OutlookLog log = new OutlookLog();
.setOutlookId(String.valueOf(outlookId)) log.setCallTime(Instant.now())
.setMsg(msg) .setGithubId(String.valueOf(githubId) )
.setOriginalMsg(original_msg).setResultc(result); .setOutlookId(String.valueOf(outlookId))
writeApi.writeMeasurement("e5",org, WritePrecision.NS, log); .setMsg(msg)
.setOriginalMsg(original_msg)
.setResultc(result)
.setCallTime(Instant.now());
list.add(log);
}
writeApi.writeMeasurements("e5",org, WritePrecision.NS, list);
influxDBClient.close();
} }
} }
@ -123,17 +136,16 @@ public class influxdb2Test {
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[\"githubId\"] == \"1002\")" + "|> filter(fn: (r) => r[\"githubId\"] == \"1002\")" +
"|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")" + "|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";
"|> limit(n: 100)";
QueryApi queryApi = influxDBClient.getQueryApi(); QueryApi queryApi = influxDBClient.getQueryApi();
System.out.println(System.currentTimeMillis());
List<OutlookLog> tables = queryApi.query(flux,org,OutlookLog.class); List<OutlookLog> tables = queryApi.query(flux,org,OutlookLog.class);
System.out.println(System.currentTimeMillis());
for (OutlookLog table : tables) { for (OutlookLog table : tables) {
if (table.getMsg() == null) { if (table.getMsg() == null) {
continue; continue;
} }
System.out.println("Msg: " + table.getMsg()); // System.out.println(table);
System.out.println("OriginalMsg: " + table.getOriginalMsg());
System.out.println("---------------");
} }
System.out.println("tables 大小:" + tables.size()); System.out.println("tables 大小:" + tables.size());
influxDBClient.close(); influxDBClient.close();
@ -144,7 +156,7 @@ public class influxdb2Test {
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[\"githubId\"] == \"1002\")" + "|> filter(fn: (r) => r[\"githubId\"] == \"1002\")" +
"|> limit(n: 100)"; "|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")" ;
QueryApi queryApi = influxDBClient.getQueryApi(); QueryApi queryApi = influxDBClient.getQueryApi();
queryApi.query(flux,org,OutlookLog.class,(cancellable, outlookLog) -> { queryApi.query(flux,org,OutlookLog.class,(cancellable, outlookLog) -> {
if (outlookLog.getMsg() != null) { if (outlookLog.getMsg() != null) {