栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

InfluxDB 踩坑小记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

InfluxDB 踩坑小记

基于version: 1.5.2

使用UTC时间

1、group by time(1w, 3d) // 周日到下周六

1w 默认为周四到下周三,可通过在时间分组中添加偏移量来调整

2、 查询时先调整时区(北京时间)

order by time desc tz('Asia/Shanghai')

3、调整时区后,再调整常见时间格式

// 2022-05-12T23:55:00+08:00
DateFormat dformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date datetime = dformat.parse(date);
DateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String lastDateTime = dateformat.format(datetime);
// 2022-05-12 23:55:00

4、按时间范围查询时可直接按时间戳比较大小

InfluxDB默认的时间是纳秒(ns),即19位时间戳

new Date().getTime() 得到的是13位的时间戳,精度是毫秒(ms)

1 毫秒=1000000 纳秒

select mean(value) as value from go_dns_response where time >=1652284800000000000 and time <=1652371259000000000

5、insert

直接insert时将自动创建表。

没有修改操作。

measurement:相当于MySQL数据库中的表

point:表里的一行数据,由时间戳(timestamp)、标签(tag)、数据(field)组成

timestamp:数据的时间戳,相当于主键。不赋值时,默认为系统时间

tag:相当于索引,采用key=value形式

field:相当于实际记录的数据值,也是采用key=value形式

Retention Policy:存储策略,可自动清理数据,默认会创建存储策略 autogen (保留时间为永久)

注:插入数据时,注意此处的设置,以防插入失败

 

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;

@Slf4j
@Configuration
public class DataSourceInfluxConfig {

	static OkHttpClient.Builder client = new OkHttpClient.Builder().readTimeout(180, TimeUnit.SECONDS);

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

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

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

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

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

	@Bean
	public InfluxDB influxdb() {
		InfluxDB influxDB;
		log.info(url + " " + username + " " + password + " " + database);
		if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password))
			influxDB = InfluxDBFactory.connect(url, username, password, client); // 用户名密码都要配置
		else
			influxDB = InfluxDBFactory.connect(url, client);
		try {
			
//			influxDB.setDatabase(database).enableBatch(1000 * 180, 1000 * 180, TimeUnit.MILLISECONDS);
			influxDB.setDatabase(database);
		} catch (Exception e) {
			log.error("", e);
			e.printStackTrace();
		} finally {
			influxDB.setRetentionPolicy(retention_policy); // 设置默认策略
		}
		influxDB.setLogLevel(InfluxDB.LogLevel.BASIC); // 设置日志输出级别
		return influxDB;
	}
}

 InfluxDB 存储结构、读、写 - schaepher - 博客园

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885887.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号