InfluxDB Http API 與SDK API 測試
阿新 • • 發佈:2018-12-05
Http 類似Rest的介面使用起來很方便,Sdk的介面是原生的適合改造和理解內部實現的原理。具體怎麼用視情況而定,web端直接通過瀏覽器就可以訪問,可以快速檢視資料並且可以跨語言訪問,SDK基於nuget包的方式更適合擴充套件性的專案。
目錄
Http API
using System; namespace InfluxBD { /// <summary> /// InfluxDB時序資料庫操作HTTP客戶端 /// 參考:https://www.cnblogs.com/dehai/p/4887309.html /// </summary> public class HttpInfluxDBClient { string _baseAddress; string _username; string _password; /// <summary> /// 建構函式 /// </summary> /// <param name="baseAddress"></param> /// <param name="username"></param> /// <param name="password"></param> public HttpInfluxDBClient(string baseAddress, string username, string password) { this._baseAddress = baseAddress; this._username = username; this._password = password; } public string CreateDatabase(string database) { string sql = "CREATE DATABASE " + database; string url = _baseAddress + string.Format("/query?q={0}", sql); string result = HttpHelper.Post(url, sql, _username, _password); return result; } public string DeleteDatabase(string database) { string sql = "DROP DATABASE " + database; string url = _baseAddress + string.Format("/query?q={0}", sql); string result = HttpHelper.Post(url, sql, _username, _password); return result; } public string GetDatabases() { string sql = "SHOW DATABASES"; string url = _baseAddress + string.Format("/query?q={0}", sql); string result = HttpHelper.Get(url, _username, _password); return result; } /// <summary> /// 讀取資料 /// </summary> /// <param name="database"></param> /// <param name="sql"></param> /// <returns></returns> public string Query(string database, string sql) { string url = _baseAddress + string.Format("/query?db={0}&q={1}", database, sql); string result = HttpHelper.Get(url, _username, _password); return result; } /// <summary> /// 寫入資料 /// </summary> /// <param name="database"></param> /// <param name="sql">示例:test,tag=logs Field0=10,Field1=10,Field2=20</param> /// <returns></returns> public string Write(string database, string sql) { string url = _baseAddress + string.Format("/write?db={0}", database); string result = HttpHelper.Post(url, sql, _username, _password); return result; } } }
對於其他語言都是通用的,很方便。
Sdk API
using InfluxDB.Net; using InfluxDB.Net.Enums; using InfluxDB.Net.Infrastructure.Configuration; using InfluxDB.Net.Infrastructure.Influx; using InfluxDB.Net.Models; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Text; namespace InfluxBD.Sdk { public class InfluxDBSdkHelper { private InfluxDb _client; /// <summary> /// 操作狀態回撥 /// </summary> /// <param name="status"></param> public delegate void StatusCallBack(bool status); /// <summary> /// 操作結果回撥 /// </summary> /// <param name="json"></param> public delegate void ResultCallBack(string json); public InfluxDBSdkHelper(string baseUrl, string username, string password) { _client = new InfluxDb(baseUrl, username, password); } public async void CreateDatabase(string database, StatusCallBack statusCallBack) { InfluxDbApiResponse response = await _client.CreateDatabaseAsync(database); statusCallBack?.Invoke(response.Success); } public async void DeleteDatabase(string database, StatusCallBack statusCallBack) { InfluxDbApiResponse deleteResponse = await _client.DropDatabaseAsync(database); statusCallBack?.Invoke(deleteResponse.Success); } public async void GetDatabases(ResultCallBack resultCallBack) { List<Database> databases = await _client.ShowDatabasesAsync(); string result = Newtonsoft.Json.JsonConvert.SerializeObject(databases); resultCallBack?.Invoke(result); } public async void Write(string database, InfluxDB.Net.Models.Point point, StatusCallBack statusCallBack) { //基於InfluxData.Net.InfluxDb.Models.Point實體準備資料 //var point = new Point() //{ // Measurement = "logs",//表名 // Tags = new Dictionary<string, object>() // { // { "Id", 158} // }, // Fields = new Dictionary<string, object>() // { // { "Val", "webInfo" } // }, // Timestamp = DateTime.UtcNow //}; InfluxDbApiResponse writeResponse = await _client.WriteAsync(database, point); statusCallBack?.Invoke(writeResponse.Success); } public async void Query(string database,string sql, ResultCallBack resultCallBack) { List<Serie> series = await _client.QueryAsync(database, sql); string result = Newtonsoft.Json.JsonConvert.SerializeObject(series); resultCallBack?.Invoke(result); } } }
測試
以Http API為例,資料庫連線和賬號資訊:
儲存資料:表(test) 標籤(type,可以多個,任意型別) 欄位(Field0..,數值型別)
查詢資料:查詢和普通sql差不多,條件查詢可以去看官方文件
瀏覽器可以這樣訪問:
http://172.16.9.229:8086/query?u=admin&p=admin&db=rtvsweb&q=show%20databases
http://172.16.9.229:8086/query?u=admin&p=admin&db=rtvsweb&q=select%20*%20from%20logs
注:因為本地是window10上安裝的Docker每次開機後需要重啟Docker,我的influxdb是安裝在docker裡面的,懶得啟動了,所以上面的測試沒有展示資料敬請見諒。