• springboot操作influxdb


    1.influxdb安装(docker)

    1.拉取镜像

    docker pull influxdb:1.8

    2.运行

    docker run -d -p 8086:8086 --name influxdb1.8
    -v /lzp/influxdb:/var/lib/influxdb
    –restart=always
    influxdb:1.8
    这个文件映射是数据文件,配置文件在/etc/influxdb 目录下
    运行后效果
    在这里插入图片描述

    其中data保存数据文件 tsm结尾的文件

    3.修改配置

    在这里插入图片描述

    在容器中安装vim
    1.apt-get update
    2.apt-get install vim
    安装之后编辑配置文件
    [data]
    1.max-series-per-database = 1000000
    每个数据库允许的最大series数,默认设置是一百万。series 指 tag、measurement、policy 相同的数据集合
    将该设置更改为0,以允许每个数据库的序列数量不受限制。
    若超过则会返回500错误,并提示{“error”:“max series per database exceeded: ”}
    2.max-values-per-tag = 100000
    设置每一个tag允许的value最大数量,默认10W,设置0可以取消限制。
    若超过该数值,则会返回错误
    [http]
    3.auth-enabled = true
    开启登陆验证
    在这里插入图片描述

    4.增加用户

    进入容器后输入influx 客户端登陆
    create user “root” with password ‘zh123456’ with all privileges

    2.springboot集成

    1.pom

            <dependency>
                <groupId>plus.ojbk</groupId>
                <artifactId>influxdb-spring-boot-starter</artifactId>
                <version>1.0.2</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.配置文件

    influxdb.url=http://192.168.56.10:8086
    influxdb.username=root
    influxdb.password=zh123456
    influxdb.database=device
    
    • 1
    • 2
    • 3
    • 4

    3.配置类

    这个配置类的功能是增加各种操作延时时间,避免查询大量数据时延时报错。
    思路是继承原有的自动配置类,重写influxdb的构造方法,将时间配置进去。
    这个自动配置类不是官方提供的,是pom依赖作者自己写的,所以普通的修改方式无效,必须以这种重写的方式实现。

    @Configuration
    public class InlConfig extends InfluxdbAutoConfiguration {
    
        @Override
        public InfluxDB influxdb(InfluxdbProperties influxdbProperties) {
    
             OkHttpClient.Builder client = new OkHttpClient.Builder().readTimeout(1000000, TimeUnit.SECONDS);
            InfluxDB influxDB = InfluxDBFactory.connect(influxdbProperties.getUrl(), influxdbProperties.getUsername(), influxdbProperties.getPassword(),client);
            influxDB.setDatabase(influxdbProperties.getDatabase());
            influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
            return influxDB;
        }
    
        @Override
        public InfluxdbTemplate influxdbTemplate(InfluxdbProperties influxdbProperties) {
            return super.influxdbTemplate(influxdbProperties);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    ###3.创建测试类
    @Measurement(name = “device”)对应表名
    时间必须使用LocalDateTime 使用date类型会报错

    @Data
    @Measurement(name = "device")
    public class Device {
        /**
         * 设备编号
         */
        @Column(name="device_no", tag = true)  //tag 可以理解为influxdb的索引
        private String deviceNo;
        /**
         * 数据值
         */
        @Count("value")
        @Column(name="value")
        private BigDecimal value;
        /**
         * 电压
         */
    
        @Column(name="voltage")
        private Float voltage;
        /**
         * 状态
         */
        @Column(name="state")
        private Boolean state;
        /**
         * 上报时间
         */
        @Column(name="time")
        private LocalDateTime time;
    
        /**
         * 上报时间
         */
        @Column(name="test")
        public String test;
    }
    
    • 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

    4.创建测试方法

    插入 每条数据时间增加五秒

        @Test
        void insert() {
            Calendar begin=Calendar.getInstance();
            begin.setTime(new Date());//给定起始时间
    
            List<Device> deviceList = new ArrayList<>();
            for (int i = 0; i < 100000; i++) {
                begin.add(Calendar.SECOND,5);//增加了5s
                LocalDateTime localDateTime = LocalDateTime.ofInstant(begin.toInstant(), begin.getTimeZone().toZoneId());
                Device device = new Device();
                device.setDeviceNo("device-" + i);
                device.setValue(new BigDecimal(12.548));
                device.setState(true);
                device.setVoltage(3.5F);
                device.setTime(localDateTime);
                device.setTest("123");
                deviceList.add(device);
               //influxdbTemplate.insert(deviceList);
               //5w条插入一次
                if(deviceList.size()%50000==0){
                    influxdbTemplate.insert(deviceList);
                    deviceList.clear();
                }
    
            }
            //influxdbTemplate.insert(deviceList);
        }
    
    获取数量
        @Test
        void getCount() {
            QueryModel countModel = new QueryModel();
            ///countModel.setMeasurement(measurement);
            countModel.setMeasurement(InfluxdbUtils.getMeasurement(Device.class));
    //        countModel.setStart(LocalDateTime.now().plusHours(+1L));
    //        countModel.setEnd(LocalDateTime.now().plusHours(+2L));
            //countModel.setSelect(Query.count("voltage"));  //只能count field字段
            countModel.setSelect(Query.count(InfluxdbUtils.getCountField(Device.class)));
            countModel.setWhere(Op.where(countModel));
            //获得总条数
            long count = influxdbTemplate.count(Query.build(countModel));
            System.err.println(count);
        }
    
    获取数据
        @Test
        void getData() {
            QueryModel model = new QueryModel();
            //当前页
    //        model.setCurrent(1L);
    //        //每页数量
    //        model.setSize(1L);
            //model.setMeasurement(measurement);
            //表名
            model.setMeasurement(InfluxdbUtils.getMeasurement(Device.class));
            //查询的字段
            model.setSelect("voltage,state");
    
            Date date = new Date();
            model.setStart(LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault()));
            model.setEnd(LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault()).plusHours(+1));
            model.setUseTimeZone(true);  //时区 这个如果数据库在windows 会报错 需要屏蔽掉
            //model.setOrder(Order.DESC);  //排序
            //where 条件中额外参数可放入model.setMap();
            model.setWhere(Op.where(model));
            //分页数据
            System.out.println(LocalDateTime.now());
            List<Device> deviceList = influxdbTemplate.selectList(Query.build(model), Device.class);
            System.out.println(LocalDateTime.now());
            System.out.println(deviceList.size());
            System.err.println(JSON.toJSONString(deviceList));
        }
    
    • 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

    参考链接

  • 相关阅读:
    用C++或者Python解析gltf文件
    PyCharm 【unsupported Python 3.1】
    计算机网络——物理层-编码与调制(数字基带信号、模拟基带信号、码元、常用编码、基本调制方法、混合调制)
    C语言必会15个文件函数
    必看!!!客户端requests与服务端request收发请求
    DiffBIR: Towards Blind Image Restoration with Generative Diffusion Prior
    Simlab python二次开发1-将所有缸套内表面半径加大1mm
    Ubuntu升级自带的Python3版本
    项目流程图
    Nginx优化——VTS监控模块
  • 原文地址:https://blog.csdn.net/weixin_46666822/article/details/127656016