• 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.
  • Features
  • Documentation
  • How To Use
    • Writes and Queries in InfluxDB 2.0
      • Installation
        • .Net CLI
        • Or when using Package Manager
    • Use Management API to create a new Bucket in InfluxDB 2.0
      • 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.0.

    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# - 图10Features

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

    C# - 图11Documentation

    The C# clients are implemented for the InfluxDB 2.0 and InfluxDB 1.7+:

    ClientDescriptionDocumentationCompatibility
    ClientThe reference C# client that allows query, write and InfluxDB 2.0 management.readme2.0
    Client.LegacyThe reference C# client that allows you to perform Flux queries against InfluxDB 1.7+.readme1.7+

    C# - 图12How To Use

    This clients are a work in progress and hosted in Bonitoo.io Nuget Repository.

    If you want to use it with a .Net CLI or Package Manager, you have to specify source of packages. See examples below.

    C# - 图13Writes and Queries in InfluxDB 2.0

    The following example demonstrates how to write data to InfluxDB 2.0 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.0

    The following example demonstrates how to use a InfluxDB 2.0 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.Collections.Generic;
    2. using InfluxDB.Client;
    3. using InfluxDB.Client.Api.Domain;
    4. using Task = System.Threading.Tasks.Task;
    5. namespace Examples
    6. {
    7. public static class ManagementExample
    8. {
    9. private static readonly char[] Token = "".ToCharArray();
    10. public static async Task Main(string[] args)
    11. {
    12. var influxDBClient = InfluxDBClientFactory.Create("http://localhost:8086", Token);
    13. //
    14. // Create bucket "iot_bucket" with data retention set to 3,600 seconds
    15. //
    16. var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, 3600);
    17. var bucket = await influxDBClient.GetBucketsApi().CreateBucketAsync("iot_bucket", retention, "org_id");
    18. //
    19. // Create access token to "iot_bucket"
    20. //
    21. var resource = new PermissionResource{Id = bucket.Id, OrgID = "org_id",Type = PermissionResource.TypeEnum.Buckets};
    22. // Read permission
    23. var read = new Permission{Resource = resource, Action = Permission.ActionEnum.Read};
    24. // Write permission
    25. var write = new Permission{Resource = resource, Action = Permission.ActionEnum.Write};
    26. var authorization = await influxDBClient.GetAuthorizationsApi()
    27. .CreateAuthorizationAsync("org_id", new List<Permission> {read, write});
    28. //
    29. // Created token that can be use for writes to "iot_bucket"
    30. //
    31. var token = authorization.Token;
    32. influxDBClient.Dispose();
    33. }
    34. }
    35. }

    C# - 图21InfluxDB 1.8 API compatibility

    InfluxDB 1.8.0 introduced forward compatibility APIs for InfluxDB 2.0. This allow you to easily move from InfluxDB 1.x to InfluxDB 2.0 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.0 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.0 API
    HealthAsync/healthCheck the health of your InfluxDB instance

    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. 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, (cancellable, 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.0 Clients are released under the MIT License.