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/

@天才卧龙的博客 

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