數據庫操作
- 顯示已存在的所有數據庫
格式: show databases
示例如下:
-
> show databases;
-
name: databases
-
name
-
----
-
_internal
- 創建新數據庫
格式:
create database <dbname>
說明:
dbname : 數據庫名稱
示例如下:
-
> create database testdb;
-
> show databases;
-
name: databases
-
name
-
----
-
_internal
-
testdb
-
>
- 刪除數據庫
格式:
drop database <dbname>
說明:
dbname : 數據庫名稱
示例如下:
-
> drop database testdb;
-
> show databases;
-
name: databases
-
name
-
----
-
_internal
-
-
>
表操作
- 顯示指定數據庫中已存在的表
格式: show measurements
示例如下:
-
> use testdb;
-
Using database testdb
-
> show measurements;
- 創建新表並添加數據
InfluxDB沒有提供單獨的建表語句,可以通過以下方式創建數據庫並添加數據。
格式:
insert <tbname>,<tags> <values> [timestamp]
說明:
tbname : 數據表名稱
tags : 表的tag域
values : 表的value域
timestamp :當前數據的時間戳(可選,沒有提供的話系統會自帶添加)
示例如下:
-
> use testdb;
-
Using database testdb
-
> insert students,stuid=s123 score= 89
-
> show measurements;
-
name: measurements
-
name
-
----
-
students
- 刪除表
格式:
drop measurement <tbname>
說明:
tbname : 數據表名稱
示例如下:
-
> use testdb;
-
Using database testdb
-
> drop measurement students;
-
> show measurements;
-
>
數據操作
- 添加
格式:
insert <tbname>,<tags> <values> [timestamp]
說明:
tbname : 數據表名稱
tags : 表的tag域
values : 表的value域
timestamp :當前數據的時間戳(可選,沒有提供的話系統會自帶添加)
示例如下:
-
> insert students,stuid=s123 score= 79
-
> insert students,stuid=s123 score= 89 1488821368327436809
-
> select * from students
-
name: students
-
time score stuid
-
---- ----- -----
-
1488821368327436809 89 s123
-
1488821404414227498 79 s123
- 查詢
格式:
-
select <fields> from <tbname> [ into_clause ] [ where_clause ]
-
[ group_by_clause ] [ order_by_clause ] [ limit_clause ]
-
[ offset_clause ] [ slimit_clause ] [ soffset_clause ]
說明:
fields : 要查詢的字段,查詢全部可以用*
tbname : 數據表名稱
into_clause : select ... into (可選)
where_clause : where條件域(可選)
group_by_clause : group by相關(可選)
order_by_clause : order by相關(可選)
limit_clause : limit相關(可選)
offset_clause : offset相關(可選)
slimit_clause : slimit相關(可選)
soffset_clause : soffset相關(可選)
示例如下:
-
> use testdb;
-
Using database testdb
-
> show measurements;
-
name: measurements
-
name
-
----
-
students
-
-
> select * from students
-
name: students
-
time score stuid
-
---- ----- -----
-
1488821368327436809 89 s123
-
1488821404414227498 79 s123
-
1488822192864587535 69 s123
-
1488822196951305763 39 s123
-
-
> select * from students where score > 70;
-
name: students
-
time score stuid
-
---- ----- -----
-
1488821368327436809 89 s123
-
1488821404414227498 79 s123
-
-
> select * from students where score > 70 limit 1;
-
name: students
-
time score stuid
-
---- ----- -----
-
1488821368327436809 89 s123
-
-
>
- 更新
tags 和 timestamp相同時數據會執行覆蓋操作,相當於InfluxDB的更新操作。
示例如下:
-
> insert students,stuid=s123 score= 39
-
> select * from students
-
name: students
-
time score stuid
-
---- ----- -----
-
1488822338410283027 39 s123
-
-
> insert students,stuid=s123 score= 99 1488822338410283027
-
> select * from students
-
name: students
-
time score stuid
-
---- ----- -----
-
1488822338410283027 99 s123
-
-
>
- 刪除
格式:
delete from <tbname> [where_clause]
說明:
tbname : 表名稱
where_clause : where條件(可選)
刪除所有數據:
-
> delete from students;
-
> select * from students;
-
>
刪除指定條件的數據:
-
> select * from students;
-
name: students
-
time score stuid
-
---- ----- -----
-
1488820352594964019 89 s123
-
1488820356463338534 79 s123
-
-
-
> delete from students where stuid='s123' and time=1488820352594964019;
-
> select * from students;
-
name: students
-
time score stuid
-
---- ----- -----
-
1488820356463338534 79 s123
-
-
>
其它
- 控制台執行單次查詢
格式:
influx -execute '<query>'
類似 mysql -e 的功能,示例代碼如下:
-
[root@localhost ~] # influx -execute 'show databases'
-
name: databases
-
name
-
----
-
_internal
-
testdb
-
-
[root@localhost ~] #
- 指定查詢結果以csv或json格式輸出
格式:
influx -format=[format]
說明:
format : 啟動格式,支持column,csv,json三種格式,默認為column
示例如下:
-
[root@localhost ~] # influx -format=csv
-
Visit https: //enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
-
Connected to http: //localhost:8086 version 1.1.0
-
InfluxDB shell version: 1.1.0
-
> show databases;
-
name,name
-
databases,_internal
-
databases,testdb
-
> exit
-
[root@localhost ~] # influx -format=json
-
Visit https: //enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
-
Connected to http: //localhost:8086 version 1.1.0
-
InfluxDB shell version: 1.1.0
-
> show databases;
-
{ "results":[{"series":[{"name":"databases","columns":["name"],"values":[["_internal"],["testdb"]]}]}]}
-
> exit
-
[root@localhost ~] # influx -format=json -pretty
-
Visit https: //enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
-
Connected to http: //localhost:8086 version 1.1.0
-
InfluxDB shell version: 1.1.0
-
> show databases;
-
{
-
"results": [
-
{
-
"series": [
-
{
-
"name": "databases",
-
"columns": [
-
"name"
-
],
-
"values": [
-
[
-
"_internal"
-
],
-
[
-
"testdb"
-
]
-
]
-
}
-
]
-
}
-
]
-
}
-
>
- 用戶管理
可以直接在web管理頁面做操作,也可以命令行。
-
#顯示用戶
-
show users
-
#創建用戶
-
create user "username" with password 'password'
-
#創建管理員權限用戶create user "username" with password 'password' with all privileges
-
#刪除用戶
-
drop user "username"
- 連續查詢(Continous Queries)
當數據超過保存策略里指定的時間之后就會被刪除,但是這時候可能並不想數據被完全刪掉,怎么辦?
influxdb提供了聯系查詢,可以做數據統計采樣。- 查看數據庫的Continous Queries
show continuous queries
- 創建新的Continous Queries
create continous query cq_name on db_name begin select sum(count) into new_table_name from table_name group by time(30m) end
-
- cq_name:連續查詢名字;
-
- db_name:數據庫名字;
-
- sum(count):計算總和;
-
- table_name:當前表名;
-
- new_table_name:存新的數據的表名;
-
- 30m:時間間隔為30分鍾
- 刪除Continous Queries
drop continous query cp_name on db_name
-
數據保存策略(Retention Policies)
influxDB是沒有提供直接刪除數據記錄的方法,但是提供數據保存策略,主要用於指定數據保留時間,超過指定時間,就刪除這部分數據。- 查看當前數據庫Retention Policies
show retention policies on "db_name"
- 創建新的Retention Policies
- 修改Retention Policies
-
alter retention policy "rp_name" on "db_name" duration 30d default
-
create retention policy "rp_name" on "db_name" duration 3w replication 1 default
-
- rp_name:策略名;
-
- db_name:具體的數據庫名;
-
- 3w:保存3周,3周之前的數據將被刪除,influxdb具有各種事件參數,比如:h(小時),d(天),w(星期);
-
- replication 1:副本個數,一般為1就可以了;
-
- default:設置為默認策略
-
- 刪除Retention Policies
drop retention policy "rp_name"
- 數據庫與表的操作
可以直接在web管理頁面做操作,當然也可以命令行。
-
#創建數據庫
-
create database "db_name"
-
#顯示所有的數據庫
-
show databases
-
#刪除數據庫
-
drop database "db_name"
-
#使用數據庫
-
use db_name
-
#顯示該數據庫中所有的表
-
show measurements
-
#創建表,直接在插入數據的時候指定表名
-
insert test,host=127.0.0.1,monitor_name=test count=1
-
#刪除表
-
drop measurement "measurement_name"