fixed comments

pull/945/head
Anaisdg 2020-04-13 14:53:35 -05:00
parent c71463c430
commit ea16202669
3 changed files with 136 additions and 97 deletions

View File

@ -1,5 +1,6 @@
---
title: Go client library
seotitle: InfluxDB Go client library
list_title: Go
description: >
Use the Go client library to interact with InfluxDB.
@ -8,30 +9,36 @@ menu:
name: Go
parent: Client libraries
v2.0/tags: [client libraries, Go]
aliases:
- /v2.0/reference/api/client-libraries/go-cl-guide/
weight: 201
---
Use the [InfluxDB Go client library](https://github.com/influxdata/influxdb-client-go) to integrate InfluxDB into Go scripts and applications.
This guide presumes some familiarity with Go and InfluxDB.
If just getting started, see [Getting started with InfluxDB](/v2.0/get-started/).
If just getting started, see [Get started with InfluxDB](/v2.0/get-started/).
## Before you begin
1. Go 1.3 or later is required.
2. Run ```go get github.com/influxdata/influxdb-client-go``` to download the client package in your $GOPATH, followed by ```go build``to build the package.
3. Ensure that InfluxDB is running.
2. Download the client package in your $GOPATH and build the package.
```sh
# Download the InfluxDB Go client package
go get github.com/influxdata/influxdb-client-go
# Build the package
go build
```
3. Ensure that InfluxDB is running and you can connect to it.
If running InfluxDB locally, visit http://localhost:9999.
(If using InfluxDB Cloud, visit the URL of your InfluxDB Cloud UI.
For example: https://us-west-2-1.aws.cloud2.influxdata.com.)
If using InfluxDB Cloud, visit your [InfluxDB Cloud URL](/v2.0/cloud/urls).
## Write data to InfluxDB with Go
## Boilerplate for the InfluxDB Go Client Library
We are going to write some data as a point using the Go library.
Use the Go library to write and query data to and from InfluxDB.
In your Go program, import the necessary packages and specify the entry point of your executable program.
In your Go program, import necessary packages and specify the entry point of our executable program.
```go
package main
@ -44,27 +51,37 @@ import (
)
```
Next, define a few variables with the name of your [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), and [token](/v2.0/security/tokens/).
Next, define variables for your InfluxDB [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), and [token](/v2.0/security/tokens/).
```go
bucket = "<my-bucket>"
org = "<my-org>"
token = "<my-token>"
```
//variable to store the url of your local or InfluxDB Cloud instance
url = "<http://localhost:9999>"
``
In order to write data, we need to create the the InfluxDB Go Client and pass in our named parameters: `url` and `token`.
To write data, create the the InfluxDB Go Client and pass in our named parameters: `url` and `token`.
```go
client := influxdb2.NewClient("http://localhost:9999", "my-token")
client := influxdb2.NewClient(url, my-token)
```
We create a write client with the `WriteApiBlocking` method and pass in our other named parameters: `org` and `bucket`.
Create a **write client** with the `WriteApiBlocking` method and pass in your other named parameters: `org` and `bucket`.
```go
writeApi := client.WriteApiBlocking("my-org", "my-bucket")
writeApi := client.WriteApiBlocking(my-org, my-bucket)
```
We need three more lines for our program to write data.
To query data, create an InfluxDB **query client** and pass in your InfluxDB `org`.
```go
queryApi := client.QueryApi(org)
```
## Write data to InfluxDB with Go
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.
@ -99,25 +116,25 @@ client.Close()
}
```
## Query data from InfluxDB with Go
Use the Go library to query data to InfluxDB.
In order to query data, we to create the client and the query client, similarly to to the write example above.
Create a flux query and supply your `bucket` parameter.
```go
client := influxdb2.NewClient("http://localhost:9999", "my-token")
queryApi := client.QueryApi("my-org")
```
Next, we create a flux query and supply our `bucket` parameter.
```flux
```js
from(bucket:"<bucket>")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "stat"
|> filter(fn: (r) => r._measurement == "stat")
```
We query the InfluxDB server with our flux query. The query client returns the results as a FluxRecord object with a table structure. The `Query` method takes our flux query.
The `Next` method iterates over our query response. The `TableChanged` method notices whe the group key has changed.
The `Record` method returns last parsed FluxRecord and gives access to value and row properties. The `Value` method returns the actual field value.
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.
```go
result, err := queryApi.Query(context.Background(), `from(bucket:"<bucket>")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "stat")`)
@ -136,21 +153,22 @@ The `Record` method returns last parsed FluxRecord and gives access to value and
}
```
The FluxRecord object has the following methods for accessing your data:
- ```Table()```: Returns index of the table record belongs to.
- ```Start()```: Returns the inclusive lower time bound of all records in the current table.
- ```Stop()```: Returns the exclusive upper 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.
- ```Measurement()```: Returns the measurement name of the record.
- ```Values()```: Returns map of the values where key is the column name.
- ```ValueByKey(<your_tags>)```: Returns value for given column key for the record.
**The FluxRecord object includes the following methods for accessing your data:**
- `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.
- `Stop()`: Returns the exclusive upper 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.
- `Measurement()`: 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
```
```go
func main() {
// Create client
client := influxdb2.NewClient("http://localhost:9999", "my-token")

View File

@ -1,5 +1,6 @@
---
title: JavaScript client library
seotitle: InfluxDB JavaScript client library
list_title: JavaScript
description: >
Use the JavaScript client library to interact with InfluxDB.
@ -8,73 +9,93 @@ menu:
name: JavaScript
parent: Client libraries
v2.0/tags: [client libraries, JavaScript]
aliases:
- /v2.0/reference/api/client-libraries/js-cl-guide/
weight: 201
---
Use the [InfluxDB JavaScript client library](https://github.com/influxdata/influxdb-client-js) to integrate InfluxDB into JavaScript scripts and applications.This client supports both browser environments and NodeJS environments.
Use the [InfluxDB JavaScript client library](https://github.com/influxdata/influxdb-client-js) to integrate InfluxDB into JavaScript scripts and applications. This client supports both client-side (browser) and server-side (NodeJS) environments.
This guide presumes some familiarity with JavaScript, browser environments, and InfluxDB.
If just getting started, see [Getting started with InfluxDB](/v2.0/get-started/).
If just getting started, see [Get started with InfluxDB](/v2.0/get-started/).
## Before you begin
1. Install NodeJS:
1. Install Go 1.3 or later](https://golang.org/doc/install)
```sh
brew install node
```
2. Ensure that InfluxDB is running.
2. Install [NodeJS](https://nodejs.org/en/download/package-manager/):
3. Ensure that InfluxDB is running and you can connect to it.
If running InfluxDB locally, visit http://localhost:9999.
(If using InfluxDB Cloud, visit the URL of your InfluxDB Cloud UI.
For example: https://us-west-2-1.aws.cloud2.influxdata.com.)
If using InfluxDB Cloud, visit your [InfluxDB Cloud URL](/v2.0/cloud/urls).
## Easiest way to get started
1. clone the [examples directory](https://github.com/influxdata/influxdb-client-js/tree/master/examples) in the [influxdb-client-js](https://github.com/influxdata/influxdb-client-js) repo.
2. Navigate to examples directory and install NPM
1. Clone the [examples directory](https://github.com/influxdata/influxdb-client-js/tree/master/examples) in the [influxdb-client-js](https://github.com/influxdata/influxdb-client-js) repo.
2. Navigate to `examples` directory and install dependencies.
{{< code-tabs-wrapper >}}
{{% code-tabs %}}
[npm](#)
[yarn](#)
{{% /code-tabs %}}
{{% code-tab-content %}}
```sh
cd examples
npm install
# Navigate into examples directory
cd examples
# Install dependencies
npm install
```
3. Update your `./env` and `index.html` with the name of your [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), [token](/v2.0/security/tokens/), and `url` which relies upon proxy to forward requests to the target InfluxDB.
{{% /code-tab-content %}}
{{% code-tab-content %}}
```sh
# Navigate into examples directory
cd examples
# Install dependencies
yarn install
```
{{% /code-tab-content %}}
{{< /code-tabs-wrapper >}}
3. Update your `./env` and `index.html` with the name of your InfluxDB [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), [token](/v2.0/security/tokens/), and `url` which relies upon proxy to forward requests to the target InfluxDB.
4. Run the following command to run the application at [http://localhost:3001/examples/index.html]()
```sh
npm run browser
```
```sh
npm run browser
```
## Write data to InfluxDB with JavaScript
## Boilerplate for the InfluxDB Javascript Client Lbrary
Use the Javascript library to write and query data to and from InfluxDB.
We are going to write some data as a point using the JavaScript library.
To write a data point to InfluxDB using the JavaScript library, import the latest InfluxDB Javascript library in your script.
In your JavaScript script, import the latest release from the NPM repository.
```js
import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client/dist/index.browser.mjs'
```
Next, define a few constant variables with the name of your [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), [token](/v2.0/security/tokens/), and `url` which relies upon proxy to forward requests to the target InfluxDB.
Next, define constants for your InfluxDB [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), [token](/v2.0/security/tokens/), and `url` which relies on a proxy to forward requests to the target InfluxDB instance.
```js
const url = '/influx'
const token = 'my-token'
const org = 'my-org'
const bucket = 'my-bucket'
const token = '<my-token>'
const org = '<my-org>'
const bucket = '<my-bucket>'
//variable to store the url of your local or InfluxDB Cloud instance
const url = 'http://localhost:9999'
```
In order to write data, we need to instantiate the InfluxDB JavaScript Client and pass in our named parameters: `url` and `token`.
Instantiate the InfluxDB JavaScript Client and pass in our named parameters: `url` and `token`.
```js
const influxDB = new InfluxDB({url, token})
const InfluxDB = new InfluxDB({url, token})
```
## Write data to InfluxDB with JavaScript
Use the Javascript library to write data to InfluxDB.
We get a write client with the `getWriteApi` method and pass in our other named parameters: `org` and `bucket`.
Use the `getWriteApi` method of the InfluxDB client to create a **write client**. Provide your InfluxDB `org` and `bucket`.
```js
const writeApi = influxDB.getWriteApi(org, bucket)
const writeApi = InfluxDB.getWriteApi(org, bucket)
```
We need three more lines for our program to write data.
The `useDefaultTags` method instructs the write api to use default tags when writing points. Create a [point](/v2.0/reference/glossary/#point) and write it to InfluxDB using the `writePoint` method. The `tag` and `floatField` methods add key value pairs for the tags and fields, respectively. Close the client to flush all pending writes and finish.
```js
@ -108,14 +129,15 @@ writeApi
```
## Query data from InfluxDB with JavaScript
Use the Javascript library to query data from InfluxDB.
We get a query client with the `getQueryApi` method and pass in our named parameter: `org`.
Use the `getQueryApi` method of the `InfluxDB` client to create a new **query client**. Provide your InfluxDB `org`.
```js
const queryApi = influxDB.getQueryApi(org)
```
Next, we create a flux query and supply our `bucket` parameter.
Create a Flux query (including your `bucket` parameter).
```js
const fluxQuery =
@ -162,6 +184,4 @@ queryApi.queryRows(fluxQuery, {
})
```
```
For more information, see the [JavaScript client README on GitHub](https://github.com/influxdata/influxdb-client-js).

View File

@ -16,7 +16,7 @@ weight: 201
Use the [InfluxDB Python client library](https://github.com/influxdata/influxdb-client-python) to integrate InfluxDB into Python scripts and applications.
This guide presumes some familiarity with Python and InfluxDB.
If just getting started, see [Getting started with InfluxDB](/v2.0/get-started/).
If just getting started, see [Get started with InfluxDB](/v2.0/get-started/).
## Before you begin
@ -48,21 +48,20 @@ Next, we define a few variables with the name of your [bucket](/v2.0/organizatio
bucket = "<my-bucket>"
org = "<my-org>"
token = "<my-token>"
#variable to store the url of your local or InfluxDB Cloud instance
url="<http://localhost:9999>"
```
In order to write data, we need to instantiate the client. The `InfluxDBClient` object takes three named parameters: `url`, `org`, and `token`. Pass in the named parameters.
Instantiate the client. The `InfluxDBClient` object takes three named parameters: `url`, `org`, and `token`. Pass in the named parameters.
```python
client = InfluxDBClient(
url="http://localhost:9999",
url=url,
token=token,
org=org
)
```
The `InfluxDBClient` object has a `write_api` method, used for configuration.
Instantiate a writer object using the `client` object and the `write_api` method.
Use the `write_api` method to configure the writer object.
The `InfluxDBClient` object has a `write_api` method, used for configuration. Instantiate a **write client** using the `client` object and the `write_api` method. Use the `write_api` method to configure the writer object.
```python
write_api = client.write_api(write_options=SYNCHRONOUS)
@ -101,13 +100,13 @@ write_api.write(bucket=bucket, org=org, record=p)
```
## Query data from InfluxDB with Python
In order to query data, we need to instantiate the query client.
To query data, instantiate the **query client**.
```python
query_api = client.query_api()
```
Next, we create a flux query.
Next, create a flux query.
```python
query = from(bucket:"my-bucket")\
@ -117,13 +116,15 @@ query = from(bucket:"my-bucket")\
|> filter(fn:(r) => r._field == "temperature" )
```
We query the InfluxDB server with our flux query. The query client returns the results as a Flux Object with a table structure. The query() method takes two parameters: `org` and `query`.
The query client sends the Flux query to InfluxDB and returns a Flux Object with a table structure. The `query()` method takes two named parameters:`org` and `query`.
```python
result = client.query_api().query(org=org, query=query)
```
We iterate through the tables and records in the Flux Object to return our values and fields using the `get_value()` and `get_field()` methods, respectively.
Iterate through the tables and records in the Flux Object.
Use the `get_value()` method to return values.
Use the `get_field()` method to return fields.
```python
results = []
for table in result:
@ -134,16 +135,16 @@ print(results)
[(temperature, 25.3)]
```
The Flux Object has the following methods for accessing your data:
- ```get_measurement()```: Returns the measurement name of the record.
- ```get_field()```: Returns the field name.
- ```get_values()```: Returns the actual field value.
- ```values()```: Returns map of the values where key is the column name.
- ```values.get(“<your tags>”)```: Returns value for given column key for the record.
- ```get_time()```: Returns the time of the record.
- ```get_start()```: Returns the inclusive lower time bound of all records in the current table.
- ```get_stop()```: Returns the exclusive upper time bound of all records in the current table.
**The Flux Object provides the following methods for accessing your data:**
- `get_measurement()`: Returns the measurement name of the record.
- `get_field()`: Returns the field name.
- `get_values()`: Returns the actual field value.
- `values()`: Returns a map of column values.
- `values.get("<your tag>")`: Returns a value from the record for given column.
- `get_time()`: Returns the time of the record.
- `get_start()`: Returns the inclusive lower time bound of all records in the current table.
- `get_stop()`: Returns the exclusive upper time bound of all records in the current table.
### Complete example query script