spring整合influxdb 2.5.1

1. 引入依赖

因为influxdb-client-java 6.5.0使用了okhttp3的4.10.0版本,如果使用了其他版本的okhttp3,是要升级版本。

        <dependency>
            <groupId>com.influxdb</groupId>
            <artifactId>influxdb-client-java</artifactId>
            <version>6.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>logging-interceptor</artifactId>
            <version>4.10.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.squareup.okhttp3</groupId>
                    <artifactId>okhttp</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-stdlib-jdk8</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2. 创建bean并注入到IOC容器

指定服务的地址,token,org,bucket(相当于库)

@Configuration
public class InfluxdbConfig {

    @Value("${influx.url}")
    private String influxDBUrl;
    @Value("${influx.token}")
    private String token;
    @Value("${influx.org}")
    private String org;
    @Value("${influx.bucket}")
    private String bucket;

    @Bean
    public InfluxDBClient influxDBClient() {
        InfluxDBClient influxDBClient = InfluxDBClientFactory.create(influxDBUrl, token.toCharArray(), org, bucket);
        influxDBClient.setLogLevel(LogLevel.BASIC);
        return influxDBClient;
    }
}

3. 创建映射类

 @Measurement(name = "test_influx") 映射表明

 @Column 映射字段,有三个值:timestamp = true 表明是时序时间,字段名默认为time。

name = "device_code",  映射的数据库字段名。tag = true 表明是否是索引。tag,经常作为搜索条件的字段,可以加快搜索速度,但是不重复的数量不要大于10万(看文档好像会有报错,可以按方法步骤解除)。

@Measurement(name = "test_influx")
@Data
public class TestInfluxEntity {

    @Column(timestamp = true)
    private Instant time; //主键生成时间

    @Column(name = "device_code", tag = true)
    private String deviceCode; // 设备号

    @Column(name = "longitude")
    private BigDecimal longitude; // 经度

    @Column(name = "latitude")
    private BigDecimal latitude; // 维度

    @Column(name = "temperature")
    private BigDecimal temperature; // 温度
}

 4. 查询和新增

 influxDBClient.getWriteApiBlocking().writeMeasurements(WritePrecision.S, entities)

writeMeasurements 批量插入。writeMeasurement 单个插入。WritePrecision.S :秒,同一秒只会存在同一条数据,可根据业务需求设定。

influx数据表和字段类型会根据首次插入的数据自动设置

public class TestService {

    @Value("${influx.bucket}")
    private String bucket;

    @Autowired
    private InfluxDBClient influxDBClient;


    public List<TestInfluxEntity> selectTempDeviceHis(String deviceCode, Date beginTime, Date endTime) {
        if (StringUtils.isEmpty(deviceCode) || Objects.isNull(beginTime) || Objects.isNull(endTime) || !beginTime.before(endTime)) {
            return Lists.newArrayList();
        }
        String flux = "from(bucket: \" + " bucket " +\")\n" +
                "  |> range(start: " + beginTime.getTime() / 1000 + ", stop: " + endTime.getTime() / 1000 + " )\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"test_influx\")\n" +
                "  |> filter(fn: (r) => r.device_code == \"" + deviceCode + "\")\n" +
                "  |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\") " +
                "  |> sort(columns: [\"_time\"], desc: false) ";
        System.out.println(flux);
        return influxDBClient.getQueryApi().query(flux, TestInfluxEntity.class);

    }
    
    public void batchSave(List<TempDeviceHisInfluxEntity> entities) {
        if (!CollectionUtils.isEmpty(entities)) {
            influxDBClient.getWriteApiBlocking().writeMeasurements(WritePrecision.S, entities);
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值