springboot+influxdb实现crud操作案例

本文详细介绍了如何使用SpringBoot与InfluxDB进行集成,完成数据的增删查改操作。涵盖了工程结构、配置文件、Controller、Service层、javabean以及测试用例等关键步骤,包括单条、批量插入,查询和删除数据的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 工程结构

二 .工程代码详解

2.1 pom文件

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!--  influxdb 常用jar -->
    <!-- https://mvnrepository.com/artifact/org.influxdb/influxdb-java -->
    <dependency>
      <groupId>org.influxdb</groupId>
      <artifactId>influxdb-java</artifactId>
      <version>2.15</version>
    </dependency>
    <!--  influxdb 常用gson -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.62</version>
    </dependency>

  </dependencies>

2.2 config目录

2.2.1 获取配置文件参数

package com.ljf.influxdb.demo.config;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author liujianfu
* @description       读取配置文件的内容
* @date 2021/5/27 23:19
* @param
* @return
*/
@Component
@ConfigurationProperties(prefix = "demo.influx")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DemoInfluxDbProperties {

    private String userName;
    private String password;
    private String connUrl;
    private String dataBase;
    private String retentionPolicy;
    private String measureMents;
    private String duration;
    private Boolean startDefine;
}

2.2.2 influxdb的template模板

package com.ljf.influxdb.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
public class InfluxDbTemplate {

    private String userName;
    private String password;
    private String connUrl;
    private String dataBase;
    private String retentionPolicy;
    private InfluxDB influxDB;
   //默认保留策略
    private final String POLICY = "autogen";
    //默认构造参数
    public InfluxDbTemplate() {

    }
    //有参构造函数
    public InfluxDbTemplate(String username,
                              String password,
                              String connUrl,
                              String database,
                              String retentionPolicy,String duration,boolean startDefine) {
        this.userName = username;
        this.password = password;
        this.connUrl = connUrl;
        this.dataBase = database;
        this.retentionPolicy = ObjectUtils.isEmpty(retentionPolicy) ? POLICY : retentionPolicy;
        //创建连接
        buildInfluxConn();
        //创建保留策略
        if(startDefine){//使用自定义的保留侧率
            creatRetentionPolicy(retentionPolicy, duration,1, true);//最后一个参数true,代表开启使用,false不使用
        }

    }
    /**
    * @author liujianfu
    * @description       创建连接
    * @date 2021/5/27 23:02
    * @param []
    * @return void
    */
    private void buildInfluxConn() {
        if (ObjectUtils.isEmpty(influxDB)) {
            try {
                influxDB = InfluxDBFactory.connect(connUrl, userName, password);
            } catch (Exception e) {
                log.error("influxDb connection error: {}", e.getMessage());
                throw new RuntimeException(e.getMessage());
            }
        }
        influxDB.setRetentionPolicy(retentionPolicy);
        influxDB.setLogLevel(InfluxDB.LogLevel.NONE);//日志级别
    }
    /**
    * @author liujianfu
    * @description       查询记录
    * @date 2021/5/27 23:05
    * @param [sql]
    * @return org.influxdb.dto.QueryResult
    */

    public QueryResult query(String sql) {
        return influxDB.query(new Query(sql, dataBase));
    }
    /**
    * @author liujianfu
    * @description       创建一条记录
    * @date 2021/5/27 23:05
    * @param [measurement, time, tags, fields]
    * @return org.influxdb.dto.Point
    */
    public Point buildPoints(String measurement,
                             long time,
                             Map<String, String> tags,
                             Map<String, Object> fields) {
        return Point.measurement(measurement)
                .time(time
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值