C#使用时序数据库 InfluxDB

一、安装

https://docs.influxdata.com/influxdb/v2/install/?t=Windows

解压后使用cmd运行

访问 localhost:8086

配置

第一次登入会初始化

配置登入账号
保存TOKEN

这个TOKEN用于后期代码链接访问数据库,忘记了只能删除重新生成

点击QUCK START进入管理页面

默认配置文件

windows:在用户文件夹下 C:\Users\Administrator.influxdbv2

linux: /etc/influxdb/influxdb.conf

二、C#调用

Load Data>Sources 选择c# 查看配置示例
创建一个控制台程序

安装InfluxDB客户端

创建链接
csharp 复制代码
using System.Linq;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;
using InfluxDB.Client.Writes;

namespace Examples
{
  public class Examples
  {
    public static async Task Main(string[] args)
    {
      // You can generate an API token from the "API Tokens Tab" in the UI
      var token = Environment.GetEnvironmentVariable("INFLUX_TOKEN")!;
      const string bucket = "Test";
      const string org = "CC";

      using var client = new InfluxDBClient("http://127.0.0.1:8086", token);
    }
  }
}
写入数据
csharp 复制代码
//方式一、使用WriteRecord
const string data = "mem,host=host1 used_percent=23.43234543";
using (var writeApi = client.GetWriteApi())
{
  writeApi.WriteRecord(data,bucket, org, WritePrecision.Ns );
}

//方式二、使用WritePoint
var point = PointData
  .Measurement("mem")
  .Tag("host", "host1")
  .Field("used_percent", 23.43234543)
  .Timestamp(DateTime.UtcNow, WritePrecision.Ns);

using (var writeApi = client.GetWriteApi())
{
  writeApi.WritePoint(point,bucket, org);
}

//方式三、使用实体类
var mem = new Mem { Host = "host1", UsedPercent = 23.43234543, Time = DateTime.UtcNow };

using (var writeApi = client.GetWriteApi())
{
  writeApi.WriteMeasurement( mem,bucket, org, WritePrecision.Ns);
}


[Measurement("mem")]
private class Mem
{
  [Column("host", IsTag = true)] public string Host { get; set; }
  [Column("used_percent")] public double? UsedPercent { get; set; }
  [Column(IsTimestamp = true)] public DateTime Time { get; set; }
}
最终测试代码
csharp 复制代码
// See https://aka.ms/new-console-template for more information
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;

Console.WriteLine("Hello, World!");
Environment.SetEnvironmentVariable("INFLUX_TOKEN", "O9I2Kpeg...kLPSrQLWhTiJCQPWy6HJFjN9hK33UoLnG34vfFdqZ5KmoDLS-kkw==");

var token = Environment.GetEnvironmentVariable("INFLUX_TOKEN")!;
const string bucket = "Test";
const string org = "CC";

using (var client = new InfluxDBClient("http://localhost", token))
{
    using (var writeApi = client.GetWriteApi())
    {
       while (true)
        {
            var randon = new Random();
            var point = PointData
                           .Measurement("mem")
                           .Tag("host", "host1")
                           .Field("used_percent", randon.Next(10, 100)) //可以添加多个字段
                           .Field("memory_percent",randon.Next(0,10))
                           .Timestamp(DateTime.UtcNow, WritePrecision.Ns);

            writeApi.WritePoint(point, bucket, org);

            Thread.Sleep(2000);
        }
    }
}
在管理页面查看数据


相关推荐
爱和冰阔落5 小时前
【Python基础】从变量到面向对象:打通 Python 入门核心语法
开发语言·python
凡人叶枫5 小时前
Effective C++ 条款05:了解 C++ 默默编写并调用哪些函数
java·linux·开发语言·c++·effective c++·编程范式
少司府5 小时前
C++进阶:AVL树
开发语言·数据结构·c++·二叉树·avl树
某风吾起5 小时前
C语言总结
c语言·开发语言
winlife_5 小时前
全程用 AI 做一款商业级手游 · EP7 表现层与手感:从“能跑“到“摸起来爽“
java·开发语言·人工智能·unity·ai编程·游戏开发·mcp
千纸鹤の脉搏5 小时前
多线程的初步使用
java·开发语言·学习·多线程
专注VB编程开发20年5 小时前
阿里通义灵码插件安装失败
开发语言·ide·c#·visual studio
weixin_446260855 小时前
Typora 插件开发实战:基于 JavaScript/HTML 构建定制化 Markdown 扩展
开发语言·javascript·html
好家伙VCC5 小时前
Rust+Bioinfo:80ms极速SNP注释引擎
java·开发语言·算法·rust
qq4356947015 小时前
Vue02
开发语言·前端·javascript