一:时序数据库-Influx应用
目录
0、版本号
1、登录页面
2、账号基本信息
3、数据库案例
4、可视化
5、java案例
0、版本号
InfluxDB v2.4.0
1、登录页面
http://127.0.0.1:8086/signin
账号:自己账号 密码:自己密码
2、账号基本信息
3、数据库案例
4、可视化
5、java案例
1、pom
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId>
<version>6.11.0</version>
</dependency>
2、初始化WriteApiBlocking
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import org.chameleon.threadpool.monitor.PoolCollectData;
import java.util.List;
public class InfluxdbAssign {
private final WriteApiBlocking writeApi;
public InfluxdbAssign() {
writeApi = InfluxDBClientFactory.create(Constant.INFLUXDB_URL,
Constant.INFLUXDB_TOKEN,
Constant.INFLUXDB_ORG,
Constant.INFLUXDB_BUCKET).getWriteApiBlocking();
}
3、写入
package example;
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.QueryApi;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
public class InfluxDB2Example {
private static char[] token = "my-token".toCharArray();
private static String org = "my-org";
private static String bucket = "my-bucket";
public static void main(final String[] args) {
InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://localhost:8086", token, org, bucket);
//
// 获取写的api
//
WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();
//
// 写数据point的方式
//
Point point = Point.measurement("temperature")
.addTag("location", "west")
.addField("value", 55D)
.time(Instant.now().toEpochMilli(), WritePrecision.MS);
writeApi.writePoint(point);
//
// Write by协议的方式写
//
writeApi.writeRecord(WritePrecision.NS, "temperature,location=north value=60.0");
//
// Write by 对象的方式写!!!推荐
//
Temperature temperature = new Temperature();
temperature.location = "south";
temperature.value = 62D;
temperature.time = Instant.now();
writeApi.writeMeasurement( WritePrecision.NS, temperature);
//
// Q查询数据
//
String flux = "from(bucket:\"my-bucket\") |> range(start: 0)";
QueryApi queryApi = influxDBClient.getQueryApi();
List<FluxTable> tables = queryApi.query(flux);
for (FluxTable fluxTable : tables) {
List<FluxRecord> records = fluxTable.getRecords();
for (FluxRecord fluxRecord : records) {
System.out.println(fluxRecord.getTime() + ": " + fluxRecord.getValueByKey("_value"));
}
}
influxDBClient.close();
}
对应的POJO
@Measurement(name = "temperature")
private static class Temperature {
@Column(tag = true)
String location;
@Column
Double value;
@Column(timestamp = true)
Instant time;
}
}