edits
parent
3720054340
commit
7625dbf0cd
content/influxdb/v2.0/reference/api/client-libraries
|
@ -36,7 +36,7 @@ If just getting started, see [Get started with InfluxDB](/v2.0/get-started/).
|
|||
|
||||
Use the Go library to write and query data from InfluxDB.
|
||||
|
||||
In your Go program, import the necessary packages and specify the entry point of your executable program.
|
||||
1. In your Go program, import the necessary packages and specify the entry point of your executable program.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
@ -50,7 +50,7 @@ import (
|
|||
)
|
||||
```
|
||||
|
||||
Next, define variables for your InfluxDB [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), and [token](/v2.0/security/tokens/).
|
||||
2. Define variables for your InfluxDB [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), and [token](/v2.0/security/tokens/).
|
||||
|
||||
```go
|
||||
bucket := "example-bucket"
|
||||
|
@ -60,19 +60,19 @@ token := "example-token"
|
|||
url := "http://localhost:9999"
|
||||
```
|
||||
|
||||
To write data, create the the InfluxDB Go Client and pass in the `url` and `token` parameters.
|
||||
3. Create the the InfluxDB Go client and pass in the `url` and `token` parameters.
|
||||
|
||||
```go
|
||||
client := influxdb2.NewClient(url, token)
|
||||
```
|
||||
|
||||
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
|
||||
writeApi := client.WriteApiBlocking(org, bucket)
|
||||
```
|
||||
|
||||
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
|
||||
queryApi := client.QueryApi(org)
|
||||
|
@ -82,8 +82,9 @@ queryApi := client.QueryApi(org)
|
|||
|
||||
Use the Go library to write data to InfluxDB.
|
||||
|
||||
Create a [point](/v2.0/reference/glossary/#point) and write it to InfluxDB using the `WritePoint` method of the API writer struct.
|
||||
Close the client to flush all pending writes and finish.
|
||||
1. Create a [point](/v2.0/reference/glossary/#point) and write it to InfluxDB using the `WritePoint` method of the API writer struct.
|
||||
|
||||
2. Close the client to flush all pending writes and finish.
|
||||
|
||||
```go
|
||||
p := influxdb2.NewPoint("stat",
|
||||
|
@ -95,6 +96,7 @@ client.Close()
|
|||
```
|
||||
|
||||
### Complete example write script
|
||||
|
||||
```go
|
||||
func main() {
|
||||
bucket := "example-bucket"
|
||||
|
@ -120,7 +122,7 @@ client.Close()
|
|||
## Query data from InfluxDB with Go
|
||||
Use the Go library to query data to InfluxDB.
|
||||
|
||||
Create a Flux query and supply your `bucket` parameter.
|
||||
1. Create a Flux query and supply your `bucket` parameter.
|
||||
|
||||
```js
|
||||
from(bucket:"<bucket>")
|
||||
|
@ -167,7 +169,6 @@ The query client sends the Flux query to InfluxDB and returns the results as a F
|
|||
- `Values()`: Returns a map of column values.
|
||||
- `ValueByKey(<your_tags>)`: Returns a value from the record for given column key.
|
||||
|
||||
|
||||
### Complete example query script
|
||||
|
||||
```go
|
||||
|
|
Loading…
Reference in New Issue