前言
在我們很多應(yīng)用中會遇到有一種基于一系列時間的數(shù)據(jù)需要處理,通過時間的順序可以將這些數(shù)據(jù)點連成線,再通過數(shù)據(jù)統(tǒng)計后可以做成多緯度的報表,也可通過機器學(xué)習(xí)來實現(xiàn)數(shù)據(jù)的預(yù)測告警。而時序數(shù)據(jù)庫就是用于存放管理這種有著時間順序數(shù)據(jù)的,時序數(shù)據(jù)庫一般都支持時序數(shù)據(jù)的快速寫入、持久化、多緯度的聚合查詢等基本功能。
InfluxDB簡介
InfluxDB是一個基于時間序列數(shù)據(jù)而開發(fā)的高性能數(shù)據(jù)存儲平臺,它可以對時序數(shù)據(jù)進行高吞吐量的攝取、壓縮和實時查詢。InfluxDB是用Go語言編寫的,它會編譯成一個沒有外部依賴的二進制文件來運行,支持Java、JavaScript、c#等語言。InfluxDB支持類似SQL的查詢語言,同時還支持正則表達式、算術(shù)表達式和時間序列特定函數(shù)以加速數(shù)據(jù)的處理效率。如下是跟InfluxDB相關(guān)的網(wǎng)址:
InfluxDB官網(wǎng):https://www.influxdata.com/
InfluxDB官方文檔:https://docs.influxdata.com/influxdb/
InfluxDB官方下載:https://portal.influxdata.com/downloads
InfluxDB客戶端工具下載:https://docs.influxdata.com/influxdb/v1.6/tools/api_client_libraries/
特點:
- 無結(jié)構(gòu)(無模式):可以是任意數(shù)量的列
- 可以設(shè)置metric的保存時間
- 支持與時間有關(guān)的相關(guān)函數(shù)(如min、max、sum、count、mean、median等),方便統(tǒng)計
- 支持存儲策略:可以用于數(shù)據(jù)的刪改。(influxDB沒有提供數(shù)據(jù)的刪除與修改方法)
- 支持連續(xù)查詢:是數(shù)據(jù)庫中自動定時啟動的一組語句,和存儲策略搭配可以降低InfluxDB的系統(tǒng)占用量。
- 原生的HTTP支持,內(nèi)置HTTP API
- 支持類似sql語法
- 支持設(shè)置數(shù)據(jù)在集群中的副本數(shù)
- 支持定期采樣數(shù)據(jù),寫入另外的measurement,方便分粒度存儲數(shù)據(jù)。
- 自帶web管理界面,方便使用(登入方式:http://:8083)
InfluxDB操作
這里將會簡單的介紹下如何操作InfluxDB,通過這些操作基本也能滿足工作上的需要了。操作InfluxDB可以通過命令行工具,也可借助開源的客戶端工具,我這里使用的是一款名叫“InfluxDBStudio”基于C#編寫的開源工具。常用操作的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#顯示用戶 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" #創(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ù)的時候指定表名,其中test為表名 insert test,host=127.0.0.1,monitor_name=test count =1 #刪除表 drop measurement "measurement_name" #查詢數(shù)據(jù) select * from test order by time desc #查看當(dāng)前數(shù)據(jù)庫的數(shù)據(jù)保存策略(Retention Policies) show retention policies on "db_name" #創(chuàng)建新的數(shù)據(jù)保存策略 #rp_name:策略名 #db_name:具體的數(shù)據(jù)庫名; #3w:保存3周,3周之前的數(shù)據(jù)將被刪除,influxdb具有各種事件參數(shù),比如:h(小時),d(天),w(星期) #replication 1:副本個數(shù),一般為1就可以了 # default :設(shè)置為默認(rèn)策略 create retention policy "rp_name" on "db_name" duration 3w replication 1 default #修改數(shù)據(jù)保存策略 alter retention policy "rp_name" on "db_name" duration 30d default #刪除數(shù)據(jù)保存策略 drop retention policy "rp_name" #查看數(shù)據(jù)庫的連續(xù)查詢(Continous Queries) show continuous queries #創(chuàng)建新的連續(xù)查詢(Continous Queries) #cq_name:連續(xù)查詢名字 #db_name:數(shù)據(jù)庫名字 # sum ( count ):計算總和 #table_name:當(dāng)前表名 #new_table_name:存新的數(shù)據(jù)的表名 #30m:時間間隔為30分鐘 create continous query cq_name on db_name begin select sum ( count ) into new_table_name from table_name group by time (30m) end #刪除連續(xù)查詢 drop continous query cp_name on db_name |
實現(xiàn)
通過上述的介紹與操作我們已基本熟悉了InfluxDB數(shù)據(jù)庫,那么接下來咱們就一起來看看在ASP.NET Core2程序中如何來讀寫操作InfluxDB數(shù)據(jù)庫吧。而這里我們借助一個名為“InfluxData.Net”的第三方庫來與InfluxDB交互通訊,基于“InfluxData.Net”庫可以使得我們很簡單的完成數(shù)據(jù)庫的讀寫操作。在ASP.NET Core2項目中通過Nuget管理器引用“InfluxData.Net”類庫后,敲入實現(xiàn)代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using InfluxData.Net.Common.Enums; using InfluxData.Net.InfluxDb; using InfluxData.Net.InfluxDb.Models; using Microsoft.AspNetCore.Mvc; namespace WebApplication1.Controllers { public class InfoController : Controller { //聲明InfluxDbClient private InfluxDbClient clientDb; public InfoController() { //連接InfluxDb的API地址、賬號、密碼 var infuxUrl = "http://localhost:8086/" ; var infuxUser = "admin" ; var infuxPwd = "admin" ; //創(chuàng)建InfluxDbClient實例 clientDb = new InfluxDbClient(infuxUrl, infuxUser, infuxPwd, InfluxDbVersion.Latest); } /// <summary> /// 從InfluxDB中讀取數(shù)據(jù) /// </summary> public async void GetData() { //傳入查詢命令,支持多條 var queries = new [] { " SELECT * FROM Reading WHERE time> now() - 24h " }; var dbName = "code-hub" ; //從指定庫中查詢數(shù)據(jù) var response = await clientDb.Client.QueryAsync(queries, dbName); //得到Serie集合對象(返回執(zhí)行多個查詢的結(jié)果) var series = response.ToList(); //取出第一條命令的查詢結(jié)果,是一個集合 var list = series[0].Values; //從集合中取出第一條數(shù)據(jù) var info_model = list.FirstOrDefault(); } /// <summary> /// 往InfluxDB中寫入數(shù)據(jù) /// </summary> public async void AddData() { //基于InfluxData.Net.InfluxDb.Models.Point實體準(zhǔn)備數(shù)據(jù) var point_model = new Point() { Name = "Reading" , //表名 Tags = new Dictionary< string , object >() { { "Id" , 158} }, Fields = new Dictionary< string , object >() { { "Val" , "webInfo" } }, Timestamp = DateTime.UtcNow }; var dbName = "code-hub" ; //從指定庫中寫入數(shù)據(jù),支持傳入多個對象的集合 var response = await clientDb.Client.WriteAsync(point_model, dbName); } } } |
總結(jié)
1、InfluxDB是個專業(yè)的時序數(shù)據(jù)庫,通過時序庫可幫助我們更高效的處理應(yīng)用中的時序數(shù)據(jù)。
2、使用InfluxDB庫時需先了解該庫的一些特色功能,如數(shù)據(jù)保存策略、連續(xù)查詢等。
3、通過“InfluxData.Net”類庫可快速簡便的幫助我們在ASP.NET Core2程序中實現(xiàn)對InfluxDB的讀寫操作。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:http://www.cnblogs.com/Andre/p/9884051.html