InfluxDB.Client

作者:追风剑情 发布于:2023-7-18 14:42 分类:C#

一、下载InfluxDB

官方下载 Download InfluxDB v2.0 for Windows
1111.png
GitHub下载

8888.png
influx.exe是官方提供的命令行客户端
influxd.exe是服务器端

二、启动InfluxDB

在cmd中运行influxd.exe
示例:在cmd中输入 >D:\Software\influxdb2-2.0.9-windows-amd64\influxd.exe (这是作者的路径)
9999.png

三、访问InfluxDB后台

InfluxDB默认后台登录地址:http://localhost:8086
访问InfluxDB本地API文档:http://localhost:8086/docs
在线文档 https://docs.influxdata.com/influxdb/v2.0/
访问C#示例代码,在左侧菜单中选择【data】。
7777.png
[GitHub] influxdb-client-csharp
[GitHub] influxdb-client-java

四、初始化用户设置

首次使用InfluxDB时,需要初始化用户设置,登录后台会自动提示进行初始化设置。如图
22222.png

五、创建.NETFramework项目

六、安装 InfluxDB.Client

[工具]->"NuGet 包管理器"->"程序包管理器控制台"

44444.png

输入 Install-Package InfluxDB.Client 回车

55555.png

示例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Sockets;
  4. using System.Threading.Tasks;
  5. using InfluxDB.Client;
  6. using InfluxDB.Client.Api.Domain;
  7. using InfluxDB.Client.Core;
  8. using InfluxDB.Client.Core.Flux.Domain;
  9. using InfluxDB.Client.Writes;
  10.  
  11. namespace InfluxClientTest
  12. {
  13. public class Program
  14. {
  15. // token的值可在示例代码中找到
  16. const string TOKEN = "a1HBj6SY62ajZjr-j6FmeOw3417ZtUn3rD7fsiPCA_lL_4hLdx7fHHqWJMzkR8Xp5wurvYKz39CALfhIFw1Ydw==";
  17. const string BUCKET = "my-bucket";
  18. const string ORG = "test";
  19. const string URL = "http://localhost:8086";
  20. static InfluxDBClient client;
  21.  
  22. static void Main(string[] args)
  23. {
  24. //创建InfluxDB客户端
  25. client = new InfluxDBClient(URL, TOKEN);
  26. //插入数据
  27. Insert();
  28. //查询数据
  29. Query();
  30.  
  31. Console.ReadKey();
  32. }
  33.  
  34. // 插入数据
  35. public static void Insert()
  36. {
  37. const string data = "mem,host=host1 used_percent=23.43234543";
  38. using (var writeApi = client.GetWriteApi())
  39. {
  40. writeApi.WriteRecord(data, WritePrecision.Ns, BUCKET, ORG);
  41. }
  42. }
  43.  
  44. // 查询数据
  45. public static async void Query()
  46. {
  47. var query = $"from(bucket: \"{BUCKET}\") |> range(start: -1h)";
  48. // 返回数据表集合
  49. List<FluxTable> tables = await client.GetQueryApi().QueryAsync(query, ORG);
  50. // 遍历每张数据表
  51. foreach (FluxTable table in tables)
  52. {
  53. Console.WriteLine(table.Records.Count);
  54. // 遍历数据表的每行记录
  55. foreach(FluxRecord record in table.Records)
  56. {
  57. // 遍历每条记录的字段
  58. foreach(KeyValuePair<string, object> kv in record.Values)
  59. {
  60. Console.WriteLine("key={0}, value={1}", kv.Key, kv.Value);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }

查询数据返回结果
6666.png

Syntax: Line protocol
Flux v0.x documentation

  1. 测量名称(measurement name)、标记键(tag key)和字段键(field key)不能以下划线开头。_命名空间是为InfluxDB系统保留的。
  2. Measurements, tag keys, tag values, 和 field keys 都是字符串类型。InfluxDB会将tag按字符串存储。
  3. InfluxDB 会将不同的 field key 归到不同的表。每张表仅记录一个 field key。每条记录返回多个tag(如果有)和一个字段以及字段的值。
  4. 同一张表中的记录可用tag分类。
  5. |>操作符的意思是将上一步的查询结果传递给下一步进行处理。
  6. 查询多个字段可用or进行连接 |> filter(fn: (r) => r["_field"] == "xxx" or r["_field"] == "yyy")
  7. 如果在同一个时间点插入了多个字段,希望查询出来的每条记录包含插入的多个字段,可以添加
    |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  8. 限制返回2条记录|> limit(n:2)
  9. 降序排序|> sort(desc: true)
  10. 返回最新的一条记录 |> sort(desc: true) |> limit(n:1),还可以直接写成|> top(n:1)等效于前面写的sort和limit组合。
  11. 按tag过滤 |> filter(fn:(r)=>r.tag1=="736")


抛异常
InfluxDB.Client.Core.Exceptions.UnauthorizedException: unauthorized access

解决方案:检查是否token参数传错了

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号