fix(client): Fixing Go client guide reflect latest state

pull/1589/head
vlastahajek 2020-10-02 17:30:56 +02:00
parent c332dde043
commit 117b5c99fe
1 changed files with 50 additions and 51 deletions

View File

@ -21,15 +21,12 @@ If just getting started, see [Get started with InfluxDB](/influxdb/v2.0/get-star
## Before you begin ## Before you begin
1. [Install Go 1.3 or later](https://golang.org/doc/install). 1. [Install Go 1.13 or later](https://golang.org/doc/install).
2. Download the client package in your $GOPATH and build the package. 2. Add the client package your to your project dependencies.
```sh ```sh
# Download the InfluxDB Go client package # Add InfluxDB Go client package to your project go.mod
go get github.com/influxdata/influxdb-client-go go get github.com/influxdata/influxdb-client-go/v2
# Build the package
go build
``` ```
3. Ensure that InfluxDB is running and you can connect to it. 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](/influxdb/v2.0/reference/urls/). For information about what URL to use to connect to InfluxDB OSS or InfluxDB Cloud, see [InfluxDB URLs](/influxdb/v2.0/reference/urls/).
@ -44,12 +41,12 @@ Use the Go library to write and query data from InfluxDB.
package main package main
import ( import (
"context" "context"
"fmt" "fmt"
"time" "time"
influxdb2 "github.com/influxdata/influxdb-client-go" "github.com/influxdata/influxdb-client-go/v2"
) )
``` ```
2. Define variables for your InfluxDB [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), and [token](/influxdb/v2.0/security/tokens/). 2. Define variables for your InfluxDB [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), and [token](/influxdb/v2.0/security/tokens/).
@ -68,16 +65,16 @@ Use the Go library to write and query data from InfluxDB.
client := influxdb2.NewClient(url, token) client := influxdb2.NewClient(url, token)
``` ```
4. Create a **write client** with the `WriteApiBlocking` method and pass in the `org` and `bucket` parameters. 4. Create a **write client** with the `WriteAPIBlocking` method and pass in the `org` and `bucket` parameters.
```go ```go
writeApi := client.WriteApiBlocking(org, bucket) writeAPI := client.WriteAPIBlocking(org, bucket)
``` ```
5. To query data, create an InfluxDB **query client** and pass in your InfluxDB `org`. 5. To query data, create an InfluxDB **query client** and pass in your InfluxDB `org`.
```go ```go
queryApi := client.QueryApi(org) queryAPI := client.QueryAPI(org)
``` ```
## Write data to InfluxDB with Go ## Write data to InfluxDB with Go
@ -93,32 +90,32 @@ Use the Go library to write data to InfluxDB.
map[string]string{"unit": "temperature"}, map[string]string{"unit": "temperature"},
map[string]interface{}{"avg": 24.5, "max": 45}, map[string]interface{}{"avg": 24.5, "max": 45},
time.Now()) time.Now())
writeApi.WritePoint(context.Background(), p) writeAPI.WritePoint(context.Background(), p)
client.Close() client.Close()
``` ```
### Complete example write script ### Complete example write script
```go ```go
func main() { func main() {
bucket := "example-bucket" bucket := "example-bucket"
org := "example-org" org := "example-org"
token := "example-token" token := "example-token"
// Store the URL of your InfluxDB instance // Store the URL of your InfluxDB instance
url := "http://localhost:8086" url := "http://localhost:8086"
// Create new client with default option for server url authenticate by token // Create new client with default option for server url authenticate by token
client := influxdb2.NewClient(url, token) client := influxdb2.NewClient(url, token)
// User blocking write client for writes to desired bucket // User blocking write client for writes to desired bucket
writeApi := client.WriteApiBlocking(org, bucket) writeAPI := client.WriteAPIBlocking(org, bucket)
// Create point using full params constructor // Create point using full params constructor
p := influxdb2.NewPoint("stat", p := influxdb2.NewPoint("stat",
map[string]string{"unit": "temperature"}, map[string]string{"unit": "temperature"},
map[string]interface{}{"avg": 24.5, "max": 45}, map[string]interface{}{"avg": 24.5, "max": 45},
time.Now()) time.Now())
// Write point immediately // Write point immediately
writeApi.WritePoint(context.Background(), p) writeAPI.WritePoint(context.Background(), p)
// Ensures background processes finishes // Ensures background processes finishes
client.Close() client.Close()
} }
``` ```
## Query data from InfluxDB with Go ## Query data from InfluxDB with Go
@ -128,8 +125,8 @@ Use the Go library to query data to InfluxDB.
```js ```js
from(bucket:"<bucket>") from(bucket:"<bucket>")
|> range(start: -1h) |> range(start: -1h)
|> filter(fn: (r) => r._measurement == "stat") |> 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 sends the Flux query to InfluxDB and returns the results as a FluxRecord object with a table structure.
@ -143,20 +140,22 @@ Use the Go library to query data to InfluxDB.
- `Value`: Returns the actual field value. - `Value`: Returns the actual field value.
```go ```go
result, err := queryApi.Query(context.Background(), `from(bucket:"<bucket>")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "stat")`) result, err := queryAPI.Query(context.Background(), `from(bucket:"<bucket>")
if err == nil { |> range(start: -1h)
for result.Next() { |> filter(fn: (r) => r._measurement == "stat")`)
if result.TableChanged() { if err == nil {
fmt.Printf("table: %s\n", result.TableMetadata().String()) for result.Next() {
} if result.TableChanged() {
fmt.Printf("value: %v\n", result.Record().Value()) fmt.Printf("table: %s\n", result.TableMetadata().String())
} }
if result.Err() != nil { fmt.Printf("value: %v\n", result.Record().Value())
fmt.Printf("query parsing error: %s\n", result.Err().Error())
}
} else {
panic(err)
} }
if result.Err() != nil {
fmt.Printf("query parsing error: %s\n", result.Err().Error())
}
} else {
panic(err)
}
``` ```
**The FluxRecord object includes the following methods for accessing your data:** **The FluxRecord object includes the following methods for accessing your data:**
@ -178,9 +177,9 @@ Use the Go library to query data to InfluxDB.
// Create client // Create client
client := influxdb2.NewClient(url, token) client := influxdb2.NewClient(url, token)
// Get query client // Get query client
queryApi := client.QueryApi(org) queryAPI := client.QueryAPI(org)
// Get QueryTableResult // Get QueryTableResult
result, err := queryApi.Query(context.Background(), `from(bucket:"my-bucket")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "stat")`) result, err := queryAPI.Query(context.Background(), `from(bucket:"my-bucket")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "stat")`)
if err == nil { if err == nil {
// Iterate over query response // Iterate over query response
for result.Next() { for result.Next() {