influx2.3.0 操作指南

  • 版本:influx2.3.0

  • 安装方式:docker安装

1、删除数据

1
2
3
4
5
# 按tag和按时间删除数据
influx delete --bucket bucketName \
--start '1970-01-01T00:00:00Z' \
--stop $(date +"%Y-%m-%dT%H:%M:%SZ") \
--predicate '_measurement="测量点名称" AND tagName="参数"'
  • bucketName:存储桶名称

  • measurement:测量点名称

  • tagName:测量点定义的tag名称

2、导入/导出Line Protocol格式数据(influx cli)

influx特有的导出方式,导出内容类似于CSV文件格式,可以通过python将其转换为CSV,再导入其它数据库

1
2
3
4
5
6
7
8
influxd inspect export-lp \
--bucket-id a7b1313e6fa3a90d \
--engine-path /var/lib/influxdb2/engine \
--output-path ./port_history_data.lp \
--start 2020-01-01T00:00:00Z \
--end 2024-01-31T23:59:59Z \
--measurement port_history_data \
--compress #压缩
  • –bucket-id:存储桶的ID,不能用名称,只能用ID
  • –engine-path:influx数据存储路径,默认是/var/lib/influxdb2/engine
  • –output-path:导出后输出,及文件名,类型可以是 lp或 tar.gz
  • –start/–end:筛选导出数据
  • –measurement:测量点名称
  • –compress:是否需要压缩,建议加上

导入:

-p:表示文件中time的时间类型,s:表示秒,ns:表示纳秒,默认导出的是纳秒格式

1
2
3
4
#格式 
influx write -b 存储桶名称 --format lp -f 文件路径 -p s
#例子
influx write -b BBB --format lp -f ./file -p s

influx数据时区转换脚本

因为influx有时区问题,写过的转换脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#! /bin/bash

#获取bucketID
bucketID=`influx bucket find --name=BBB | awk '{print $1}' | sed -n '2,2p'`
echo '获取到bucketId:' $bucketID

#备份数据
echo "开始备份数据"
influxd inspect export-lp --bucket-id ${bucketID} --engine-path /var/lib/influxdb2/engine --output-path ./old_data1 --measurement table1
influxd inspect export-lp --bucket-id ${bucketID} --engine-path /var/lib/influxdb2/engine --output-path ./old_data2 --measurement table2

#增加28800s
echo "正在转换数据"
awk '{$3=($3/1000000000)+28800} 1' old_data1 > old_data1_new
awk '{$3=($3/1000000000)+28800} 1' old_data2 > old_data2_new

#清空旧表
echo "开始清空旧表"
influx delete --bucket acmeict --start '1970-01-01T00:00:00Z' --stop $(date +"%Y-%m-%dT%H:%M:%SZ") --predicate '_measurement="table1"'
influx delete --bucket acmeict --start '1970-01-01T00:00:00Z' --stop $(date +"%Y-%m-%dT%H:%M:%SZ") --predicate '_measurement="table2"'

#重新写入
echo "正在重新写入"
influx write -b acmeict --format lp -f ./old_data1_new -p s
influx write -b acmeict --format lp -f ./old_data2_new -p s
echo "升级完成."