influxdb 1.8+版本 修改数据之修改field
-
插入数据
-
删除数据
只能按 time的时间范围删除所有数据
不支持 field字段的条件删除 -
更新数据
把不符合的数据的field修改成其他值,那么查询sql就检索不到了,曲线救 国,在业务层面看间接实现了数据的删除。
package com.example.demo.other;
import org.influxdb.BatchOptions;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class xx {
public static void main(String[] args) {
updat();
// in();
// delete();
}
/**
* 插入数据
*/
public static void in() {
// 连接到InfluxDB
InfluxDB influxDB = getInfluxDB();
// 创建表
String database = "ins_message";
String retentionPolicy = "autogen"; // 保留策略,默认为"autogen"
influxDB.createDatabase(database);
influxDB.setDatabase(database);
influxDB.createRetentionPolicy(retentionPolicy, database, "30d", 1, true);
// 插入7天内的数据
String measurement = "my_user_message";
String eventCode = "0";
String eventType = "2";
Instant now = Instant.now();
Instant sevenDaysAgo = now.minus(7, ChronoUnit.DAYS);
for (int i = 0; i < 10; i++) {
Instant timestamp = now.minus(i, ChronoUnit.DAYS);
Point point = Point.measurement(measurement)
.time(timestamp.toEpochMilli(), TimeUnit.MILLISECONDS)
.addField("eventCode", eventCode)
.addField("eventType", eventType)
.addField("source", "ins_8k")
.tag("userId", String.valueOf(111111111))
.tag("pointId", String.valueOf(222222222))
.tag("machineId", String.valueOf(333333333))
.build();
influxDB.write(database, retentionPolicy, point);
}
// 关闭InfluxDB连接
influxDB.close();
}
/**
* influxdb 只支持 按time的时间范围删除 。
* 不支持 field的条件删除
*/
public static void delete() {
// 连接到InfluxDB
InfluxDB influxDB = getInfluxDB();
// 构建删除语句
String database = "ins_message";
String measurement = "my_user_message";
String whereClause = "time > now() - 17d"; // 删除7天内的数据
String deleteQuery = String.format("DELETE FROM %s WHERE %s", measurement, whereClause);
Query query = new Query(deleteQuery, database);
QueryResult queryResult = influxDB.query(query);
System.out.println(deleteQuery);
}
/**
* influxdb 修改数据
*/
public static void updat() {
// InfluxDB连接信息
InfluxDB influxDB = getInfluxDB();
// 获取当前时间和8天前的时间
Instant now = Instant.now();
Instant sevenDaysAgo = now.minus(8, ChronoUnit.DAYS);
// 构建查询语句
String database = "ins_message";
String measurement = "my_user_message";
String updateQuery = String.format("SELECT * FROM \"%s\" WHERE time >= '%s' AND time <= '%s' AND eventCode = '0' AND eventType = '2'", measurement, sevenDaysAgo, now);
Query query = new Query(updateQuery, database);
// 执行查询
QueryResult queryResult = influxDB.query(query);
// 处理查询结果
if (queryResult.hasError()) {
System.out.println("查询操作执行失败:" + queryResult.getError());
} else {
// 创建批量写入对象
BatchPoints batchPoints = BatchPoints
.database(database)
.build();
if (queryResult != null && queryResult.getResults() != null) {
for (QueryResult.Result result : queryResult.getResults()) {
List<QueryResult.Series> series = result.getSeries();
if (series != null && series.size() > 0) {
for (QueryResult.Series serie : series) {
List<List<Object>> values = serie.getValues();//字段字集合
List<String> colums = serie.getColumns();//字段名
for (List<Object> value : values) {
Float eventType = 0f;
Float eventCode = 0f;
long time = 0;
String userId = "";
String pointId = "";
String machineId = "";
for (int j = 0; j < colums.size(); j++) {
if ("time".equals(colums.get(j))) {
time = Instant.parse((String) value.get(j)).toEpochMilli();
} else if ("eventCode".equals(colums.get(j))) {
eventCode = Float.parseFloat((String) value.get(j));
} else if ("eventType".equals(colums.get(j))) {
eventType = Float.parseFloat((String) value.get(j));
} else if ("userId".equals(colums.get(j))) {
userId = (String) value.get(j);
} else if ("pointId".equals(colums.get(j))) {
pointId = (String) value.get(j);
} else if ("machineId".equals(colums.get(j))) {
machineId = (String) value.get(j);
}
}
if (eventType == 2.0f && eventCode == 0.0f) {
Point point = Point.measurement(measurement)
.time(time, TimeUnit.MILLISECONDS)
.tag("userId", userId)
.tag("pointId", pointId)
.tag("machineId", machineId)
.addField("source", "errorData")
.build();
batchPoints.point(point);
}
}
}
}
}
}
// 设置批量写入参数
BatchOptions options = BatchOptions.DEFAULTS.bufferLimit(10000).actions(100);
influxDB.enableBatch(options);
// 执行批量写入
influxDB.write(batchPoints);
System.out.println("数据更新成功");
}
// 关闭InfluxDB连接
influxDB.close();
}
public static InfluxDB getInfluxDB() {
// InfluxDB连接信息
String influxDbUrl = "http://127.0.0.1:8086";
String username = "xxxx";
String password = "xxxxxx";
// 连接到InfluxDB
return InfluxDBFactory.connect(influxDbUrl, username, password);
}
}