1
zhang
7 天以前 3cf258f4eff5e5c197a3026398281b2e63ccb002
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
package com.zy.influxdb.service;
 
import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.time.Instant;
import java.util.LinkedHashMap;
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) {
        WriteApiBlocking writeApiBlocking = influxDBClient.getWriteApiBlocking();
        Point point = Point.measurement(measurement)
                .addTags(tags)
                .addFields(fields)
                .time(Instant.now().toEpochMilli(), WritePrecision.MS);
        try {
            writeApiBlocking.writePoint(point);
            System.out.println("Data written to the database.");
        }
        catch (Exception e) {
            System.err.println("Failed to write data to the database.");
            e.printStackTrace();
        }
    }
    /**
     * 查询数据
     * @param sql sql语句
     * @return 查询结果列表
     */
    public  List<FluxTable> queryData(String sql) {
        try {
            // 执行查询
            return influxDBClient.getQueryApi().query(sql);
        } catch (Exception e) {
            System.err.println("Failed to query data from the database.");
            e.printStackTrace();
        }
        return null;
    }
 
 
    /**
     * 查询数据
     * @param sql sql语句
     * @return 查询结果列表
     */
    public  <T>  List<T> queryData(String sql,T t) {
        try {
            // 执行查询
            List<T> temperatures = influxDBClient.getQueryApi().q(sql, t.getClass());
 
 
        } catch (Exception e) {
            System.err.println("Failed to query data from the database.");
            e.printStackTrace();
        }
        return null;
    }
 
    //POJO类定义如下:
    @Measurement(name = "temperature")//表名
    public static class Temperature {
 
        @Column(tag = true)//tag
        String location;
 
        @Column//field
        Double value;
 
        @Column(timestamp = true)//timestamp
        Instant time;
 
    }
}