1、打开C#官方示例
自带的UI上有示例,路径如下:Data—Sources—c#,如下图:
2、得到Api Token
选择Data— API Tokens,然后选择已有的token,如下图:
按上图的链接点击后显示如下,下面框选的部分就是api token
3、创建项目
创建一个.net 5.0的项目,然后引入InfluxDB.Client
如下图:
4、创建Bucket
static void Main(string[] args) { const string token = "O75KHC7S3YXgNWg5dOt5z-tKRMqdIPzChJUIEw_2GDM9iR-2m8hEtd4QzUDX7KR3-4-MmgAKUgLmDjMNOcXb6Q=="; const string org = "test"; using var client = InfluxDBClientFactory.Create("http://localhost:8086", token); var orgId = client.GetOrganizationsApi().FindOrganizationsAsync(org: org).Result.First().Id; var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, 3600); var newBucket = client.GetBucketsApi().CreateBucketAsync("iot_bucket2", retention, orgId).Result; }
5、写入数据
const string bucket = "test1"; string data = "mem,host=host1 temperature=23.55,humidity=56.79"; using (var writeApi = client.GetWriteApi()) { writeApi.WriteRecord(bucket, org, WritePrecision.Ms, data); }