轉(zhuǎn)載自:https://www.cnblogs.com/jackyroc/p/7677508.html
influxdb是目前比較流行的時間序列數(shù)據(jù)庫。
- 何謂時間序列數(shù)據(jù)庫?
什么是時間序列數(shù)據(jù)庫,最簡單的定義就是數(shù)據(jù)格式里包含Timestamp字段的數(shù)據(jù),比如某一時間環(huán)境的溫度,CPU的使用率等。但是,有什么數(shù)據(jù)不包含Timestamp呢?幾乎所有的數(shù)據(jù)其實都可以打上一個Timestamp字段。時間序列數(shù)據(jù)的更重要的一個屬性是如何去查詢它,包括數(shù)據(jù)的過濾,計算等等。
Influxdb
Influxdb是一個開源的分布式時序、時間和指標(biāo)數(shù)據(jù)庫,使用go語言編寫,無需外部依賴。
它有三大特性:
- 時序性(Time Series):與時間相關(guān)的函數(shù)的靈活使用(諸如最大、最小、求和等);
- 度量(Metrics):對實時大量數(shù)據(jù)進(jìn)行計算;
- 事件(Event):支持任意的事件數(shù)據(jù),換句話說,任意事件的數(shù)據(jù)我們都可以做操作。
同時,它有以下幾大特點:
- schemaless(無結(jié)構(gòu)),可以是任意數(shù)量的列;
- min, max, sum, count, mean, median 一系列函數(shù),方便統(tǒng)計;
- Native HTTP API, 內(nèi)置http支持,使用http讀寫;
- Powerful Query Language 類似sql;
- Built-in Explorer 自帶管理工具。
Influxdb安裝
注:本文使用的influxdb version是1.0.2
在講解具體的安裝步驟之前,先說說influxdb的兩個http端口:8083和8086
- port 8083:管理頁面端口,訪問localhost:8083可以進(jìn)入你本機的influxdb管理頁面;
- port 8086:http連接influxdb client端口,一般使用該端口往本機的influxdb讀寫數(shù)據(jù)。
OS X
brew update
brew install influxdb
Docker Image
docker pull influxdb
Ubuntu & Debian
wget https://dl.influxdata.com/influxdb/releases/influxdb_1.0.2_amd64.deb
sudo dpkg -i influxdb_1.0.2_amd64.deb
RedHat & CentOS
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.0.2.x86_64.rpm
sudo yum localinstall influxdb-1.0.2.x86_64.rpm
Standalone Linux Binaries (64-bit)
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.0.2_linux_amd64.tar.gz
tar xvfz influxdb-1.0.2_linux_amd64.tar.gz
Standalone Linux Binaries (32-bit)
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.0.2_linux_i386.tar.gz
tar xvfz influxdb-1.0.2_linux_i386.tar.gz
Standalone Linux Binaries (ARM)
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.0.2_linux_armhf.tar.gz
tar xvfz influxdb-1.0.2_linux_armhf.tar.gz
How to start?
安裝完之后,如何啟動呢?
sudo service influxdb start
到這里influxdb安裝啟動完成,可以訪問influxdb管理頁面:本地管理頁面,該版本沒有登錄用戶及密碼,可以自行設(shè)置讀寫的用戶名和密碼。
如何在命令行使用
安裝完畢之后,如何在命令行使用呢?

influxdb基本操作
名詞解釋
在具體的講解influxdb的相關(guān)操作之前先說說influxdb的一些專有名詞,這些名詞代表什么。
influxdb相關(guān)名詞
- database:數(shù)據(jù)庫;
- measurement:數(shù)據(jù)庫中的表;
- points:表里面的一行數(shù)據(jù)。
influxDB中獨有的一些概念
Point由時間戳(time)、數(shù)據(jù)(field)和標(biāo)簽(tags)組成。
- time:每條數(shù)據(jù)記錄的時間,也是數(shù)據(jù)庫自動生成的主索引;
- fields:各種記錄的值;
- tags:各種有索引的屬性。
還有一個重要的名詞:series
所有在數(shù)據(jù)庫中的數(shù)據(jù),都需要通過圖表來表示,series表示這個表里面的所有的數(shù)據(jù)可以在圖標(biāo)上畫成幾條線(注:線條的個數(shù)由tags排列組合計算出來)
舉個簡單的小栗子:
有如下數(shù)據(jù):

它的series為:

