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>
2.配置文件
influxdb.url=http://192.168.56.10:8086
influxdb.username=root
influxdb.password=zh123456
influxdb.database=device
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);
}
}
###3.创建测试类 @Measurement(name = “device”)对应表名 时间必须使用LocalDateTime 使用date类型会报错
@Data
@Measurement(name = "device")
public class Device {
@Column(name="device_no", tag = true)
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;
}
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);
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);
if(deviceList.size()%50000==0){
influxdbTemplate.insert(deviceList);
deviceList.clear();
}
}
}
获取数量
@Test
void getCount() {
QueryModel countModel = new QueryModel();
countModel.setMeasurement(InfluxdbUtils.getMeasurement(Device.class));
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.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);
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));
}
参考链接
|