From 6c229bdd8c538ef4990a6c54ecfc081a7e95c923 Mon Sep 17 00:00:00 2001
From: zhang <zc857179121@qq.com>
Date: 星期五, 06 二月 2026 10:04:50 +0800
Subject: [PATCH] 1
---
component/component-Influxdb/src/main/java/com/zy/component/influxdb/service/InfluxDBService.java | 92 +++++++++++++++++++++++++++++++++++++++-------
1 files changed, 78 insertions(+), 14 deletions(-)
diff --git a/component/component-Influxdb/src/main/java/com/zy/component/influxdb/service/InfluxDBService.java b/component/component-Influxdb/src/main/java/com/zy/component/influxdb/service/InfluxDBService.java
index 9010073..bb79b77 100644
--- a/component/component-Influxdb/src/main/java/com/zy/component/influxdb/service/InfluxDBService.java
+++ b/component/component-Influxdb/src/main/java/com/zy/component/influxdb/service/InfluxDBService.java
@@ -2,14 +2,19 @@
import com.influxdb.v3.client.InfluxDBClient;
import com.influxdb.v3.client.Point;
+import com.influxdb.v3.client.PointValues;
+import com.influxdb.v3.client.query.QueryOptions;
+import com.influxdb.v3.client.query.QueryType;
import com.influxdb.v3.client.write.WritePrecision;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
import java.time.Instant;
-import java.util.LinkedHashMap;
+import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -40,7 +45,6 @@
.setTimestamp(Instant.now().toEpochMilli(), WritePrecision.MS);
try {
influxDBClient.writePoint(point);
- System.out.println("Data written to the database.");
} catch (Exception e) {
logger.error("Failed to write data to the database.");
e.printStackTrace();
@@ -56,18 +60,10 @@
public List<Map<String, Object>> queryData(String sql) {
try {
// 鎵ц鏌ヨ
- Stream<Object[]> query = influxDBClient.query(sql);
- logger.info("鏌ヨ鏁版嵁锛歿}", query);
- // 杞崲涓� Map 鍒楄〃锛堜究浜庡悗缁鐞嗭級
- List<Map<String, Object>> collect = query.map(record -> {
- Map<String, Object> map = new LinkedHashMap<>();
- for (int i = 0; i < record.length; i += 2) {
- map.put((String) record[i], record[i + 1]);
- }
- return map;
- })
- .collect(Collectors.toList());
- return collect;
+ Stream<Map<String, Object>> mapStream = influxDBClient.queryRows(sql);
+ List<Map<String, Object>> result = mapStream.collect(Collectors.toList());
+ logger.info("鏌ヨ鏁版嵁锛歿}", result);
+ return result;
} catch (Exception e) {
logger.error("Failed to query data from the database.");
e.printStackTrace();
@@ -75,5 +71,73 @@
return null;
}
+ /**
+ * 鏌ヨ鏁版嵁
+ *
+ * @param sql sql璇彞
+ * @return 鏌ヨ缁撴灉鍒楄〃
+ */
+ public <T> List<T> queryPoints(String sql, Class<T> clazz) {
+ try {
+ // 鎵ц鏌ヨ
+ Stream<PointValues> queryPoints = influxDBClient.queryPoints(sql);
+ Field[] declaredFields = clazz.getDeclaredFields();
+
+ // 鍒涘缓涓�涓垪琛ㄧ敤浜庡瓨鍌ㄧ粨鏋�
+ List<T> result = queryPoints.map(point -> {
+ T newInstance = null;
+ try {
+ // 浣跨敤鏃犲弬鏋勯�犲嚱鏁板垱寤烘柊瀹炰緥
+ newInstance = clazz.getDeclaredConstructor().newInstance();
+ } catch (NoSuchMethodException | InstantiationException e) {
+ // 濡傛灉娌℃湁鏃犲弬鏋勯�犲嚱鏁版垨鏃犳硶瀹炰緥鍖栵紝鍒欐姏鍑烘湁鎰忎箟鐨勫紓甯�
+ throw new RuntimeException("鏃犳硶鍒涘缓瀵硅薄瀹炰緥锛岃纭繚绫绘湁鍏叡鏃犲弬鏋勯�犲嚱鏁�: " + clazz.getName(), e);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+
+ String[] tagNames = point.getTagNames();
+ for (String tagName : tagNames) {
+ for (Field declaredField : declaredFields) {
+ if (declaredField.getName().equals(tagName)) {
+ declaredField.setAccessible(true);
+ try {
+ declaredField.set(newInstance, point.getTag(tagName));
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ break;
+ }
+ }
+ }
+ String[] fieldNames = point.getFieldNames();
+ for (String fieldName : fieldNames) {
+ for (Field declaredField : declaredFields) {
+ if (declaredField.getName().equals(fieldName)) {
+ declaredField.setAccessible(true);
+ try {
+ declaredField.set(newInstance, point.getField(fieldName));
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ break;
+ }
+ }
+ }
+ return newInstance;
+ }).collect(Collectors.toList());
+
+ logger.info("鏌ヨ鏁版嵁锛歿}", result);
+ return result;
+ } catch (Exception e) {
+ logger.error("Failed to query data from the database.");
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+
}
--
Gitblit v1.9.1