參考文檔
InfluxDB簡介
InfluxDB 是一個(gè)時(shí)間序列數(shù)據(jù)庫,旨在處理高寫入和查詢負(fù)載漂问。它是TICK堆棧的組成部分 萍启。InfluxDB 旨在用作涉及大量帶時(shí)間戳數(shù)據(jù)的任何用例的后備存儲,包括 DevOps 監(jiān)控肋杖,應(yīng)用程序指標(biāo)溉仑,物聯(lián)網(wǎng)傳感器數(shù)據(jù)和實(shí)時(shí)分析。
主要特點(diǎn)
以下是 InfluxDB 目前支持的一些功能状植,使其成為處理時(shí)間序列數(shù)據(jù)的絕佳選擇浊竟。
- 專為時(shí)間序列數(shù)據(jù)編寫的自定義高性能數(shù)據(jù)存儲。TSM 引擎允許高攝取速度和數(shù)據(jù)壓縮津畸。
- 完全寫在 Go振定。它編譯成單個(gè)二進(jìn)制文件,沒有外部依賴項(xiàng)肉拓。
- 簡單后频,高性能的寫入和查詢 HTTP API。
- 插件支持其他數(shù)據(jù)提取協(xié)議暖途,如 Graphite卑惜,collectd 和 OpenTSDB。
- 專為類似 SQL 的查詢語言量身定制丧肴,可輕松查詢聚合數(shù)據(jù)残揉。
- 標(biāo)簽允許對系列進(jìn)行索引以實(shí)現(xiàn)快速有效的查詢。
- 保留策略有效地自動(dòng)使過時(shí)數(shù)據(jù)過期芋浮。
- 連續(xù)查詢自動(dòng)計(jì)算聚合數(shù)據(jù)抱环,以提高頻繁查詢的效率。
InfluxDB 的開源版本在單個(gè)節(jié)點(diǎn)上運(yùn)行纸巷。如果您需要高可用性來消除單點(diǎn)故障镇草,請考慮InfluxDB企業(yè)版。
InfluxDB默認(rèn)使用以下網(wǎng)絡(luò)端口:
- TCP 端口
8086
用于通過 InfluxDB 的 HTTP API 進(jìn)行客戶端 - 服務(wù)器通信 - TCP 端口
8088
用于 RPC 服務(wù)以進(jìn)行備份和還原
除了上面的端口瘤旨,InfluxDB 還提供了多個(gè)可能需要自定義端口的插件梯啤。可以通過配置文件修改所有端口映射存哲,配置文件位于 /etc/influxdb/influxdb.conf
默認(rèn)安裝位置因宇。
使用網(wǎng)絡(luò)時(shí)間協(xié)議(NTP)用于保證寫入InfluxDB的時(shí)間準(zhǔn)確
在安裝部署 InfluxDB 的服務(wù)器首先要保證好服務(wù)器的 UTC 本地時(shí)間準(zhǔn)確七婴,不然寫入的時(shí)間序列數(shù)據(jù)肯定有誤。
為了保證時(shí)間準(zhǔn)確察滑,最好開啟 NTP 服務(wù)進(jìn)行時(shí)鐘同步打厘。
使用docker啟動(dòng)InfluxDB
下載鏡像:docker pull influxdb
[root@server81 influxdb]# docker pull influxdb
Using default tag: latest
Trying to pull repository docker.io/library/influxdb ...
latest: Pulling from docker.io/library/influxdb
cd8eada9c7bb: Pull complete
c2677faec825: Pull complete
fcce419a96b1: Pull complete
7eb470cb34c6: Pull complete
5d32b89f3e91: Pull complete
c06b59b518ce: Pull complete
59c5fc9b8822: Pull complete
a6b36405d3fd: Pull complete
Digest: sha256:2c1dba8c2b9713355edce4a9824a10226be0d4ace071ecb663224a4d29ae8403
Status: Downloaded newer image for docker.io/influxdb:latest
[root@server81 influxdb]#
編寫啟動(dòng)influxdb
服務(wù)腳本:
[root@server81 influxdb]# cat restart_influxdb.sh
#!/bin/bash
basedir=$(cd `dirname $0`;pwd)
docker stop influxdb
docker rm influxdb
docker run -d --name influxdb -p 8086:8086 -v $basedir/influxdb:/var/lib/influxdb influxdb
[root@server81 influxdb]#
執(zhí)行啟動(dòng)influxdb服務(wù):
## 執(zhí)行shell腳本重啟服務(wù)
[root@server81 influxdb]# ./restart_influxdb.sh
influxdb
influxdb
d2918dc47850c94f0532e41465e8943c5e45c30c1884b529939bd49cd8c30263
[root@server81 influxdb]#
[root@server81 influxdb]# ls
influxdb restart_influxdb.sh
[root@server81 influxdb]#
[root@server81 influxdb]# ls influxdb/
data meta wal
[root@server81 influxdb]#
登陸influxdb的CLI界面
## 查看docker啟動(dòng)進(jìn)程
[root@server81 influxdb]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2918dc47850 influxdb "/entrypoint.sh in..." 17 minutes ago Up 17 minutes 0.0.0.0:8086->8086/tcp influxdb
82a294241ff7 registry:2 "/entrypoint.sh /e..." 4 weeks ago Up 2 days 0.0.0.0:5000->5000/tcp registry
[root@server81 influxdb]#
## 進(jìn)入influxdb容器內(nèi)
[root@server81 influxdb]# docker exec -it influxdb bash
root@d2918dc47850:/#
root@d2918dc47850:/#
執(zhí)行 influx 將啟動(dòng) CLI 并自動(dòng)連接到本地 InfluxDB 實(shí)例(假設(shè)您已經(jīng)啟動(dòng)了服務(wù)器 service influxdb start 或influxd 直接運(yùn)行)。輸出應(yīng)如下所示:
[root@server81 influxdb]# docker exec -it influxdb bash
root@d2918dc47850:/#
root@d2918dc47850:/# influx
Connected to http://localhost:8086 version 1.7.2
InfluxDB shell version: 1.7.2
Enter an InfluxQL query
> exit
root@d2918dc47850:/#
root@d2918dc47850:/# influx -precision rfc3339
Connected to http://localhost:8086 version 1.7.2
InfluxDB shell version: 1.7.2
Enter an InfluxQL query
>
筆記:
- InfluxDB HTTP API
8086
默認(rèn)在端口上運(yùn)行贺辰。因此户盯,influx
將連接到端口8086
和localhost
默認(rèn)。如果您需要更改這些默認(rèn)值饲化,請運(yùn)行influx --help
查看相關(guān)幫助莽鸭。- 使用
-precision
參數(shù)指定任何返回的時(shí)間戳的格式/精度。在上面的示例中吃靠,rfc3339
告訴InfluxDB以RFC3339格式(YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ
)返回時(shí)間戳硫眨。
退出的方式,輸入exit
現(xiàn)在撩笆,命令行已準(zhǔn)備好以 Influx 查詢語言(又稱 InfluxQL )語句的形式獲取輸入捺球。要退出 InfluxQL shell,請鍵入 exit
并單擊 return
夕冲。
創(chuàng)建InfluxDB數(shù)據(jù)庫
全新安裝的 InfluxDB 沒有數(shù)據(jù)庫(除了系統(tǒng)
_internal
)氮兵,因此創(chuàng)建一個(gè)是我們的首要任務(wù)。您可以使用CREATE DATABASE <db-name>InfluxQL
語句創(chuàng)建數(shù)據(jù)庫歹鱼,其中<db-name>
是您要?jiǎng)?chuàng)建的數(shù)據(jù)庫的名稱泣栈。只要數(shù)據(jù)庫的名稱是雙引號的字符串,就可以包含任何unicode
字符弥姻。如果名稱僅包含ASCII
字母南片,數(shù)字或下劃線且不以數(shù)字開頭,則也可以不加引號庭敦。
創(chuàng)建一個(gè)數(shù)據(jù)庫疼进,名稱為mydb
:
> CREATE DATABASE mydb
>
注意:按 Enter 鍵后,將出現(xiàn)一個(gè)新提示秧廉,并且不顯示任何其他提示伞广。在 CLI 中,這意味著語句已執(zhí)行且沒有要顯示的錯(cuò)誤疼电。如果出現(xiàn)問題嚼锄,將始終顯示錯(cuò)誤。沒有消息就是好消息蔽豺!
現(xiàn)在已經(jīng)創(chuàng)建了mydb 數(shù)據(jù)庫区丑,可以 SHOW DATABASES 語句顯示所有現(xiàn)有數(shù)據(jù)庫:
root@d2918dc47850:/# influx -precision rfc3339
Connected to http://localhost:8086 version 1.7.2
InfluxDB shell version: 1.7.2
Enter an InfluxQL query
>
> CREATE DATABASE mydb
>
> show databases
name: databases
name
----
_internal
mydb
>
>
> SHOW DATABASES
name: databases
name
----
_internal
mydb
>
注意: 該
_internal
數(shù)據(jù)庫中創(chuàng)建和使用InfluxDB
存儲內(nèi)部運(yùn)行指標(biāo)。稍后再查看以了解InfluxDB
如何在幕后執(zhí)行。
設(shè)置使用哪個(gè)數(shù)據(jù)庫 use <db-name>
在使用SHOW DATABASES
查看了有哪些數(shù)據(jù)庫之后沧侥,就可以使用USE <db-name>
來設(shè)置將要使用的數(shù)據(jù)庫可霎,自動(dòng)為將來的所有請求設(shè)置數(shù)據(jù)庫。例如:
> SHOW DATABASES
name: databases
name
----
_internal
mydb
>
> use mydb
Using database mydb
>
現(xiàn)在正什,將來的命令只會(huì)針對mydb
數(shù)據(jù)庫運(yùn)行啥纸。
寫入數(shù)據(jù)到數(shù)據(jù)庫中
現(xiàn)在我們有了一個(gè)數(shù)據(jù)庫,
InfluxDB
已準(zhǔn)備好接受查詢和寫入婴氮。
InfluxDB
中的數(shù)據(jù)按“時(shí)間序列”進(jìn)行組織,其中包含測量值,如“cpu_load
”或“temperature
”盾致。
時(shí)間序列的數(shù)據(jù)存在多個(gè)時(shí)間點(diǎn)測量值都是零的情況主经,部分存在測量值。
其中每個(gè)時(shí)間點(diǎn)的數(shù)據(jù)包含了時(shí)間戳和測量值(例如"cpu_load
")庭惜,并且至少有一個(gè)鍵值field
對應(yīng)罩驻,例如cpu_load = 21.2
。
大體格式可以簡單理解這樣 :時(shí)間 cpu_load = 21.2
护赊。
另外還有一種鍵值標(biāo)簽tags
的數(shù)據(jù)惠遏,例如 "host=server01
","region=EMEA
"等。
從概念上講骏啰,您可以將存儲測量值數(shù)據(jù)表格視為一個(gè)SQL表节吮,其中主索引始終是時(shí)間。
tags
并且fields
是表中的有效列判耕。tags
可以是索引透绩,而fields
則無法設(shè)置為索引。相對于SQL表不同之處在于壁熄,使用InfluxDB
帚豪,您可以進(jìn)行數(shù)百萬次測量,您不必預(yù)先定義表格草丧,也不會(huì)存儲空值狸臣。
使用線路協(xié)議(Line Protocol
)將點(diǎn)寫入InfluxDB,其遵循以下格式:
<measurement>[,<tag-key>=<tag-value>...] <field-key>=<field-value>[,<field2-key>=<field2-value>...] [unix-nano-timestamp]
以下幾行都是可以寫入 InfluxDB 的點(diǎn)的示例:
cpu,host=serverA,region=us_west value=0.64
payment,device=mobile,product=Notepad,method=credit billed=33,licenses=3i 1434067467100293230
stock,symbol=AAPL bid=127.46,ask=127.48
temperature,machine=unit42,type=assembly external=25,internal=37 1434067467000000000
注意:有關(guān)線路協(xié)議的更多信息昌执,請參見“ 語法”頁面烛亦。
要使用 CLI 將單個(gè)時(shí)間序列數(shù)據(jù)點(diǎn)插入 InfluxDB,請輸入 INSERT 后跟一個(gè)點(diǎn):
root@d2918dc47850:/# influx
Connected to http://localhost:8086 version 1.7.2
InfluxDB shell version: 1.7.2
Enter an InfluxQL query
> show databases
name: databases
name
----
_internal
mydb
>
> use mydb
Using database mydb
>
> INSERT cpu,host=serverA,region=us_west value=0.64
>
INSERT cpu,host=serverA,region=us_west value=0.64
說明:測量名稱為cpu
和標(biāo)記(tag
)的點(diǎn)host仙蚜,region
現(xiàn)在已經(jīng)寫入數(shù)據(jù)庫此洲,測量value
值為0.64
。
查詢剛才寫的數(shù)據(jù):
> SELECT "host", "region", "value" FROM "cpu"
name: cpu
time host region value
---- ---- ------ -----
1546688002825475579 serverA us_west 0.64
>
可以看出測量名稱cpu
就是SQL
的表名委粉,其他就是字段名稱呜师。這樣就更加好理解了。
注意:在插入數(shù)據(jù)的時(shí)候贾节,并沒有提供時(shí)間戳汁汗。如果沒有為某個(gè)點(diǎn)提供時(shí)間戳衷畦,InfluxDB 會(huì)在獲取該點(diǎn)時(shí)分配本地當(dāng)前時(shí)間戳。
這個(gè)時(shí)間戳就類型 mysql 中的自增長id知牌。
讓我們嘗試存儲另一種類型的數(shù)據(jù)祈争,在同一測量中有兩個(gè)字段:
> INSERT temperature,machine=unit42,type=assembly external=25,internal=37
>
查詢剛剛插入temperature
的所有字段和標(biāo)記,您可以使用*
運(yùn)算符:
> select * from temperature
name: temperature
time external internal machine type
---- -------- -------- ------- ----
1546688551508705124 25 37 unit42 assembly
>
警告: 在大型數(shù)據(jù)庫上使用*不帶LIMIT子句可能會(huì)導(dǎo)致性能問題角寸。您可以使用Ctrl+C取消響應(yīng)時(shí)間過長的查詢菩混。
## 再次插入多條數(shù)據(jù)
> INSERT temperature,machine=unit42,type=assembly external=25,internal=37
> INSERT temperature,machine=unit42,type=assembly external=25,internal=37
> INSERT temperature,machine=unit42,type=assembly external=25,internal=37
> INSERT temperature,machine=unit42,type=assembly external=25,internal=37
> INSERT temperature,machine=unit42,type=assembly external=25,internal=37
> INSERT temperature,machine=unit42,type=assembly external=25,internal=37
>
> select * from temperature
name: temperature
time external internal machine type
---- -------- -------- ------- ----
1546688551508705124 25 37 unit42 assembly
1546688749536654124 25 37 unit42 assembly
1546688750174577918 25 37 unit42 assembly
1546688750737028280 25 37 unit42 assembly
1546688751229687907 25 37 unit42 assembly
1546688751772543015 25 37 unit42 assembly
1546688752401279324 25 37 unit42 assembly
## 使用limit查詢,基本跟SQL寫法一致
> select * from temperature limit 3
name: temperature
time external internal machine type
---- -------- -------- ------- ----
1546688551508705124 25 37 unit42 assembly
1546688749536654124 25 37 unit42 assembly
1546688750174577918 25 37 unit42 assembly
>
InfluxQL有許多功能和關(guān)鍵字扁藕,這里沒有涉及沮峡,包括對Go風(fēng)格的正則表達(dá)式的支持。例如:
執(zhí)行使用正則匹配所有數(shù)據(jù) SELECT * FROM /.*/ LIMIT 1
> SELECT * FROM /.*/
name: cpu
time external host internal machine region type value
---- -------- ---- -------- ------- ------ ---- -----
1546688002825475579 serverA us_west 0.64
name: temperature
time external host internal machine region type value
---- -------- ---- -------- ------- ------ ---- -----
1546688551508705124 25 37 unit42 assembly
1546688749536654124 25 37 unit42 assembly
1546688750174577918 25 37 unit42 assembly
1546688750737028280 25 37 unit42 assembly
1546688751229687907 25 37 unit42 assembly
1546688751772543015 25 37 unit42 assembly
1546688752401279324 25 37 unit42 assembly
>
## 如果要看有哪些表格數(shù)據(jù)亿柑,可以使用這句進(jìn)行查看邢疙。
> SELECT * FROM /.*/ limit 1
name: cpu
time external host internal machine region type value
---- -------- ---- -------- ------- ------ ---- -----
1546688002825475579 serverA us_west 0.64
name: temperature
time external host internal machine region type value
---- -------- ---- -------- ------- ------ ---- -----
1546688551508705124 25 37 unit42 assembly
>
執(zhí)行where語句條件判斷:select * from "temperature" where "internal" > 36
> select * from "temperature" where "internal" > 36
name: temperature
time external internal machine type
---- -------- -------- ------- ----
1546688551508705124 25 37 unit42 assembly
1546688749536654124 25 37 unit42 assembly
1546688750174577918 25 37 unit42 assembly
1546688750737028280 25 37 unit42 assembly
1546688751229687907 25 37 unit42 assembly
1546688751772543015 25 37 unit42 assembly
1546688752401279324 25 37 unit42 assembly
>
> select * from "temperature" where "internal" > 38
>
這就是將數(shù)據(jù)寫入InfluxDB并進(jìn)行查詢所需要知道的全部內(nèi)容。要了解有關(guān)InfluxDB寫入?yún)f(xié)議的更多信息望薄,請查看寫入數(shù)據(jù)指南疟游。要進(jìn)一步探索查詢語言,請查看查詢數(shù)據(jù)指南痕支。有關(guān)InfluxDB概念的更多信息颁虐,請查看“ 關(guān)鍵概念”頁面。
參考文檔