Go client library

    This guide presumes some familiarity with Go and InfluxDB. If just getting started, see Get started with InfluxDB.

    1. .

    2. Add the client package your to your project dependencies.

    3. Ensure that InfluxDB is running and you can connect to it. For information about what URL to use to connect to InfluxDB OSS or InfluxDB Cloud, see InfluxDB URLs.

    Use the Go library to write and query data from InfluxDB.

      1. import (
      2. "context"
      3. "fmt"
      4. "time"
      5. "github.com/influxdata/influxdb-client-go/v2"
      6. )
    1. Define variables for your InfluxDB , organization, and .

      1. bucket := "example-bucket"
      2. org := "example-org"
      3. token := "example-token"
      4. // Store the URL of your InfluxDB instance
      5. url := "http://localhost:8086"
    2. Create the the InfluxDB Go client and pass in the url and token parameters.

    3. Create a write client with the WriteAPIBlocking method and pass in the org and bucket parameters.

      1. writeAPI := client.WriteAPIBlocking(org, bucket)
    4. To query data, create an InfluxDB query client and pass in your InfluxDB org.

      1. queryAPI := client.QueryAPI(org)

    Use the Go library to write data to InfluxDB.

    1. func main() {
    2. bucket := "example-bucket"
    3. org := "example-org"
    4. token := "example-token"
    5. // Store the URL of your InfluxDB instance
    6. url := "http://localhost:8086"
    7. client := influxdb2.NewClient(url, token)
    8. // User blocking write client for writes to desired bucket
    9. writeAPI := client.WriteAPIBlocking(org, bucket)
    10. // Create point using full params constructor
    11. p := influxdb2.NewPoint("stat",
    12. map[string]string{"unit": "temperature"},
    13. map[string]interface{}{"avg": 24.5, "max": 45},
    14. time.Now())
    15. // Write point immediately
    16. writeAPI.WritePoint(context.Background(), p)
    17. // Ensures background processes finishes
    18. client.Close()
    19. }

    Use the Go library to query data to InfluxDB.

    1. Create a Flux query and supply your bucket parameter.

      1. from(bucket:"<bucket>")
      2. |> range(start: -1h)
      3. |> filter(fn: (r) => r._measurement == "stat")

      The query client sends the Flux query to InfluxDB and returns the results as a FluxRecord object with a table structure.

    The query client includes the following methods:

    • Query: Sends the Flux query to InfluxDB.
    • Next: Iterates over the query response.
    • TableChanged: Identifies when the group key changes.
    • Record: Returns the last parsed FluxRecord and gives access to value and row properties.
    • Value: Returns the actual field value.
    • Table(): Returns the index of the table the record belongs to.
    • Start(): Returns the inclusive lower time bound of all records in the current table.
    • Time(): Returns the time of the record.
    • Value() : Returns the actual field value.
    • Field(): Returns the field name.
    • : Returns the measurement name of the record.
    • Values(): Returns a map of column values.
    • ValueByKey(<your_tags>): Returns a value from the record for given column key.

    Complete example query script

    1. func main() {
    2. // Create client
    3. client := influxdb2.NewClient(url, token)
    4. // Get query client
    5. queryAPI := client.QueryAPI(org)
    6. // Get QueryTableResult
    7. result, err := queryAPI.Query(context.Background(), `from(bucket:"my-bucket")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "stat")`)
    8. if err == nil {
    9. // Iterate over query response
    10. for result.Next() {
    11. // Notice when group key has changed
    12. if result.TableChanged() {
    13. fmt.Printf("table: %s\n", result.TableMetadata().String())
    14. }
    15. // Access data
    16. fmt.Printf("value: %v\n", result.Record().Value())
    17. }
    18. // Check for an error
    19. if result.Err() != nil {
    20. fmt.Printf("query parsing error: %s\n", result.Err().Error())
    21. }
    22. } else {
    23. panic(err)
    24. }
    25. // Ensures background processes finishes

    For more information, see the .