Class InfluxDBClient
Inherited Members
Namespace: InfluxDB3.Client
Assembly: InfluxDB3.Client.dll
Syntax
public class InfluxDBClient : IInfluxDBClient, IDisposable
Constructors
InfluxDBClient()
Initializes a new instance of the client using connection string.
Supported parameters are:
- INFLUX_HOST - InfluxDB host URL (required)
- INFLUX_TOKEN - authentication token (required)
- INFLUX_AUTH_SCHEME - token authentication scheme (default is
nullfor Cloud access) - INFLUX_ORG - organization name
- INFLUX_DATABASE - database (bucket) name
- INFLUX_PRECISION - timestamp precision when writing data (
ns(default),us,ms,s) - INFLUX_GZIP_THRESHOLD - threshold for gzipping data when writing (default is
1000) - INFLUX_WRITE_NO_SYNC - bool value whether to skip waiting for WAL persistence on write (default is
false)
Declaration
public InfluxDBClient()
Examples
using var client = new InfluxDBClient();
InfluxDBClient(ClientConfig)
Initializes a new instance of the client with provided configuration.
Declaration
public InfluxDBClient(ClientConfig config)
Parameters
| Type | Name | Description |
|---|---|---|
| ClientConfig | config | The configuration of the client. |
Examples
using var client = new InfluxDBClient( new ClientConfig { Host = "https://us-east-1-1.aws.cloud2.influxdata.com", Token = "my-token", Organization = "my-org", Database = "my-database" } );
InfluxDBClient(string)
Initializes a new instance of the client using connection string.
Supported parameters are:
- token - authentication token (required)
- authScheme - token authentication scheme (default
nullfor Cloud access) - org - organization name
- database - database (bucket) name
- precision - timestamp precision when writing data (
ns(default),us,ms,s) - gzipThreshold - threshold for gzip data when writing (default is
1000) - writeNoSync - bool value whether to skip waiting for WAL persistence on write (default is
false)
Declaration
public InfluxDBClient(string connectionString)
Parameters
| Type | Name | Description |
|---|---|---|
| string | connectionString | Connection string in URL format. |
Examples
using var client = new InfluxDBClient(host: "https://us-east-1-1.aws.cloud2.influxdata.com?token=my-token&database=my-db");
InfluxDBClient(string, string, string?, string?, string?)
Initializes a new instance of the client with provided configuration options.
Declaration
public InfluxDBClient(string host, string token, string? organization = null, string? database = null, string? authScheme = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | host | The URL of the InfluxDB server. |
| string | token | The authentication token for accessing the InfluxDB server. |
| string | organization | The organization name to be used for operations. |
| string | database | The database to be used for InfluxDB operations. |
| string | authScheme | Token authentications scheme. |
Examples
using var client = new InfluxDBClient(host: "https://us-east-1-1.aws.cloud2.influxdata.com", token: "my-token", organization: "my-org", database: "my-database");
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Declaration
public void Dispose()
GetServerVersion()
Retrieves the server version of the connected InfluxDB instance.
Declaration
public Task<string?> GetServerVersion()
Returns
| Type | Description |
|---|---|
| Task<string> | A string representing the version of the InfluxDB server. Returns null if the version cannot be determined from the response headers or body. |
Query(string, QueryType?, string?, Dictionary<string, object>?, Dictionary<string, string>?, TimeSpan?)
Query data from InfluxDB IOx using FlightSQL.
Declaration
public IAsyncEnumerable<object?[]> Query(string query, QueryType? queryType = null, string? database = null, Dictionary<string, object>? namedParameters = null, Dictionary<string, string>? headers = null, TimeSpan? timeout = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | query | The SQL query string to execute. |
| QueryType? | queryType | The type of query sent to InfluxDB. Default to 'SQL'. |
| string | database | The database to be used for InfluxDB operations. |
| Dictionary<string, object> | namedParameters | Name:Value pairs to use as named parameters for this query. Parameters referenced using '$placeholder' syntax in the query will be substituted with the values provided. The supported types are: string, bool, int, float. |
| Dictionary<string, string> | headers | The headers to be added to query request. The headers specified here are preferred over the headers specified in the client configuration. |
| TimeSpan? | timeout | The timeout to use for only this query request. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<object[]> | Batches of rows |
Examples
The following example shows how to use SQL query with named parameters:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
var results = client.Query(
query: "SELECT a, b, c FROM my_table WHERE id = $id AND name = $name",
namedParameters: new Dictionary<string, object> { { "id", 1 }, { "name", "test" } }
);
The following example shows how to use custom request headers:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
var results = client.Query(
query: "SELECT a, b, c FROM my_table",
headers: new Dictionary<string, string> { { "X-Tracing-Id", "123" } }
);
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The client is already disposed |
QueryBatches(string, QueryType?, string?, Dictionary<string, object>?, Dictionary<string, string>?, TimeSpan?)
Query data from InfluxDB IOx using FlightSQL.
Declaration
public IAsyncEnumerable<RecordBatch> QueryBatches(string query, QueryType? queryType = null, string? database = null, Dictionary<string, object>? namedParameters = null, Dictionary<string, string>? headers = null, TimeSpan? timeout = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | query | The SQL query string to execute. |
| QueryType? | queryType | The type of query sent to InfluxDB. Default to 'SQL'. |
| string | database | The database to be used for InfluxDB operations. |
| Dictionary<string, object> | namedParameters | Name:Value pairs to use as named parameters for this query. Parameters referenced using '$placeholder' syntax in the query will be substituted with the values provided. The supported types are: string, bool, int, float. |
| Dictionary<string, string> | headers | The headers to be added to query request. The headers specified here are preferred over the headers specified in the client configuration. |
| TimeSpan? | timeout | The timeout to use for only this query request. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<RecordBatch> | Batches of rows |
Examples
The following example shows how to use SQL query with named parameters:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
var results = client.QueryBatches(
query: "SELECT a, b, c FROM my_table WHERE id = $id AND name = $name",
namedParameters: new Dictionary<string, object> { { "id", 1 }, { "name", "test" } }
);
The following example shows how to use custom request headers:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
var results = client.QueryBatches(
query: "SELECT a, b, c FROM my_table",
headers: new Dictionary<string, string> { { "X-Tracing-Id", "123" } }
);
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The client is already disposed |
QueryPoints(string, QueryType?, string?, Dictionary<string, object>?, Dictionary<string, string>?, TimeSpan?)
Query data from InfluxDB IOx into PointData structure using FlightSQL.
Declaration
public IAsyncEnumerable<PointDataValues> QueryPoints(string query, QueryType? queryType = null, string? database = null, Dictionary<string, object>? namedParameters = null, Dictionary<string, string>? headers = null, TimeSpan? timeout = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | query | The SQL query string to execute. |
| QueryType? | queryType | The type of query sent to InfluxDB. Default to 'SQL'. |
| string | database | The database to be used for InfluxDB operations. |
| Dictionary<string, object> | namedParameters | Name:Value pairs to use as named parameters for this query. Parameters referenced using '$placeholder' syntax in the query will be substituted with the values provided. The supported types are: string, bool, int, float. |
| Dictionary<string, string> | headers | The headers to be added to query request. The headers specified here are preferred over the headers specified in the client configuration. |
| TimeSpan? | timeout | The timeout to use for only this query request. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<PointDataValues> | Batches of rows |
Examples
The following example shows how to use SQL query with named parameters:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
var results = client.QueryPoints(
query: "SELECT a, b, c FROM my_table WHERE id = $id AND name = $name",
namedParameters: new Dictionary<string, object> { { "id", 1 }, { "name", "test" } }
);
The following example shows how to use custom request headers:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
var results = client.QueryPoints(
query: "SELECT a, b, c FROM my_table",
headers: new Dictionary<string, string> { { "X-Tracing-Id", "123" } }
);
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The client is already disposed |
WritePointAsync(PointData, string?, WritePrecision?, Dictionary<string, string>?, CancellationToken)
Write data to InfluxDB.
Warning: If you write only with one Point, and that Point contains null fields, those fields will not be written to InfluxDB. If such fields are later queried explicitly, for example, "SELECT field_with_value, field_with_null_value FROM my_table" an error will be thrown
Declaration
public Task WritePointAsync(PointData point, string? database = null, WritePrecision? precision = null, Dictionary<string, string>? headers = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| PointData | point | Specifies the Data point to write into InfluxDB. The point is considered as one batch unit. |
| string | database | The database to be used for InfluxDB operations. |
| WritePrecision? | precision | The to use for the timestamp in the write API call. |
| Dictionary<string, string> | headers | The headers to be added to write request. The headers specified here are preferred over the headers specified in the client configuration. |
| CancellationToken | cancellationToken | specifies the token to monitor for cancellation requests. |
Returns
| Type | Description |
|---|---|
| Task |
Examples
The following example shows how to write a single point with custom headers:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
await client.WritePointAsync(
point: PointData.Measurement("h2o").SetTag("location", "europe").SetField("level", 2),
headers: new Dictionary<string, string> { { "X-Tracing-Id", "123" } }
);
WritePointsAsync(IEnumerable<PointData>, string?, WritePrecision?, Dictionary<string, string>?, CancellationToken)
Write data to InfluxDB.
Declaration
public Task WritePointsAsync(IEnumerable<PointData> points, string? database = null, WritePrecision? precision = null, Dictionary<string, string>? headers = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<PointData> | points | Specifies the Data points to write into InfluxDB. The points is considered as one batch unit. |
| string | database | The database to be used for InfluxDB operations. |
| WritePrecision? | precision | The to use for the timestamp in the write API call. |
| Dictionary<string, string> | headers | The headers to be added to write request. The headers specified here are preferred over the headers specified in the client configuration. |
| CancellationToken | cancellationToken | specifies the token to monitor for cancellation requests. |
Returns
| Type | Description |
|---|---|
| Task |
Examples
The following example shows how to write multiple points with custom headers:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
await client.WritePointsAsync(
points: new[]{
PointData.Measurement("h2o").SetTag("location", "europe").SetField("level", 2),
PointData.Measurement("h2o").SetTag("location", "us-west").SetField("level", 4),
},
headers: new Dictionary<string, string> { { "X-Tracing-Id", "123" } }
);
WriteRecordAsync(string, string?, WritePrecision?, Dictionary<string, string>?, CancellationToken)
Write data to InfluxDB.
Declaration
public Task WriteRecordAsync(string record, string? database = null, WritePrecision? precision = null, Dictionary<string, string>? headers = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | record | Specifies the record in InfluxDB Line Protocol. The record is considered as one batch unit. |
| string | database | The database to be used for InfluxDB operations. |
| WritePrecision? | precision | The to use for the timestamp in the write API call. |
| Dictionary<string, string> | headers | The headers to be added to write request. The headers specified here are preferred over the headers specified in the client configuration. |
| CancellationToken | cancellationToken | specifies the token to monitor for cancellation requests. |
Returns
| Type | Description |
|---|---|
| Task |
Examples
The following example shows how to write a single record with custom headers:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
await client.WriteRecordAsync(
record: "stat,unit=temperature value=24.5",
headers: new Dictionary<string, string> { { "X-Tracing-Id", "123" } }
);
WriteRecordsAsync(IEnumerable<string>, string?, WritePrecision?, Dictionary<string, string>?, CancellationToken)
Write data to InfluxDB.
Declaration
public Task WriteRecordsAsync(IEnumerable<string> records, string? database = null, WritePrecision? precision = null, Dictionary<string, string>? headers = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<string> | records | Specifies the records in InfluxDB Line Protocol. The records is considered as one batch unit. |
| string | database | The database to be used for InfluxDB operations. |
| WritePrecision? | precision | The to use for the timestamp in the write API call. |
| Dictionary<string, string> | headers | The headers to be added to write request. The headers specified here are preferred over the headers specified in the client configuration. |
| CancellationToken | cancellationToken | specifies the token to monitor for cancellation requests. |
Returns
| Type | Description |
|---|---|
| Task |
Examples
The following example shows how to write multiple records with custom headers:
using var client = new InfluxDBClient(host: "http://localhost:8086", token: "my-token", organization: "my-org", database: "my-database");
await client.WriteRecordsAsync(
records: new[] { "stat,unit=temperature value=24.5", "stat,unit=temperature value=25.5" },
headers: new Dictionary<string, string> { { "X-Tracing-Id", "123" } }
);