• 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

    influxdb-client-csharp

    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+ (). 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
      • 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
    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. // Write by Point
    21. //
    22. var point = PointData.Measurement("temperature")
    23. .Tag("location", "west")
    24. .Field("value", 55D)
    25. .Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);
    26. writeApi.WritePoint("bucket_name", "org_id", point);
    27. //
    28. // Write by LineProtocol
    29. //
    30. writeApi.WriteRecord("bucket_name", "org_id", WritePrecision.Ns, "temperature,location=north value=60.0");
    31. //
    32. // Write by POCO
    33. //
    34. var temperature = new Temperature {Location = "south", Value = 62D, Time = DateTime.UtcNow};
    35. writeApi.WriteMeasurement("bucket_name", "org_id", WritePrecision.Ns, temperature);
    36. }
    37. //
    38. // Query data
    39. //
    40. var flux = "from(bucket:\"temperature-sensors\") |> range(start: 0)";
    41. var fluxTables = await influxDBClient.GetQueryApi().QueryAsync(flux, "org_id");
    42. fluxTables.ForEach(fluxTable =>
    43. {
    44. fluxRecords.ForEach(fluxRecord =>
    45. {
    46. Console.WriteLine($"{fluxRecord.GetTime()}: {fluxRecord.GetValue()}");
    47. });
    48. });
    49. influxDBClient.Dispose();
    50. }
    51. [Measurement("temperature")]
    52. private class Temperature
    53. {
    54. [Column("location", IsTag = true)] public string Location { get; set; }
    55. [Column("value")] public double Value { get; set; }
    56. [Column(IsTimestamp = true)] public DateTime Time;
    57. }
    58. }
    59. }

    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:

    .Net CLI
    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. orgId);
    28. // Read permission
    29. var read = new Permission(Permission.ActionEnum.Read, resource);
    30. // Write permission
    31. var write = new Permission(Permission.ActionEnum.Write, resource);
    32. var authorization = await client.GetAuthorizationsApi()
    33. .CreateAuthorizationAsync(orgId, new List<Permission> { read, write });
    34. //
    35. // Created token that can be use for writes to "iot_bucket"
    36. //
    37. Console.WriteLine($"Authorized token to write into iot_bucket: {authorization.Token}");
    38. }
    39. }
    40. }

    for InfluxDB 2.x. This allow you to easily move from InfluxDB 1.x to InfluxDB 2.x Cloud or open source.

    APIEndpointDescription
    QueryApi.csQuery data in InfluxDB 1.8.0+ using the InfluxDB 2.x API and Flux (endpoint should be enabled by )
    WriteApi.csWrite data to InfluxDB 1.8.0+ using the InfluxDB 2.x API
    PingAsyncChecks 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:

    C# - 图24.Net CLI
    Or 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. }

    The InfluxDB 2.x Clients are released under the .