e5/src/test/java/influxdb2Test.java

176 lines
6.7 KiB
Java
Raw Normal View History

2021-08-17 17:56:15 +08:00
import com.influxdb.LogLevel;
import com.influxdb.client.*;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
import io.qyi.e5.outlook_log.entity.OutlookLog;
2021-07-30 17:55:05 +08:00
import org.junit.jupiter.api.Test;
2021-08-17 17:56:15 +08:00
import org.springframework.cglib.beans.BeanMap;
2021-08-18 18:05:36 +08:00
import java.util.*;
2021-07-30 17:55:05 +08:00
public class influxdb2Test {
String token = "HquxNXwOyfgW-f8wzkSUuBz0tswWiFPsTbEnr5jKFS3BY3RcKezDdQF0o5yeoNfaiwQUJhy8YJSIUrVrWSQn8Q==";
InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://127.0.0.1:8086", token.toCharArray()
2021-08-17 17:56:15 +08:00
);
private String org = "luoye";
2021-07-30 17:55:05 +08:00
2021-08-18 18:40:06 +08:00
WriteOptions writeOptions = WriteOptions.builder()
.batchSize(100)
.build();
2021-08-17 17:56:15 +08:00
@Test
public void save() {
2021-07-30 17:55:05 +08:00
2021-08-17 17:56:15 +08:00
influxDBClient.setLogLevel(LogLevel.BASIC);
WriteOptions writeOptions = WriteOptions.builder()
.batchSize(5000)
.flushInterval(1000)
.bufferLimit(10000)
.jitterInterval(1000)
.retryInterval(5000)
.build();
2021-07-30 17:55:05 +08:00
2021-08-17 17:56:15 +08:00
List<OutlookLog> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
OutlookLog outlookLog = new OutlookLog();
outlookLog.setMsg(i + "- ok").setOriginalMsg("加入成功").setCallTime(System.currentTimeMillis());
2021-08-17 17:56:15 +08:00
list.add(outlookLog);
}
2021-08-17 18:46:31 +08:00
2021-08-18 18:05:36 +08:00
try (WriteApi writeApi = influxDBClient.getWriteApi()) {
// writeApi.writeMeasurement("e5", org ,WritePrecision.NS,outlookLog);
writeApi.writeMeasurements("e5", org, WritePrecision.NS, list);
2021-08-18 09:55:36 +08:00
List<Point> list1 = new ArrayList<>();
2021-08-18 18:05:36 +08:00
list.forEach(outlookLog -> {
BeanMap beanMap = BeanMap.create(outlookLog);
2021-08-18 09:55:36 +08:00
Point point = Point
2021-08-18 18:05:36 +08:00
.measurement("githubId_100")
.addTag("githubId", "123465")
// .addFields(beanMap)
.addFields(beanMap);
2021-08-18 09:55:36 +08:00
list1.add(point);
2021-08-18 18:05:36 +08:00
});
System.out.println("list 大小:" + list1.size());
Map<String, Object> aa = new HashMap<>();
aa.put("a1", 1);
writeApi.writePoint("e5", org, list1.get(0));
2021-08-18 18:05:36 +08:00
}
influxDBClient.close();
}
@Test
public void saveLog() {
2021-08-18 18:05:36 +08:00
influxDBClient.setLogLevel(LogLevel.BASIC);
// for (int i = 0; i < 1000; i++) {
addLog(19658189, 4959, "error", 1, "检测到3次连续错误下次将不再自动调用请修正错误后再授权开启续订。");
// }
influxDBClient.close();
2021-08-18 18:05:36 +08:00
}
public void addLog(int githubId, int outlookId, String msg, int result, String original_msg) {
2021-08-18 09:55:36 +08:00
2021-08-18 18:40:06 +08:00
try (WriteApi writeApi = influxDBClient.getWriteApi()) {
// List<OutlookLog>
// for (int i = 0; i < 10000; i++) {
2021-08-18 18:40:06 +08:00
OutlookLog log = new OutlookLog();
log.setCallTime(System.currentTimeMillis())
.setGithubId(String.valueOf(githubId))
2021-08-18 18:40:06 +08:00
.setOutlookId(String.valueOf(outlookId))
.setMsg(msg)
.setOriginalMsg(original_msg)
.setResult(result);
Thread.sleep(1);
writeApi.writeMeasurement("e5", org, WritePrecision.NS, log);
// }
2021-08-17 17:56:15 +08:00
} catch (InterruptedException e) {
e.printStackTrace();
2021-08-17 17:56:15 +08:00
}
2021-08-18 18:05:36 +08:00
}
2021-08-18 18:05:36 +08:00
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))
2021-08-18 18:05:36 +08:00
.addField("msg", msg)
.addField("resultc", result)
.addField("originalMsg", original_msg);
writeApi.writePoint("e5", org, point);
2021-08-18 18:05:36 +08:00
}
2021-08-17 17:56:15 +08:00
}
2021-07-30 17:55:05 +08:00
@Test
public void find() {
2021-08-17 17:56:15 +08:00
String flux = "from(bucket:\"e5\") |> range(start: 0)" +
"|> filter(fn: (r) => r[\"_measurement\"] == \"OutlookLog\")" +
2021-08-18 18:05:36 +08:00
"|> filter(fn: (r) => r[\"githubId\"] == \"1002\")" +
"|> filter(fn: (r) => r[\"outlookId\"] == \"37\")" +
2021-08-17 17:56:15 +08:00
"|> limit(n: 100)";
QueryApi queryApi = influxDBClient.getQueryApi();
List<FluxTable> tables = queryApi.query(flux, org);
2021-08-17 17:56:15 +08:00
for (FluxTable fluxTable : tables) {
List<FluxRecord> records = fluxTable.getRecords();
2021-08-17 17:56:15 +08:00
for (FluxRecord fluxRecord : records) {
2021-08-18 18:05:36 +08:00
// System.out.println(fluxRecord.getField());
System.out.println(fluxRecord.getField() + " ->" + fluxRecord.getValueByKey("_value"));
2021-08-17 17:56:15 +08:00
}
2021-08-18 18:05:36 +08:00
System.out.println("------------------------------------------");
2021-08-17 17:56:15 +08:00
}
influxDBClient.close();
2021-07-30 17:55:05 +08:00
}
2021-08-17 17:56:15 +08:00
2021-08-18 18:05:36 +08:00
@Test
public void findPojo() {
2021-08-18 18:05:36 +08:00
String flux = "from(bucket:\"e5\") |> range(start: 0)" +
"|> filter(fn: (r) => r[\"_measurement\"] == \"OutlookLog\")" +
"|> filter(fn: (r) => r[\"githubId\"] == \"1002\")" +
"|> filter(fn: (r) => r[\"outlookId\"] == \"38\")" +
2021-08-18 18:40:06 +08:00
"|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";
2021-08-18 18:05:36 +08:00
QueryApi queryApi = influxDBClient.getQueryApi();
2021-08-18 18:40:06 +08:00
System.out.println(System.currentTimeMillis());
List<OutlookLog> tables = queryApi.query(flux, org, OutlookLog.class);
2021-08-18 18:40:06 +08:00
System.out.println(System.currentTimeMillis());
2021-08-18 18:05:36 +08:00
for (OutlookLog table : tables) {
if (table.getMsg() == null) {
continue;
}
2021-08-18 18:40:06 +08:00
// System.out.println(table);
2021-08-18 18:05:36 +08:00
}
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\"] == \"1003\")" +
"|> filter(fn: (r) => r[\"outlookId\"] == \"39\")" +
"|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";
2021-08-18 18:05:36 +08:00
QueryApi queryApi = influxDBClient.getQueryApi();
queryApi.query(flux, org, OutlookLog.class, (cancellable, outlookLog) -> {
if (outlookLog.getMsg() != null) {
System.out.println(outlookLog);
}
2021-08-18 18:05:36 +08:00
});
System.out.println("查询完成");
Thread.sleep(5_000);
influxDBClient.close();
}
2021-07-30 17:55:05 +08:00
}