Install InfluxDB
InfluxDB2.0 OSS(Object Storage Service,简称OSS)
时序平台建立的目的是为了收集、存储、处理和可视化性能和信息
解压你刚下载的文件到该目录下 C:\Program Files\InfluxData\
,并且可以根据你自己的需要改名
默认情况下,InfluxDB使用TCP端口8086通过InfluxDB HTTP API
进行客户机-服务器通信。
Start InfluxDB
使用cmd
打开influxd.exe
注:
使用influxd --reporting-disabled
命令可关闭信息反馈
设置InfluxDB通过UI界面
- 浏览器访问
localhost:8086
- 点击Get Started
设置你的初始化用户
- 输入初始化用户的用户名
- 给你的初始化用户赋密码
- 输入你的初始化Org名
- 输入你的初始化Bucket名字
- 点击继续
注:
Bucket相当于库的概念,是用来存储你的时序数据的地方。每个的Bucket都有自己的保留策略(比如数据保留几天,这样的一个时间点概念)
录入数据
从UI里面获取以下信息
- organization – See View organizations for instructions on viewing your organization ID.
- Bucket – See View buckets for instructions on viewing your bucket ID.
- API token – See View tokens for instructions on viewing your API token.
- InfluxDB URL – See InfluxDB URLs.
使用约定的格式去录入数据,每行代表一个数据点. 每个数据点都需要一个measurement
和 field
可能还需要tag
和时间戳
。
注:
- measurement相当于table
- filed相当于属性
- tag相当于索引
JAVA Demo
引入依赖
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId>
<version>3.3.0</version>
</dependency>
InfluxDB2Example
推荐使用Point写入数据
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);
//
// Write data
//
WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();
//
// Write by Data Point
//
Point point = Point.measurement("temperature")
.addTag("location", "west")
.addField("value", 55D)
.time(Instant.now().toEpochMilli(), WritePrecision.MS);
writeApi.writePoint(point);
//
// Write by LineProtocol
//
writeApi.writeRecord(WritePrecision.NS, "temperature,location=north value=60.0");
//
// Write by POJO
//
Temperature temperature = new Temperature();
temperature.location = "south";
temperature.value = 62D;
temperature.time = Instant.now();
writeApi.writeMeasurement( WritePrecision.NS, temperature);
influxDBClient.close();
}
@Measurement(name = "temperature")
private static class Temperature {
@Column(tag = true)
String location;
@Column
Double value;
@Column(timestamp = true)
Instant time;
}
}
查数据
可能看的不仔细和不认真的原因,我并没发现InfluxQurey可以写类SQL语句查询,好像过滤条件都需要用他们的flux语句来写查询
Java Demo
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);
//
// Query data
//
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();
}
}