1. 引入依赖
因为influxdb-client-java 6.5.0使用了okhttp3的4.10.0版本,如果使用了其他版本的okhttp3,是要升级版本。
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId>
<version>6.5.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.10.0</version>
<exclusions>
<exclusion>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</exclusion>
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</exclusion>
</exclusions>
</dependency>
2. 创建bean并注入到IOC容器
指定服务的地址,token,org,bucket(相当于库)
@Configuration
public class InfluxdbConfig {
@Value("${influx.url}")
private String influxDBUrl;
@Value("${influx.token}")
private String token;
@Value("${influx.org}")
private String org;
@Value("${influx.bucket}")
private String bucket;
@Bean
public InfluxDBClient influxDBClient() {
InfluxDBClient influxDBClient = InfluxDBClientFactory.create(influxDBUrl, token.toCharArray(), org, bucket);
influxDBClient.setLogLevel(LogLevel.BASIC);
return influxDBClient;
}
}
3. 创建映射类
@Measurement(name = "test_influx") 映射表明
@Column 映射字段,有三个值:timestamp = true 表明是时序时间,字段名默认为time。
name = "device_code", 映射的数据库字段名。tag = true 表明是否是索引。tag,经常作为搜索条件的字段,可以加快搜索速度,但是不重复的数量不要大于10万(看文档好像会有报错,可以按方法步骤解除)。
@Measurement(name = "test_influx")
@Data
public class TestInfluxEntity {
@Column(timestamp = true)
private Instant time; //主键生成时间
@Column(name = "device_code", tag = true)
private String deviceCode; // 设备号
@Column(name = "longitude")
private BigDecimal longitude; // 经度
@Column(name = "latitude")
private BigDecimal latitude; // 维度
@Column(name = "temperature")
private BigDecimal temperature; // 温度
}
4. 查询和新增
influxDBClient.getWriteApiBlocking().writeMeasurements(WritePrecision.S, entities)
writeMeasurements 批量插入。writeMeasurement 单个插入。WritePrecision.S :秒,同一秒只会存在同一条数据,可根据业务需求设定。
influx数据表和字段类型会根据首次插入的数据自动设置
public class TestService {
@Value("${influx.bucket}")
private String bucket;
@Autowired
private InfluxDBClient influxDBClient;
public List<TestInfluxEntity> selectTempDeviceHis(String deviceCode, Date beginTime, Date endTime) {
if (StringUtils.isEmpty(deviceCode) || Objects.isNull(beginTime) || Objects.isNull(endTime) || !beginTime.before(endTime)) {
return Lists.newArrayList();
}
String flux = "from(bucket: \" + " bucket " +\")\n" +
" |> range(start: " + beginTime.getTime() / 1000 + ", stop: " + endTime.getTime() / 1000 + " )\n" +
" |> filter(fn: (r) => r[\"_measurement\"] == \"test_influx\")\n" +
" |> filter(fn: (r) => r.device_code == \"" + deviceCode + "\")\n" +
" |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\") " +
" |> sort(columns: [\"_time\"], desc: false) ";
System.out.println(flux);
return influxDBClient.getQueryApi().query(flux, TestInfluxEntity.class);
}
public void batchSave(List<TempDeviceHisInfluxEntity> entities) {
if (!CollectionUtils.isEmpty(entities)) {
influxDBClient.getWriteApiBlocking().writeMeasurements(WritePrecision.S, entities);
}
}
}