首页 > 数据库 >Net6 操作时序数据库 Influxdb ,插入数据

Net6 操作时序数据库 Influxdb ,插入数据

时间:2022-11-17 17:35:04浏览次数:44  
标签:8086 Influxdb options Value var Net6 logger 时序 public

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

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/

@天才卧龙的博客 

标签:8086,Influxdb,options,Value,var,Net6,logger,时序,public
From: https://www.cnblogs.com/chenwolong/p/16900169.html

相关文章