• influxdb-client-csharp
    • Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+ ( see details ). For connecting to InfluxDB 1.7 or earlier instances, use the influxdb-csharp client library.
  • Documentation
  • Features
  • How To Use
    • Writes and Queries in InfluxDB 2.x
      • Installation
        • .Net CLI
        • Or when using Package Manager
    • Use Management API to create a new Bucket in InfluxDB 2.x
      • Installation
        • .Net CLI
        • Or when using Package Manager
    • InfluxDB 1.8 API compatibility
    • Flux queries in InfluxDB 1.7+
      • Installation
        • .Net CLI
        • Or when using Package Manager
  • Contributing
  • License

    C# - 图1influxdb-client-csharp

    CircleCI codecov Nuget License GitHub issues GitHub pull requests Slack Status

    This repository contains the reference C# client for the InfluxDB 2.x.

    C# - 图9Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+ (see details). For connecting to InfluxDB 1.7 or earlier instances, use the influxdb-csharp client library.

    C# - 图10Documentation

    This section contains links to the client library documentation.

    ClientDescriptionDocumentationCompatibility
    ClientThe reference C# client that allows query, write and InfluxDB 2.x management.readme2.x
    Client.LinqThe library supports to use a LINQ expression to query the InfluxDB.readme2.x
    Client.LegacyThe reference C# client that allows you to perform Flux queries against InfluxDB 1.7+.readme1.7+

    C# - 图11Features

    • Supports querying using the Flux language over the InfluxDB 1.7+ REST API (/api/v2/query endpoint)
    • InfluxDB 2.x client
      • Querying data using the Flux language
      • Writing data using
      • InfluxDB 2.x Management API client for managing
        • sources, buckets
        • tasks
        • authorizations
        • health check

    C# - 图12How To Use

    C# - 图13Writes and Queries in InfluxDB 2.x

    The following example demonstrates how to write data to InfluxDB 2.x and read them back using the Flux language:

    C# - 图14Installation

    Use the latest version:

    C# - 图15.Net CLI
    1. dotnet add package InfluxDB.Client
    C# - 图16Or when using Package Manager
    1. Install-Package InfluxDB.Client
    1. using System;
    2. using InfluxDB.Client;
    3. using InfluxDB.Client.Api.Domain;
    4. using InfluxDB.Client.Core;
    5. using InfluxDB.Client.Writes;
    6. using Task = System.Threading.Tasks.Task;
    7. namespace Examples
    8. {
    9. public static class QueriesWritesExample
    10. {
    11. private static readonly char[] Token = "".ToCharArray();
    12. public static async Task Main(string[] args)
    13. {
    14. var influxDBClient = InfluxDBClientFactory.Create("http://localhost:8086", Token);
    15. //
    16. // Write Data
    17. //
    18. using (var writeApi = influxDBClient.GetWriteApi())
    19. {
    20. //
    21. // Write by Point
    22. //
    23. var point = PointData.Measurement("temperature")
    24. .Tag("location", "west")
    25. .Field("value", 55D)
    26. .Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);
    27. writeApi.WritePoint("bucket_name", "org_id", point);
    28. //
    29. // Write by LineProtocol
    30. //
    31. writeApi.WriteRecord("bucket_name", "org_id", WritePrecision.Ns, "temperature,location=north value=60.0");
    32. //
    33. // Write by POCO
    34. //
    35. var temperature = new Temperature {Location = "south", Value = 62D, Time = DateTime.UtcNow};
    36. writeApi.WriteMeasurement("bucket_name", "org_id", WritePrecision.Ns, temperature);
    37. }
    38. //
    39. // Query data
    40. //
    41. var flux = "from(bucket:\"temperature-sensors\") |> range(start: 0)";
    42. var fluxTables = await influxDBClient.GetQueryApi().QueryAsync(flux, "org_id");
    43. fluxTables.ForEach(fluxTable =>
    44. {
    45. var fluxRecords = fluxTable.Records;
    46. fluxRecords.ForEach(fluxRecord =>
    47. {
    48. Console.WriteLine($"{fluxRecord.GetTime()}: {fluxRecord.GetValue()}");
    49. });
    50. });
    51. influxDBClient.Dispose();
    52. }
    53. [Measurement("temperature")]
    54. private class Temperature
    55. {
    56. [Column("location", IsTag = true)] public string Location { get; set; }
    57. [Column("value")] public double Value { get; set; }
    58. [Column(IsTimestamp = true)] public DateTime Time;
    59. }
    60. }
    61. }

    C# - 图17Use Management API to create a new Bucket in InfluxDB 2.x

    The following example demonstrates how to use a InfluxDB 2.x Management API. For further information see client documentation.

    C# - 图18Installation

    Use the latest version:

    C# - 图19.Net CLI
    1. dotnet add package InfluxDB.Client
    C# - 图20Or when using Package Manager
    1. Install-Package InfluxDB.Client
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using InfluxDB.Client;
    5. using InfluxDB.Client.Api.Domain;
    6. using Task = System.Threading.Tasks.Task;
    7. namespace Examples
    8. {
    9. public static class ManagementExample
    10. {
    11. public static async Task Main(string[] args)
    12. {
    13. const string url = "http://localhost:8086";
    14. const string token = "my-token";
    15. const string org = "my-org";
    16. using var client = InfluxDBClientFactory.Create(url, token);
    17. // Find ID of Organization with specified name (PermissionAPI requires ID of Organization).
    18. var orgId = (await client.GetOrganizationsApi().FindOrganizationsAsync(org: org)).First().Id;
    19. //
    20. // Create bucket "iot_bucket" with data retention set to 3,600 seconds
    21. //
    22. var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, 3600);
    23. var bucket = await client.GetBucketsApi().CreateBucketAsync("iot_bucket", retention, orgId);
    24. //
    25. // Create access token to "iot_bucket"
    26. //
    27. var resource = new PermissionResource(PermissionResource.TypeEnum.Buckets, bucket.Id, null,
    28. orgId);
    29. // Read permission
    30. var read = new Permission(Permission.ActionEnum.Read, resource);
    31. // Write permission
    32. var write = new Permission(Permission.ActionEnum.Write, resource);
    33. var authorization = await client.GetAuthorizationsApi()
    34. .CreateAuthorizationAsync(orgId, new List<Permission> { read, write });
    35. //
    36. // Created token that can be use for writes to "iot_bucket"
    37. //
    38. Console.WriteLine($"Authorized token to write into iot_bucket: {authorization.Token}");
    39. }
    40. }
    41. }

    C# - 图21InfluxDB 1.8 API compatibility

    InfluxDB 1.8.0 introduced forward compatibility APIs for InfluxDB 2.x. This allow you to easily move from InfluxDB 1.x to InfluxDB 2.x Cloud or open source.

    The following forward compatible APIs are available:

    APIEndpointDescription
    QueryApi.cs/api/v2/queryQuery data in InfluxDB 1.8.0+ using the InfluxDB 2.x API and Flux (endpoint should be enabled by flux-enabled option)
    WriteApi.cs/api/v2/writeWrite data to InfluxDB 1.8.0+ using the InfluxDB 2.x API
    PingAsync/pingChecks the status of InfluxDB instance and version of InfluxDB.

    For detail info see InfluxDB 1.8 example.

    C# - 图22Flux queries in InfluxDB 1.7+

    The following example demonstrates querying using the Flux language.

    C# - 图23Installation

    Use the latest version:

    C# - 图24.Net CLI
    1. dotnet add package InfluxDB.Client.Flux
    C# - 图25Or when using Package Manager
    1. Install-Package InfluxDB.Client.Flux
    1. using System;
    2. using InfluxDB.Client.Flux;
    3. namespace Examples
    4. {
    5. public static class FluxExample
    6. {
    7. public static void Run()
    8. {
    9. using var fluxClient = FluxClientFactory.Create("http://localhost:8086/");
    10. var fluxQuery = "from(bucket: \"telegraf\")\n"
    11. + " |> filter(fn: (r) => (r[\"_measurement\"] == \"cpu\" AND r[\"_field\"] == \"usage_system\"))"
    12. + " |> range(start: -1d)"
    13. + " |> sample(n: 5, pos: 1)";
    14. fluxClient.QueryAsync(fluxQuery, record =>
    15. {
    16. // process the flux query records
    17. Console.WriteLine(record.GetTime() + ": " + record.GetValue());
    18. },
    19. (error) =>
    20. {
    21. // error handling while processing result
    22. Console.WriteLine(error.ToString());
    23. }, () =>
    24. {
    25. // on complete
    26. Console.WriteLine("Query completed");
    27. }).GetAwaiter().GetResult();
    28. }
    29. }
    30. }

    C# - 图26Contributing

    If you would like to contribute code you can do through GitHub by forking the repository and sending a pull request into the master branch.

    C# - 图27License

    The InfluxDB 2.x Clients are released under the MIT License.