基于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 {
/**
* 异步插入: enableBatch这里第一个是point的个数,第二个是时间,单位毫秒 point的个数和时间是联合使用的,如果满100条或者60 *
* 1000毫秒 满足任何一个条件就会发送一次写的请求。
*/
// 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 - 博客园
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)