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 | 89 ++++++++++++++++++++++++++++++++++++++------
1 files changed, 77 insertions(+), 12 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 32e71c6..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,13 +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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -55,18 +61,9 @@
try {
// 鎵ц鏌ヨ
Stream<Map<String, Object>> mapStream = influxDBClient.queryRows(sql);
- logger.info("鏌ヨ鏁版嵁锛歿}", mapStream.collect(Collectors.toList()));
- //query.forEach(row -> System.out.printf("| %-8s | %-8s | %-30s |%n", row[1], row[2], row[0]));
- // 杞崲涓� 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 null;
+ 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();
@@ -74,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