|
|
|
@ -0,0 +1,88 @@
|
|
|
|
|
package org.dromara.tsdb.component.impl;
|
|
|
|
|
|
|
|
|
|
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.FluxRecord;
|
|
|
|
|
import com.influxdb.query.FluxTable;
|
|
|
|
|
import org.dromara.tsdb.component.InfluxDbClient;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
public class InfluxDb2xClientImpl implements InfluxDbClient {
|
|
|
|
|
|
|
|
|
|
private final InfluxDBClient influxDBClient;
|
|
|
|
|
private final String bucket;
|
|
|
|
|
|
|
|
|
|
public InfluxDb2xClientImpl(
|
|
|
|
|
@Value("${influxdb.url}") String url,
|
|
|
|
|
@Value("${influxdb.token}") String token,
|
|
|
|
|
@Value("${influxdb.org}") String org,
|
|
|
|
|
@Value("${influxdb.bucket}") String bucket) {
|
|
|
|
|
this.influxDBClient = InfluxDBClientFactory.create(url, token.toCharArray(), org, bucket);
|
|
|
|
|
this.bucket = bucket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void writeData(String measurement, Map<String, String> tags, Map<String, Object> fields) {
|
|
|
|
|
try (WriteApi writeApi = influxDBClient.getWriteApi()) {
|
|
|
|
|
Point point = Point.measurement(measurement);
|
|
|
|
|
tags.forEach(point::addTag);
|
|
|
|
|
// 处理 fields,确保类型正确
|
|
|
|
|
fields.forEach((key, value) -> {
|
|
|
|
|
if (value instanceof Number) {
|
|
|
|
|
point.addField(key, (Number) value);
|
|
|
|
|
} else if (value instanceof String) {
|
|
|
|
|
point.addField(key, (String) value);
|
|
|
|
|
} else if (value instanceof Boolean) {
|
|
|
|
|
point.addField(key, (Boolean) value);
|
|
|
|
|
} else {
|
|
|
|
|
throw new IllegalArgumentException("Unsupported field type: " + value.getClass().getName());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
writeApi.writePoint(point);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void writeData(String measurement, Map<String, String> tags, Map<String, Object> fields,long timestamp) {
|
|
|
|
|
try (WriteApi writeApi = influxDBClient.getWriteApi()) {
|
|
|
|
|
Point point = Point.measurement(measurement)
|
|
|
|
|
.time(timestamp,WritePrecision.MS);//设置时间戳
|
|
|
|
|
tags.forEach(point::addTag);
|
|
|
|
|
// 处理 fields,确保类型正确
|
|
|
|
|
fields.forEach((key, value) -> {
|
|
|
|
|
if (value instanceof Number) {
|
|
|
|
|
point.addField(key, (Number) value);
|
|
|
|
|
} else if (value instanceof String) {
|
|
|
|
|
point.addField(key, (String) value);
|
|
|
|
|
} else if (value instanceof Boolean) {
|
|
|
|
|
point.addField(key, (Boolean) value);
|
|
|
|
|
} else {
|
|
|
|
|
throw new IllegalArgumentException("Unsupported field type: " + value.getClass().getName());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
writeApi.writePoint(point);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<Map<String, Object>> queryData(String query) {
|
|
|
|
|
List<FluxTable> tables = influxDBClient.getQueryApi().query(query);
|
|
|
|
|
return tables.stream()
|
|
|
|
|
.flatMap(table -> table.getRecords().stream())
|
|
|
|
|
.map(record -> {
|
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
record.getValues().forEach((key, value) -> map.put(key, value));
|
|
|
|
|
return map;
|
|
|
|
|
})
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
}
|