介紹
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