Net6 操作時序數據庫 Influxdb ,插入數據

十年河東,十年河西,莫欺少年窮

學無止境,精益求精

1、引入組件

 

 2、書寫代碼

    public class InfluxDto
    {
        private readonly ILogger<InfluxDto> logger;
        private readonly IOptionsSnapshot<InfluxdbParamOptions> options;
        public InfluxDto(IOptionsSnapshot<InfluxdbParamOptions> options, ILogger<InfluxDto> logger)
        {
            this.options = options;
            this.logger = logger;
        }
        public void AddInfluxdbData()
        {
            try
            {
                using (var client = InfluxDBClientFactory.Create("http://localhost:8086", options.Value.token))
                {
                    var point = PointData
                       .Measurement("BatteryHeart") //表名
                       .Tag("BatterySN", "3343221080107660") //標籤數不能大於10萬個
                       .Field("BatteryChargingNumber", 1)
                       .Field("BatteryLoopNumber", 1) 
                       .Field("SocFlag", 1)
                       .Timestamp(DateTime.UtcNow, WritePrecision.Ns);

                    using (var writeApi = client.GetWriteApi())
                    {

                        writeApi.WritePoint(point, options.Value.bucket, options.Value.org);
                    }
                }
            }
            catch(Exception ex)
            {
                logger.LogError("時序異常:" + ex.ToString());
            }  
        }
    }

配置文件參數如下:

 

 3、創建Token 

 

詳見部署的8086端口網頁 http://localhost:8086/

@天才臥龍的博客 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章