cuikaidong
2025-06-12 066063ed92fdd40da4dfe21770557f3adba3e1af
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
package org.jeecg.modules.iot.depository;
 
 
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
public class InfluxdbTest {
    /**
     * @param connection
     * @throws SQLException
     */
    public static void createTable(Connection connection) throws SQLException {
        String createTableQuery = "CREATE TABLE IF NOT EXISTS influxdb_data (" +
                "id INT AUTO_INCREMENT PRIMARY KEY, " +
                "time VARCHAR(255), " +
                "measurement VARCHAR(255), " +
                "field_key VARCHAR(255), " +
                "field_value VARCHAR(255))";
        try (PreparedStatement statement = connection.prepareStatement(createTableQuery)) {
            statement.execute();
        }
    }
 
    public static List<Influxdb> processAndInsert(List<FluxTable> tables, Connection mysqlConnection) throws SQLException {
//        String insertQuery = "INSERT INTO influxdb_data (time, measurement, field_key, field_value, field) " +
//                "VALUES (?, ?, ?, ?, ?)";
        List<Influxdb> influxdbList = new ArrayList<>();
//        try (PreparedStatement insertStatement = mysqlConnection.prepareStatement(insertQuery)) {
 
        for (FluxTable table : tables) {
            List<FluxRecord> records = table.getRecords();
            for (FluxRecord record : records) {
                Influxdb influxdb = new Influxdb();
                for (Map.Entry<String, Object> entry : record.getValues().entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
//                        System.out.println("Key: " + key + ", Value: " + value);
                    // 拆分表数据
                    if (key.equals("_time")) {
                        // 解析 ISO 8601 字符串
                        OffsetDateTime odt = OffsetDateTime.parse(value.toString());
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
                        String customFormat = odt.format(formatter);
                        influxdb.setTime(customFormat);
//                            insertStatement.setString(1, customFormat);
                    } else if (key.equals("_value")) {
                        if (value != null) {
                            influxdb.setValue(value.toString());
//                                insertStatement.setString(4, value.toString());
                        } else {
//                                insertStatement.setString(4, "");
                            influxdb.setValue("");
                        }
                    } else if (key.equals("Parameter")) {
                        influxdb.setParameter(value.toString());
//                            insertStatement.setString(3, value.toString());
 
                    } else if (key.equals("_measurement")) {
                        influxdb.setMeasurement(value.toString());
//                            insertStatement.setString(2, value.toString());
                    } else if (key.equals("_field")) {
                        influxdb.setField(value.toString());
//                            insertStatement.setString(5, value.toString());
                    }
                }
                influxdbList.add(influxdb);
//                    insertStatement.executeUpdate();
            }
        }
//        }
        return influxdbList;
    }
}