fix(client): Fixing Go client guide reflect latest state
parent
c332dde043
commit
117b5c99fe
|
@ -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/).
|
||||||
|
@ -48,7 +45,7 @@ Use the Go library to write and query data from InfluxDB.
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
influxdb2 "github.com/influxdata/influxdb-client-go"
|
"github.com/influxdata/influxdb-client-go/v2"
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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,14 +90,14 @@ 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"
|
||||||
|
@ -109,14 +106,14 @@ Use the Go library to write data to InfluxDB.
|
||||||
// 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()
|
||||||
}
|
}
|
||||||
|
@ -143,8 +140,10 @@ 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)
|
||||||
|
|> filter(fn: (r) => r._measurement == "stat")`)
|
||||||
|
if err == nil {
|
||||||
for result.Next() {
|
for result.Next() {
|
||||||
if result.TableChanged() {
|
if result.TableChanged() {
|
||||||
fmt.Printf("table: %s\n", result.TableMetadata().String())
|
fmt.Printf("table: %s\n", result.TableMetadata().String())
|
||||||
|
@ -154,9 +153,9 @@ Use the Go library to query data to InfluxDB.
|
||||||
if result.Err() != nil {
|
if result.Err() != nil {
|
||||||
fmt.Printf("query parsing error: %s\n", result.Err().Error())
|
fmt.Printf("query parsing error: %s\n", result.Err().Error())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
panic(err)
|
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() {
|
||||||
|
|
Loading…
Reference in New Issue