influxDB 版本 2.0
java连接influxDB
-
生成Token
图片.png
生成token时可选择读写可操作的bucket
图片.png来到这个页面后按照官方文档操作即可
- 添加maven依赖
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId>
<version>2.0.0</version>
</dependency>
3、连接influxdb,创建client并添加数据,查询
package com.jenson;
import java.time.Instant;
import java.util.List;
import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApi;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxTable;
/**
* @author Jenson
*/
public class InfluxDB2Example {
public static void main(final String[] args) {
// You can generate a Token from the "Tokens Tab" in the UI
String token = "S9dKiPFvnSUd9p406aCkCdwZ_q5rke8mNWzxERaNOL6SW0Fl2JIHZ1V9B317BqmBxzdaFwxei2TcYwJu2RXnOg==";
String bucket = "zzs";
String org = "jenson";
// 生成客户端
InfluxDBClient client = InfluxDBClientFactory.create("http://127.0.0.1:8086", token.toCharArray());
// 1、使用 InfluxDB Line Protocol 写入数据
String data = "mem,host=host1 used_percent=23.43234543";
try (WriteApi writeApi = client.getWriteApi()) {
writeApi.writeRecord(bucket, org, WritePrecision.NS, data);
}
// 2.使用 Data Point 写入数据
Point point = Point
.measurement("mem")
.addTag("host", "host1")
.addField("used_percent", 24.43234543)
.time(Instant.now(), WritePrecision.NS);
try (WriteApi writeApi = client.getWriteApi()) {
writeApi.writePoint(bucket, org, point);
}
// 3.使用POJO类写入数据
Mem mem = new Mem();
mem.host = "host1";
mem.used_percent = 25.43234543;
mem.time = Instant.now();
try (WriteApi writeApi = client.getWriteApi()) {
writeApi.writeMeasurement(bucket, org, WritePrecision.NS, mem);
}
// 4. 查询数据
String query = "from(bucket: \""+bucket+"\") |> range(start: -1h) |> filter(fn: (r) => r._measurement == \"mem\")";
List<FluxTable> tables = client.getQueryApi().query(query, org);
System.out.println(tables.toString());
}
}
报错
Exception in thread "main" java.lang.NoSuchMethodError: kotlin.collections.ArraysKt.copyInto([B[BIII)[B
at okio.Segment.writeTo(Segment.kt:164)
at okio.Segment.compact(Segment.kt:147)
at okio.Buffer.write(Buffer.kt:1854)
at okio.Buffer.read(Buffer.kt:1865)
at okio.Buffer.writeAll(Buffer.kt:1655)
at okio.Options$Companion.buildTrieRecursive(Options.kt:187)
at okio.Options$Companion.buildTrieRecursive(Options.kt:174)
at okio.Options$Companion.buildTrieRecursive$default(Options.kt:113)
at okio.Options$Companion.of(Options.kt:72)
at okhttp3.internal.Util.<clinit>(Util.kt:72)
at okhttp3.internal.concurrent.TaskRunner.<clinit>(TaskRunner.kt:309)
at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:41)
at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:47)
at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.kt:471)
at com.influxdb.client.InfluxDBClientOptions$Builder.build(InfluxDBClientOptions.java:451)
at com.influxdb.client.InfluxDBClientFactory.create(InfluxDBClientFactory.java:157)
at com.influxdb.client.InfluxDBClientFactory.create(InfluxDBClientFactory.java:133)
at com.influxdb.client.InfluxDBClientFactory.create(InfluxDBClientFactory.java:116)
at com.jenson.InfluxDB2Example.main(InfluxDB2Example.java:27)
添加依赖:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.3.50</version>
</dependency>
图片.png
在springboot中使用
- 把上面的InfluxDBClient做成一个bean
/**
* @author Jenson
*/
@Configuration
public class InfluxdbConfig {
@Value("${jenson.influxdb.url}")
private String url;
@Value("${jenson.influxdb.token}")
private String token;
@Bean("influxdbClient")
InfluxDBClient createInfluxdbClient(){
// 生成客户端
InfluxDBClient client = InfluxDBClientFactory.create(this.url, this.token.toCharArray());
return client;
}
}
- 把用到的token、url这些参数写在配置文件里,与第一步对应
jenson:
influxdb:
url: http://127.0.0.1:8086
token: S9dKiPFvnSUd9p406aCkCdwZ_q5rke8mNWzxERaNOL6SW0Fl2JIHZ1V9B317BqmBxzdaFwxei2TcYwJu2RXnOg==
- 写post、get接口
// 注入influxDB客户端
@Autowired
private InfluxDBClient influxdbClient;
// 写入数据
@PostMapping("/post-influx-data")
public Boolean postInfluxData(@RequestBody Mem mem) {
mem.setTime(Instant.now());
try (WriteApi writeApi = influxdbClient.getWriteApi()) {
writeApi.writeMeasurement("zzs", "jenson", WritePrecision.NS, mem);
}
return Boolean.TRUE;
}
// 获取数据
@GetMapping("/get-influx-data")
public List<FluxRecord> getInfluxData(@RequestParam Integer startTime) {
String query = "from(bucket: \"zzs\") |> range(start: -"+startTime+"h) |> filter(fn: (r) => r._measurement == \"mem\")";
List<FluxTable> tables = influxdbClient.getQueryApi().query(query, "jenson");
List<FluxRecord> fluxRecords = new ArrayList<>();
tables.forEach(fluxTable -> {
List<FluxRecord> records = fluxTable.getRecords();
fluxRecords.addAll(records);
});
return fluxRecords;
}
参考:https://github.com/influxdata/influxdb-client-java/tree/master/client
网友评论