Skip to content

The go package that provides a simple and convenient way to interact with InfluxDB 3.

License

Notifications You must be signed in to change notification settings

erratic-pattern/influxdb3-go

 
 

Repository files navigation

Gopher

Go Reference Go Report Card CodeQL analysis Lint Code Base CircleCI Code Cov Community Slack

InfluxDB 3 Go Client

The go package that provides an easy and convenient way to interact with InfluxDB 3. This package supports both writing data to InfluxDB and querying data using the FlightSQL client, which allows you to execute SQL queries against InfluxDB IOx.

Installation

Add the latest version of the client package to your project dependencies (go.mod):

go get github.com/InfluxCommunity/influxdb3-go

Usage

set environment variables:

  • INFLUXDB_URL region of your influxdb cloud e.g. https://us-east-1-1.aws.cloud2.influxdata.com/
  • INFLUXDB_TOKEN read/write token generated in cloud
  • INFLUXDB_DATABASE name of database e.g .my-database
linux/macos
export INFLUXDB_URL="<url>"
export INFLUXDB_DATABASE="<database>"
export INFLUXDB_TOKEN="<token>"
windows
setx INFLUXDB_URL "<url>"
setx INFLUXDB_DATABASE "<database>"
setx INFLUXDB_TOKEN "<token>"

To get started with influxdb client import influxdb3-go package.

import (
  "context"
  "encoding/json"
  "fmt"
  "os"

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

Create influxdb3.Client with New function. Make sure to Close client after with defer keyword.

url := os.Getenv("INFLUXDB_URL")
token := os.Getenv("INFLUXDB_TOKEN")
database := os.Getenv("INFLUXDB_DATABASE")

// Create a new client using an InfluxDB server base URL and an authentication token
client, err := influxdb3.New(influxdb3.ClientConfig{
    Host: url,
    Token: token,
    Database: database,
})
// Close client at the end and escalate error if present
defer func (client *influxdb3.Client)  {
    err := client.Close()
    if err != nil {
        panic(err)
    }
}(client)

The client can be now used to insert data using line-protocol.

line := "stat,unit=temperature avg=23.5,max=45.0"
err = client.Write(context.Background(), []byte(line))

Fetch data using FlightSQL query and print result.

query := `
        SELECT *
        FROM "stat"
        WHERE
        time >= now() - interval '5 minute'
        AND
        "unit" IN ('temperature')
`;

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

if err != nil {
    panic(err)
}

for iterator.Next() {
    value := iterator.Value()

    fmt.Printf("avg is %f\n", value["avg"])
    fmt.Printf("max is %f\n", value["max"])
}

Example

Prepare environment like in Usage and run go run ./example/main.go.

Feedback

If you need help, please use our Community Slack or Community Page.

New features and bugs can be reported on GitHub: https://github.com/InfluxCommunity/influxdb3-go

Contribution

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

License

The InfluxDB 3 Go Client is released under the MIT License. which allows you to execute SQL queries on InfluxDB IOx.

About

The go package that provides a simple and convenient way to interact with InfluxDB 3.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%