influxdb基本操作
-
數(shù)據(jù)庫與表的操作
可以直接在web管理頁面做操作,當(dāng)然也可以命令行。#創(chuàng)建數(shù)據(jù)庫 create database "db_name" #顯示所有的數(shù)據(jù)庫 show databases #刪除數(shù)據(jù)庫 drop database "db_name" #使用數(shù)據(jù)庫 use db_name #顯示該數(shù)據(jù)庫中所有的表 show measurements #創(chuàng)建表,直接在插入數(shù)據(jù)的時候指定表名 insert test,host=127.0.0.1,monitor_name=test count=1 #刪除表 drop measurement "measurement_name" -
增
向數(shù)據(jù)庫中插入數(shù)據(jù)。-
通過命令行
use testDb insert test,host=127.0.0.1,monitor_name=test count=1 -
通過http接口
curl -i -XPOST 'http://127.0.0.1:8086/write?db=testDb' --data-binary 'test,host=127.0.0.1,monitor_name=test count=1'
讀者看到這里可能會觀察到插入的數(shù)據(jù)的格式貌似比較奇怪,這是因為influxDB存儲數(shù)據(jù)采用的是Line Protocol格式。那么何謂Line Protoco格式?
Line Protocol格式:寫入數(shù)據(jù)庫的Point的固定格式。
在上面的兩種插入數(shù)據(jù)的方法中都有這樣的一部分:test,host=127.0.0.1,monitor_name=test count=1其中:
- test:表名;
- host=127.0.0.1,monitor_name=test:tag;
- count=1:field
想對此格式有詳細(xì)的了解參見官方文檔
-
-
查
查詢數(shù)據(jù)庫中的數(shù)據(jù)。-
通過命令行
select * from test order by time desc -
通過http接口
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testDb" --data-urlencode "q=select * from test order by time desc"
influxDB是支持類sql語句的,具體的查詢語法都差不多,這里就不再做詳細(xì)的贅述了。
-
-
數(shù)據(jù)保存策略(Retention Policies)
influxDB是沒有提供直接刪除數(shù)據(jù)記錄的方法,但是提供數(shù)據(jù)保存策略,主要用于指定數(shù)據(jù)保留時間,超過指定時間,就刪除這部分?jǐn)?shù)據(jù)。-
查看當(dāng)前數(shù)據(jù)庫Retention Policies
show retention policies on "db_name"image -
創(chuàng)建新的Retention Policies
create retention policy "rp_name" on "db_name" duration 3w replication 1 default- rp_name:策略名;
- db_name:具體的數(shù)據(jù)庫名;
- 3w:保存3周,3周之前的數(shù)據(jù)將被刪除,influxdb具有各種事件參數(shù),比如:h(小時),d(天),w(星期);
- replication 1:副本個數(shù),一般為1就可以了;
- default:設(shè)置為默認(rèn)策略
-
修改Retention Policies
alter retention policy "rp_name" on "db_name" duration 30d default -
刪除Retention Policies
drop retention policy "rp_name"
-
-
連續(xù)查詢(Continous Queries)
當(dāng)數(shù)據(jù)超過保存策略里指定的時間之后就會被刪除,但是這時候可能并不想數(shù)據(jù)被完全刪掉,怎么辦?
influxdb提供了聯(lián)系查詢,可以做數(shù)據(jù)統(tǒng)計采樣。-
查看數(shù)據(jù)庫的Continous Queries
show continuous queriesimage -
創(chuàng)建新的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:連續(xù)查詢名字;
- db_name:數(shù)據(jù)庫名字;
- sum(count):計算總和;
- table_name:當(dāng)前表名;
- new_table_name:存新的數(shù)據(jù)的表名;
- 30m:時間間隔為30分鐘
-
刪除Continous Queries
drop continous query cp_name on db_name
-
-
用戶管理
可以直接在web管理頁面做操作,也可以命令行。#顯示用戶 show users #創(chuàng)建用戶 create user "username" with password 'password' #創(chuàng)建管理員權(quán)限用戶create user "username" with password 'password' with all privileges #刪除用戶 drop user "username
influxdb基本操作
名詞解釋
在具體的講解influxdb的相關(guān)操作之前先說說influxdb的一些專有名詞,這些名詞代表什么。
influxdb相關(guān)名詞
- database:數(shù)據(jù)庫;
- measurement:數(shù)據(jù)庫中的表;
- points:表里面的一行數(shù)據(jù)。
influxDB中獨有的一些概念
Point由時間戳(time)、數(shù)據(jù)(field)和標(biāo)簽(tags)組成。
- time:每條數(shù)據(jù)記錄的時間,也是數(shù)據(jù)庫自動生成的主索引;
- fields:各種記錄的值;
- tags:各種有索引的屬性。
還有一個重要的名詞:series
所有在數(shù)據(jù)庫中的數(shù)據(jù),都需要通過圖表來表示,series表示這個表里面的所有的數(shù)據(jù)可以在圖標(biāo)上畫成幾條線(注:線條的個數(shù)由tags排列組合計算出來)
舉個簡單的小栗子:
有如下數(shù)據(jù):

