1
zhang
16 小时以前 6c229bdd8c538ef4990a6c54ecfc081a7e95c923
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.zy.component.influxdb.service;
 
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;
import java.util.stream.Stream;
 
 
@Service
public class InfluxDBService {
 
    private static final Logger logger = LoggerFactory.getLogger(InfluxDBService.class);
 
    @Autowired
    private InfluxDBClient influxDBClient;
 
 
 
    /**
     * 写入数据
     *
     * @param measurement 表名
     * @param tags        标签
     * @param fields      字段
     */
    public void writeData(String measurement, Map<String, String> tags, Map<String, Object> fields) {
        Point point = Point.measurement(measurement)
                .setTags(tags)
                .setFields(fields)
                .setTimestamp(Instant.now().toEpochMilli(), WritePrecision.MS);
        try {
            influxDBClient.writePoint(point);
        } catch (Exception e) {
            logger.error("Failed to write data to the database.");
            e.printStackTrace();
        }
    }
 
    /**
     * 查询数据
     *
     * @param sql sql语句
     * @return 查询结果列表
     */
    public List<Map<String, Object>> queryData(String sql) {
        try {
            // 执行查询
            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();
        }
        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;
    }
 
 
 
}