介紹
influxdb是使用GO編寫的基於時間序列的數據庫,用於存儲大量帶有時間戳的數據,報錯DevOps監控,日誌數據,應用程序的指標、數據分析數據等等。通過influxdb自動保存數據,你不需要刪除和清理,只需要定義一段時間DB會幫你自動清理。
特點
- 每秒鐘可以處理幾百萬個數據點。
- 長時間內產生大量數據它也會爲你進行壓縮處理使空間最小化。可以保存近期高精度數據和長期採樣數據,也就是之前說的持續查詢和保留策略,保證了自動化採樣和舊數據自動過期。
- 支持多樣的數據提取插件,例如:graphite,collected,openTSD。
其它信息
influxdb默認端口8086,默認是http協議接口,使用起來簡單方便
安裝
下載安裝包:wget 'http://dl.influxdata.com/influxdb/nightlies/influxdb-nightly_darwin_amd64.tar.gz'
解壓安裝包:tar zxf influxdb-nightly_darwin_amd64.tar.gz
啓動influxdb:cd influxdb-1.7.0~n201811230800-0/ && ./usr/bin/influxd
登錄數據庫:./usr/bin/influx
查看幫助:
> help
Usage:
connect <host:port> connects to another node specified by host:port
auth prompts for username and password
pretty toggles pretty print for the json format
chunked turns on chunked responses from server
chunk size <size> sets the size of the chunked responses. Set to 0 to reset to the default chunked size
use <db_name> sets current database
format <format> specifies the format of the server responses: json, csv, or column
precision <format> specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns
consistency <level> sets write consistency level: any, one, quorum, or all
history displays command history
settings outputs the current settings for the shell
clear clears settings such as database or retention policy. run 'clear' for help
exit/quit/ctrl+d quits the influx shell
show databases show database names
show series show series information
show measurements show measurement information
show tag keys show tag key information
show field keys show field key information
A full list of influxql commands can be found at:
https://docs.influxdata.com/influxdb/latest/query_language/spec/
登錄Influxdb
influx -precision rfc3339
-precision rfc3339 #精準的指定參數,rfc3339時間格式
數據庫操作
現在influxdb已經搭建完成,可以進行入門操作,建庫、建表寫數據。當寫入一個時間序列數據時候他可能包含0到多個點,每個數據都對應一個監控樣本(例如cpu_load、溫度)至少包括一個鍵值對(cplu_load=5);
在這裏可以認爲:
- measurement就是一個表它提供時間索引。
- fields就是表中的列
-
tags是表中索引
在這裏和mysql不同是,你可以有幾百萬個measurements,而且n不需要提前定義表結構、也不會存儲空值;
1.創建一個數據庫
CREATE DATABASE {NAME};
> create database order_record;
> show databases;
name: databases
name
----
_internal
order_record
這時候我們發現數據庫有一個表“_internal”,其實這個表是influxdb數據庫的一些指標存儲庫。有點類似mysql數據庫的mysql庫。
2.寫數據
注意在寫數據的時候如果不添加時間戳,系統會默認添加一個時間
<measurement>[,<tag-key>=<tag-value>...] <field-key>=<field-value>[,<field2-key>=<field2-value>...] [unix-nano-timestamp]
> use order_record;
Using database order_record
> INSERT cpu,host=serverA,region=us_west value=0.64
> SELECT "host", "region", "value" FROM "cpu"
name: cpu
time host region value
---- ---- ------ -----
2018-11-23T16:01:29.995044Z serverA us_west 0.64
3.數據讀
同mysql一樣如果你想空值內容輸出行數,可以用 [limit 1~n;],同樣influxdb的sql也支持go語言格式的正則。
> select * from cpu;
name: cpu
time host region value
---- ---- ------ -----
2018-11-23T16:01:29.995044Z serverA us_west 0.64
> INSERT stock,symbol=AAPL bid=127.46,ask=127.48
> INSERT temperature,machine=unit42,type=assembly external=25,internal=37 1434067467000000000
> SELECT "host", "region", "value" FROM "cpu"
name: cpu
time host region value
---- ---- ------ -----
2018-11-23T16:01:29.995044Z serverA us_west 0.64
每個表輸出一行
SELECT * FROM /.*/ LIMIT 1
4.查看當前數據庫的分片
> SHOW SHARDS
name: _internal
id database retention_policy shard_group start_time end_time expiry_time owners
-- -------- ---------------- ----------- ---------- -------- ----------- ------
1 _internal monitor 1 2018-11-24T00:00:00Z 2018-11-25T00:00:00Z 2018-12-02T00:00:00Z
name: zabbix
id database retention_policy shard_group start_time end_time expiry_time owners
-- -------- ---------------- ----------- ---------- -------- ----------- ------
5 zabbix autogen 5 1969-12-29T00:00:00Z 1970-01-05T00:00:00Z 1970-01-05T00:00:00Z
4 zabbix autogen 4 1970-01-12T00:00:00Z 1970-01-19T00:00:00Z 1970-01-19T00:00:00Z
3 zabbix autogen 3 2018-11-19T00:00:00Z 2018-11-26T00:00:00Z 2018-11-26T00:00:00Z
http api調用influxdb
0.接口介紹
接口路徑 | 描述 |
---|---|
/debug/pprof | debug排查問題使用 |
/debug/requests | 使用這個請求監聽最近是否有請求 |
/debug/vars | 查詢influxdb收集到靜態信息 |
/ping | 檢測influxdb狀態 |
/query | 查詢數據接口(同時可以創建ku) |
/write | 寫入數據接口(一個已存在數據庫) |
狀態碼介紹:
- 2xx:服務請求正常
- 4xx:代表請求語法有問題
- 5xx:服務端出問題,導致超時等故障
1.創建數據庫
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb"
HTTP/1.1 200 OK
Content-Type: application/json
Request-Id: 5edd88a8-ef90-11e8-83cd-a0999b0f94e3
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.0~n201811230800
X-Request-Id: 5edd88a8-ef90-11e8-83cd-a0999b0f94e3
Date: Sat, 24 Nov 2018 02:26:38 GMT
Transfer-Encoding: chunked
{"results":[{"statement_id":0}]}
2.寫入數據
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.65 1434055564000000000'
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 1ae386c4-ef91-11e8-83d8-a0999b0f94e3
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.0~n201811230800
X-Request-Id: 1ae386c4-ef91-11e8-83d8-a0999b0f94e3
Date: Sat, 24 Nov 2018 02:31:53 GMT
3.寫入多個數據點
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257'
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 574f52a0-ef91-11e8-83d9-a0999b0f94e3
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.0~n201811230800
X-Request-Id: 574f52a0-ef91-11e8-83d9-a0999b0f94e3
Date: Sat, 24 Nov 2018 02:33:34 GMT
4.從文件導入數據庫
從文件導入時候建議不要超過5000條,如果超過請對文件進行切割,因爲http api的接口5s會超時,請求數據過多會導致數據無法確認是否成功。
文件cpu_data.txt內容如下:
cpu_load_short,host=server02 value=111
cpu_load_short,host=server02,region=us-west value=0.222 1543027130702900257
cpu_load_short,direction=in,host=server01,region=us-west value=111.222 1543027129702900257
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary @cpu_data.txt
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 4b2ed710-ef92-11e8-83e3-a0999b0f94e3
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.0~n201811230800
X-Request-Id: 4b2ed710-ef92-11e8-83e3-a0999b0f94e3
Date: Sat, 24 Nov 2018 02:40:24 GMT