這里記錄下influx控制台的簡單使用,如需更多功能請參考InfluxDB官方文檔: https://docs.influxdata.com/influxdb/v1.1/
環境: CentOS6.5_x64
InfluxDB版本:1.1.0
准備工作
- 啟動服務器
執行如下命令:
service influxdb start
示例如下:
[root@localhost ~]# service influxdb start
Starting influxdb...
influxdb process was started [ OK ]
[root@localhost ~]#
- 啟動控制台客戶端
在控制台執行influx即可啟動InfluxDB cli,示例如下:
[root@localhost ~]# influx 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 >
influx控制台基本操作
數據庫操作
- 顯示已存在的所有數據庫
格式: 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" ] ] } ] } ] } >
好,就這些了,希望對你有幫助。
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170307_使用influx控制台工具操作InfluxDB.md
歡迎補充