美文网首页java
Springboot集成influxDB实现基本操作

Springboot集成influxDB实现基本操作

作者: 初心myp | 来源:发表于2022-02-28 17:04 被阅读0次

    本文提供简单的Springboot集成influxDB,实现基础的增删改查

    package com.sinochem.it.influx.config;
    
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    import org.influxdb.InfluxDB;
    import org.influxdb.InfluxDB.ConsistencyLevel;
    import org.influxdb.InfluxDBFactory;
    import org.influxdb.dto.BatchPoints;
    import org.influxdb.dto.Point;
    import org.influxdb.dto.Point.Builder;
    import org.influxdb.dto.Pong;
    import org.influxdb.dto.Query;
    import org.influxdb.dto.QueryResult;
    
    public class InfluxDBConnection {
    
        // 用户名
        private String username;
        // 密码
        private String password;
        // 连接地址
        private String openurl;
        // 数据库
        private String database;
        // 保留策略
        private String retentionPolicy;
    
        private InfluxDB influxDB;
    
        public InfluxDBConnection(String username, String password, String openurl, String database,
                                  String retentionPolicy) {
            this.username = username;
            this.password = password;
            this.openurl = openurl;
            this.database = database;
            this.retentionPolicy = retentionPolicy == null || retentionPolicy.equals("") ? "autogen" : retentionPolicy;
            influxDbBuild();
        }
        /**
         * 创建数据库
         *
         * @param dbName
         */
        @SuppressWarnings("deprecation")
        public void createDB(String dbName) {
            influxDB.createDatabase(dbName);
        }
        /**
         * 删除数据库
         *
         * @param dbName
         */
        @SuppressWarnings("deprecation")
        public void deleteDB(String dbName) {
            influxDB.deleteDatabase(dbName);
        }
        /**
         * 测试连接是否正常
         *
         * @return true 正常
         */
        public boolean ping() {
            boolean isConnected = false;
            Pong pong;
            try {
                pong = influxDB.ping();
                if (pong != null) {
                    isConnected = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return isConnected;
        }
    
        /**
         * 连接时序数据库 ,若不存在则创建
         *
         * @return
         */
        public InfluxDB influxDbBuild() {
            if (influxDB == null) {
                influxDB = InfluxDBFactory.connect(openurl, username, password);
            }
            try {
                // if (!influxDB.databaseExists(database)) {
                // influxDB.createDatabase(database);
                // }
            } catch (Exception e) {
                // 该数据库可能设置动态代理,不支持创建数据库
                // e.printStackTrace();
            } finally {
                influxDB.setRetentionPolicy(retentionPolicy);
            }
            influxDB.setLogLevel(InfluxDB.LogLevel.NONE);
            return influxDB;
        }
    
        /**
         * 创建自定义保留策略
         *
         * @param policyName
         * 策略名
         * @param duration
         * 保存天数
         * @param replication
         * 保存副本数量
         * @param isDefault
         * 是否设为默认保留策略
         */
        public void createRetentionPolicy(String policyName, String duration, int replication, Boolean isDefault) {
            String sql = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s ", policyName,
                    database, duration, replication);
            if (isDefault) {
                sql = sql + " DEFAULT";
            }
            this.query(sql);
        }
        /**
         * 创建默认的保留策略
         *
         * @param 策略名:default,保存天数:30天,保存副本数量:1
         * 设为默认保留策略
         */
        public void createDefaultRetentionPolicy() {
            String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
                    "default", database, "30d", 1);
            this.query(command);
        }
    
        /**
         * 查询
         *
         * @param command
         * 查询语句
         * @return
         */
        public QueryResult query(String command) {
            return influxDB.query(new Query(command, database));
        }
    
        /**
         * 插入
         *
         * @param measurement
         * 表
         * @param tags
         * 标签
         * @param fields
         * 字段
         */
        public void insert(String measurement, Map<String, String> tags, Map<String, Object> fields, long time,
                           TimeUnit timeUnit) {
            Builder builder = Point.measurement(measurement);
            builder.tag(tags);
            builder.fields(fields);
            if (0 != time) {
                builder.time(time, timeUnit);
            }
            influxDB.write(database, retentionPolicy, builder.build());
        }
    
        public void insert(String measurement, Map<String, String> tags, Map<String, Object> fields) {
            Builder builder = Point.measurement(measurement);
            builder.tag(tags);
            builder.fields(fields);
    
            influxDB.write(database, retentionPolicy, builder.build());
        }
    
        /**
         * 批量写入测点
         *
         * @param batchPoints
         */
        public void batchInsert(BatchPoints batchPoints) {
            influxDB.write(batchPoints);
            // influxDB.enableGzip();
            // influxDB.enableBatch(2000,100,TimeUnit.MILLISECONDS);
            // influxDB.disableGzip();
            // influxDB.disableBatch();
        }
        /**
         * 批量写入数据
         *
         * @param database
         * 数据库
         * @param retentionPolicy
         * 保存策略
         * @param consistency
         * 一致性
         * @param records
         * 要保存的数据(调用BatchPoints.lineProtocol()可得到一条record)
         */
        public void batchInsert(final String database, final String retentionPolicy, final ConsistencyLevel consistency,
                                final List<String> records) {
            influxDB.write(database, retentionPolicy, consistency, records);
        }
        /**
         * 删除
         *
         * @param command
         * 删除语句
         * @return 返回错误信息
         */
        public String deleteMeasurementData(String command) {
            QueryResult result = influxDB.query(new Query(command, database));
            return result.getError();
        }
    
        /**
         * 关闭数据库
         */
        public void close() {
            influxDB.close();
        }
    
        /**
         * 构建Point
         *
         * @param measurement
         * @param time
         * @param fields
         * @return
         */
        public Point pointBuilder(String measurement, long time, Map<String, String> tags, Map<String, Object> fields) {
            Point point = Point.measurement(measurement).time(time, TimeUnit.MILLISECONDS).tag(tags).fields(fields).build();
            return point;
        }
    
    
    }
    
    
    测试类
    package com.sinochem.it.influx;
    
    import com.sinochem.it.influx.config.InfluxDBConnection;
    import org.influxdb.dto.QueryResult;
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    import java.util.stream.Collectors;
    
    @SpringBootTest
    class ZhnyInfluxApplicationTests {
    
        @Test
        void contextLoads() {
            InfluxDBConnection influxDBConnection = new InfluxDBConnection("root", "SX&Root2021@ZHNY#.M", "http://10.156.130.234:39100", "testDB", "");
    
            for (int i = 0; i <1000 ; i++) {
                HashMap<String, String> targs = new HashMap<>();
                targs.put("host","serverB");
    
                HashMap<String, Object> fields = new HashMap<>();
                fields.put("region","us_wwww");
                fields.put("time",System.currentTimeMillis());
                fields.put("value",System.currentTimeMillis());
                fields.put("f1","0.8118");
                fields.put("f2","0.8118");
    
                influxDBConnection.insert("cpu",targs,fields);
            }
    
    
    
    
            QueryResult results = influxDBConnection
                    .query("SELECT count(1) FROM cpu");
            //results.getResults()是同时查询多条SQL语句的返回值,此处我们只有一条SQL,所以只取第一个结果集即可。
            QueryResult.Result oneResult = results.getResults().get(0);
            if (oneResult.getSeries() != null) {
                List<List<Object>> valueList = oneResult.getSeries().stream().map(QueryResult.Series::getValues)
                        .collect(Collectors.toList()).get(0);
                if (valueList != null && valueList.size() > 0) {
                    for (List<Object> value : valueList) {
                        Map<String, String> map = new HashMap<String, String>();
                        // 数据库中字段1取值
                        String field1 = value.get(0) == null ? null : value.get(0).toString();
                        // 数据库中字段2取值
                        String field2 = value.get(1) == null ? null : value.get(1).toString();
                        // TODO 用取出的字段做你自己的业务逻辑……
                    }
                }
            }
    
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:Springboot集成influxDB实现基本操作

        本文链接:https://www.haomeiwen.com/subject/gphehrtx.html