Documentation

Go client library for InfluxDB v3

The InfluxDB v3 influxdb3-go Go client library integrates with Go scripts and applications to write and query data stored in an InfluxDB Cloud Serverless bucket.

Installation

go get github.com/InfluxCommunity/influxdb3-go

Importing the package

The influxdb3-go client library module provides the influxdb3 package.

Import the package:

import (
    "github.com/InfluxCommunity/influxdb3-go/influxdb3"
)

API reference

Function New

Create a client to interact with InfluxDB.

Syntax

New(config ClientConfig)

Initializes and returns a influxdb3.Client instance with the following:

  • Configuration and functions for writing to the database.
  • A *flight.Client and functions for querying the database.

Parameters

  • config: A ClientConfig struct with the following configuration properties:

    • Host (string): the InfluxDB Cloud Serverless server URL
    • Token (string): a database token string
    • Database (string): the database to use for writing and querying.
    • Organization (string): Optional. The organization name or ID.
    • HTTPClient (*http.Client): Optional. Specifies a custom HTTP client, TLS configuration, or timeout to use.
    • WriteOptions (*WriteOptions): Optional. Options passed to the write client for writing to the bucket.
    • Headers (http.Header): Optional. Headers to include in all requests.

Examples

Create an InfluxDB client
package main

import (
    "github.com/InfluxCommunity/influxdb3-go/influxdb3"
)

func Query() error {

    client, err := influxdb3.New(influxdb3.ClientConfig{
        Host:       "https://cloud2.influxdata.com",
        Token:      "
API_TOKEN
"
,
Database: "
BUCKET_NAME
"
,
}) defer func(client *influxdb3.Client) { err := client.Close() if err != nil { panic(err) } }(client) if(err != nil) { panic(err) } }

Replace the following configuration values:

  • BUCKET_NAME: the name of the InfluxDB bucket to query
  • API_TOKEN: an InfluxDB API token with read permission on the specified bucket

Class influxdb3.Client

Function Client.Query()

Query data from InfluxDB IOx using SQL.

Syntax

client.Query(ctx context.Context, query string)

Sends a Flight query request with SQL to InfluxDB.

Returns the following:

  • A custom iterator (*QueryIterator) for accessing query result data and metadata.
  • An error, if any.

Parameters

  • ctx (context.Context): the context to use for the request
  • query (string): the SQL query to execute.

Examples

Query using SQL
query := `SELECT *
  FROM home
  WHERE time >= '2022-01-02T08:00:00Z'
  AND time <= '2022-01-02T20:00:00Z'`

iterator, err := client.Query(context.Background(), query)

Function Client.QueryWithOptions()

Query data from InfluxDB IOx with query options such as query type for querying with InfluxQL.

Syntax

client.QueryWithOptions(ctx context.Context, options *QueryOptions, query string)

Sends a query request with the specified query options to InfluxDB.

Returns the following:

  • A custom iterator (*QueryIterator) for accessing query result data and metadata.
  • An error, if any.

Parameters

  • ctx (context.Context): the context to use for the request
  • options: query options (query type, optional database)
  • query (string): the SQL or InfluxQL query to execute.

Examples

Query using InfluxQL
query := `SELECT *
  FROM home
  WHERE time >= 1641124000s
  AND time <= 1641124000s + 8h`
queryOptions := influxdb3.QueryOptions{QueryType: influxdb3.InfluxQL}
iterator, err := client.QueryWithOptions(context.Background(), &queryOptions, query)

Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following:

State of the InfluxDB Cloud Serverless documentation

InfluxDB Cloud Serverless documentation is a work in progress.

The new documentation for InfluxDB Cloud Serverless is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.

InfluxDB Cloud Serverless