influxdb-client-csharp
This repository contains the reference C# client for the InfluxDB 2.x.
Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+ (). For connecting to InfluxDB 1.7 or earlier instances, use the influxdb-csharp client library.
This section contains links to the client library documentation.
- Supports querying using the Flux language over the InfluxDB 1.7+ REST API ()
- InfluxDB 2.x client
- Querying data using the Flux language
- Writing data using
- Line Protocol
- POCO
- InfluxDB 2.x Management API client for managing
- sources, buckets
- tasks
- authorizations
- health check
- …
The following example demonstrates how to write data to InfluxDB 2.x and read them back using the Flux language:
Installation
.Net CLI
Or when using Package Manager
Install-Package InfluxDB.Client
using System;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;
using InfluxDB.Client.Writes;
using Task = System.Threading.Tasks.Task;
namespace Examples
{
public static class QueriesWritesExample
{
private static readonly char[] Token = "".ToCharArray();
public static async Task Main(string[] args)
{
var influxDBClient = InfluxDBClientFactory.Create("http://localhost:8086", Token);
//
// Write Data
//
using (var writeApi = influxDBClient.GetWriteApi())
{
// Write by Point
//
var point = PointData.Measurement("temperature")
.Tag("location", "west")
.Field("value", 55D)
.Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);
writeApi.WritePoint("bucket_name", "org_id", point);
//
// Write by LineProtocol
//
writeApi.WriteRecord("bucket_name", "org_id", WritePrecision.Ns, "temperature,location=north value=60.0");
//
// Write by POCO
//
var temperature = new Temperature {Location = "south", Value = 62D, Time = DateTime.UtcNow};
writeApi.WriteMeasurement("bucket_name", "org_id", WritePrecision.Ns, temperature);
}
//
// Query data
//
var flux = "from(bucket:\"temperature-sensors\") |> range(start: 0)";
var fluxTables = await influxDBClient.GetQueryApi().QueryAsync(flux, "org_id");
fluxTables.ForEach(fluxTable =>
{
fluxRecords.ForEach(fluxRecord =>
{
Console.WriteLine($"{fluxRecord.GetTime()}: {fluxRecord.GetValue()}");
});
});
influxDBClient.Dispose();
}
[Measurement("temperature")]
private class Temperature
{
[Column("location", IsTag = true)] public string Location { get; set; }
[Column("value")] public double Value { get; set; }
[Column(IsTimestamp = true)] public DateTime Time;
}
}
}
The following example demonstrates how to use a InfluxDB 2.x Management API. For further information see client documentation.
Installation
Use the latest version:
.Net CLI
Or when using Package Manager
Install-Package InfluxDB.Client
using System;
using System.Collections.Generic;
using System.Linq;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using Task = System.Threading.Tasks.Task;
namespace Examples
{
public static class ManagementExample
{
public static async Task Main(string[] args)
{
const string url = "http://localhost:8086";
const string token = "my-token";
const string org = "my-org";
using var client = InfluxDBClientFactory.Create(url, token);
// Find ID of Organization with specified name (PermissionAPI requires ID of Organization).
var orgId = (await client.GetOrganizationsApi().FindOrganizationsAsync(org: org)).First().Id;
//
// Create bucket "iot_bucket" with data retention set to 3,600 seconds
//
var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, 3600);
var bucket = await client.GetBucketsApi().CreateBucketAsync("iot_bucket", retention, orgId);
//
// Create access token to "iot_bucket"
//
orgId);
// Read permission
var read = new Permission(Permission.ActionEnum.Read, resource);
// Write permission
var write = new Permission(Permission.ActionEnum.Write, resource);
var authorization = await client.GetAuthorizationsApi()
.CreateAuthorizationAsync(orgId, new List<Permission> { read, write });
//
// Created token that can be use for writes to "iot_bucket"
//
Console.WriteLine($"Authorized token to write into iot_bucket: {authorization.Token}");
}
}
}
for InfluxDB 2.x. This allow you to easily move from InfluxDB 1.x to InfluxDB 2.x Cloud or open source.
API | Endpoint | Description |
---|---|---|
QueryApi.cs | Query data in InfluxDB 1.8.0+ using the InfluxDB 2.x API and Flux (endpoint should be enabled by ) | |
WriteApi.cs | Write data to InfluxDB 1.8.0+ using the InfluxDB 2.x API | |
PingAsync | Checks the status of InfluxDB instance and version of InfluxDB. |
For detail info see InfluxDB 1.8 example.
The following example demonstrates querying using the Flux language.
Installation
Use the latest version:
.Net CLI
Or when using Package Manager
Install-Package InfluxDB.Client.Flux
using System;
using InfluxDB.Client.Flux;
namespace Examples
{
public static class FluxExample
{
public static void Run()
{
using var fluxClient = FluxClientFactory.Create("http://localhost:8086/");
var fluxQuery = "from(bucket: \"telegraf\")\n"
+ " |> filter(fn: (r) => (r[\"_measurement\"] == \"cpu\" AND r[\"_field\"] == \"usage_system\"))"
+ " |> range(start: -1d)"
+ " |> sample(n: 5, pos: 1)";
fluxClient.QueryAsync(fluxQuery, record =>
{
// process the flux query records
Console.WriteLine(record.GetTime() + ": " + record.GetValue());
},
(error) =>
{
// error handling while processing result
Console.WriteLine(error.ToString());
}, () =>
{
// on complete
Console.WriteLine("Query completed");
}).GetAwaiter().GetResult();
}
}
The InfluxDB 2.x Clients are released under the .