C#使用時序數(shù)據(jù)庫InfluxDB的教程詳解
一、安裝
https://docs.influxdata.com/influxdb/v2/install/?t=Windows
解壓后使用cmd運行
訪問 localhost:8086
配置
第一次登入會初始化
配置登入賬號
保存TOKEN
這個TOKEN用于后期代碼鏈接訪問數(shù)據(jù)庫,忘記了只能刪除重新生成
點擊QUCK START進入管理頁面
默認配置文件
windows:在用戶文件夾下 C:\Users\Administrator.influxdbv2linux: /etc/influxdb/influxdb.conf
二、C#調(diào)用
Load Data>Sources 選擇c# 查看配置示例
創(chuàng)建一個控制臺程序
安裝InfluxDB客戶端
創(chuàng)建鏈接
using System.Linq; using System.Threading.Tasks; using InfluxDB.Client; using InfluxDB.Client.Api.Domain; using InfluxDB.Client.Core; using InfluxDB.Client.Writes; namespace Examples { public class Examples { public static async Task Main(string[] args) { // You can generate an API token from the "API Tokens Tab" in the UI var token = Environment.GetEnvironmentVariable("INFLUX_TOKEN")!; const string bucket = "Test"; const string org = "CC"; using var client = new InfluxDBClient("http://127.0.0.1:8086", token); } } }
寫入數(shù)據(jù)
//方式一、使用WriteRecord const string data = "mem,host=host1 used_percent=23.43234543"; using (var writeApi = client.GetWriteApi()) { writeApi.WriteRecord(data,bucket, org, WritePrecision.Ns ); } //方式二、使用WritePoint var point = PointData .Measurement("mem") .Tag("host", "host1") .Field("used_percent", 23.43234543) .Timestamp(DateTime.UtcNow, WritePrecision.Ns); using (var writeApi = client.GetWriteApi()) { writeApi.WritePoint(point,bucket, org); } //方式三、使用實體類 var mem = new Mem { Host = "host1", UsedPercent = 23.43234543, Time = DateTime.UtcNow }; using (var writeApi = client.GetWriteApi()) { writeApi.WriteMeasurement( mem,bucket, org, WritePrecision.Ns); } [Measurement("mem")] private class Mem { [Column("host", IsTag = true)] public string Host { get; set; } [Column("used_percent")] public double? UsedPercent { get; set; } [Column(IsTimestamp = true)] public DateTime Time { get; set; } }
最終測試代碼
// See https://aka.ms/new-console-template for more information using InfluxDB.Client; using InfluxDB.Client.Api.Domain; using InfluxDB.Client.Writes; Console.WriteLine("Hello, World!"); Environment.SetEnvironmentVariable("INFLUX_TOKEN", "O9I2Kpeg...kLPSrQLWhTiJCQPWy6HJFjN9hK33UoLnG34vfFdqZ5KmoDLS-kkw=="); var token = Environment.GetEnvironmentVariable("INFLUX_TOKEN")!; const string bucket = "Test"; const string org = "CC"; using (var client = new InfluxDBClient("http://localhost", token)) { using (var writeApi = client.GetWriteApi()) { while (true) { var randon = new Random(); var point = PointData .Measurement("mem") .Tag("host", "host1") .Field("used_percent", randon.Next(10, 100)) //可以添加多個字段 .Field("memory_percent",randon.Next(0,10)) .Timestamp(DateTime.UtcNow, WritePrecision.Ns); writeApi.WritePoint(point, bucket, org); Thread.Sleep(2000); } } }
在管理頁面查看數(shù)據(jù)
到此這篇關(guān)于C#使用時序數(shù)據(jù)庫InfluxDB的教程詳解的文章就介紹到這了,更多相關(guān)C# InfluxDB內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity ScrollView實現(xiàn)自動吸附效果
這篇文章主要為大家詳細介紹了Unity ScrollView實現(xiàn)自動吸附效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07C#實現(xiàn)將商品金額小寫轉(zhuǎn)換成大寫的方法
這篇文章主要介紹了C#實現(xiàn)將商品金額小寫轉(zhuǎn)換成大寫的方法,涉及C#數(shù)組與字符串的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-08-08DevExpress設置TreeList圖片節(jié)點背景色的方法
這篇文章主要介紹了DevExpress設置TreeList圖片節(jié)點背景色的方法,需要的朋友可以參考下2014-08-08