它的series為:

influxdb基本操作
-
數(shù)據(jù)庫與表的操作
可以直接在web管理頁面做操作,當(dāng)然也可以命令行。#創(chuàng)建數(shù)據(jù)庫 create database "db_name" #顯示所有的數(shù)據(jù)庫 show databases #刪除數(shù)據(jù)庫 drop database "db_name" #使用數(shù)據(jù)庫 use db_name #顯示該數(shù)據(jù)庫中所有的表 show measurements #創(chuàng)建表,直接在插入數(shù)據(jù)的時候指定表名 insert test,host=127.0.0.1,monitor_name=test count=1 #刪除表 drop measurement "measurement_name" -
增
向數(shù)據(jù)庫中插入數(shù)據(jù)。-
通過命令行
use testDb insert test,host=127.0.0.1,monitor_name=test count=1 -
通過http接口
curl -i -XPOST 'http://127.0.0.1:8086/write?db=testDb' --data-binary 'test,host=127.0.0.1,monitor_name=test count=1'
讀者看到這里可能會觀察到插入的數(shù)據(jù)的格式貌似比較奇怪,這是因為influxDB存儲數(shù)據(jù)采用的是Line Protocol格式。那么何謂Line Protoco格式?
Line Protocol格式:寫入數(shù)據(jù)庫的Point的固定格式。
在上面的兩種插入數(shù)據(jù)的方法中都有這樣的一部分:test,host=127.0.0.1,monitor_name=test count=1其中:
- test:表名;
- host=127.0.0.1,monitor_name=test:tag;
- count=1:field
想對此格式有詳細(xì)的了解參見官方文檔
-
-
查
查詢數(shù)據(jù)庫中的數(shù)據(jù)。-
通過命令行
select * from test order by time desc -
通過http接口
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testDb" --data-urlencode "q=select * from test order by time desc"
influxDB是支持類sql語句的,具體的查詢語法都差不多,這里就不再做詳細(xì)的贅述了。
-
-
數(shù)據(jù)保存策略(Retention Policies)
influxDB是沒有提供直接刪除數(shù)據(jù)記錄的方法,但是提供數(shù)據(jù)保存策略,主要用于指定數(shù)據(jù)保留時間,超過指定時間,就刪除這部分?jǐn)?shù)據(jù)。-
查看當(dāng)前數(shù)據(jù)庫Retention Policies
show retention policies on "db_name"image -
創(chuàng)建新的Retention Policies
create retention policy "rp_name" on "db_name" duration 3w replication 1 default- rp_name:策略名;
- db_name:具體的數(shù)據(jù)庫名;
- 3w:保存3周,3周之前的數(shù)據(jù)將被刪除,influxdb具有各種事件參數(shù),比如:h(小時),d(天),w(星期);
- replication 1:副本個數(shù),一般為1就可以了;
- default:設(shè)置為默認(rèn)策略
-
修改Retention Policies
alter retention policy "rp_name" on "db_name" duration 30d default -
刪除Retention Policies
drop retention policy "rp_name"
-
-
連續(xù)查詢(Continous Queries)
當(dāng)數(shù)據(jù)超過保存策略里指定的時間之后就會被刪除,但是這時候可能并不想數(shù)據(jù)被完全刪掉,怎么辦?
influxdb提供了聯(lián)系查詢,可以做數(shù)據(jù)統(tǒng)計采樣。-
查看數(shù)據(jù)庫的Continous Queries
show continuous queriesimage -
創(chuàng)建新的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:連續(xù)查詢名字;
- db_name:數(shù)據(jù)庫名字;
- sum(count):計算總和;
- table_name:當(dāng)前表名;
- new_table_name:存新的數(shù)據(jù)的表名;
- 30m:時間間隔為30分鐘
-
刪除Continous Queries
drop continous query cp_name on db_name
-
-
用戶管理
可以直接在web管理頁面做操作,也可以命令行。#顯示用戶 show users #創(chuàng)建用戶 create user "username" with password 'password' #創(chuàng)建管理員權(quán)限用戶create user "username" with password 'password' with all privileges #刪除用戶 drop user "username

