align format
parent
7625dbf0cd
commit
d9b93463da
|
@ -38,44 +38,44 @@ We are going to write some data in [line protocol](/v2.0/reference/syntax/line-p
|
||||||
|
|
||||||
1. In your Python program, import the InfluxDB client library and use it to write data to InfluxDB.
|
1. In your Python program, import the InfluxDB client library and use it to write data to InfluxDB.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import influxdb_client
|
import influxdb_client
|
||||||
from influxdb_client.client.write_api import SYNCHRONOUS
|
from influxdb_client.client.write_api import SYNCHRONOUS
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Define a few variables with the name of your [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), and [token](/v2.0/security/tokens/).
|
2. Define a few variables with the name of your [bucket](/v2.0/organizations/buckets/), [organization](/v2.0/organizations/), and [token](/v2.0/security/tokens/).
|
||||||
|
|
||||||
```python
|
```python
|
||||||
bucket = "<my-bucket>"
|
bucket = "<my-bucket>"
|
||||||
org = "<my-org>"
|
org = "<my-org>"
|
||||||
token = "<my-token>"
|
token = "<my-token>"
|
||||||
# Store the URL of your InfluxDB instance
|
# Store the URL of your InfluxDB instance
|
||||||
url="http://localhost:9999"
|
url="http://localhost:9999"
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Instantiate the client. The `InfluxDBClient` object takes three named parameters: `url`, `org`, and `token`. Pass in the named parameters.
|
3. Instantiate the client. The `InfluxDBClient` object takes three named parameters: `url`, `org`, and `token`. Pass in the named parameters.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
client = InfluxDBClient(
|
client = InfluxDBClient(
|
||||||
url=url,
|
url=url,
|
||||||
token=token,
|
token=token,
|
||||||
org=org
|
org=org
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
The `InfluxDBClient` object has a `write_api` method used for configuration.
|
The `InfluxDBClient` object has a `write_api` method used for configuration.
|
||||||
|
|
||||||
4. Instantiate a **write client** using the `client` object and the `write_api` method. Use the `write_api` method to configure the writer object.
|
4. Instantiate a **write client** using the `client` object and the `write_api` method. Use the `write_api` method to configure the writer object.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
write_api = client.write_api(write_options=SYNCHRONOUS)
|
write_api = client.write_api(write_options=SYNCHRONOUS)
|
||||||
```
|
```
|
||||||
|
|
||||||
5. Create a [point](/v2.0/reference/glossary/#point) object and write it to InfluxDB using the `write` method of the API writer object. The write method requires three parameters: `bucket`, `org`, and `record`.
|
5. Create a [point](/v2.0/reference/glossary/#point) object and write it to InfluxDB using the `write` method of the API writer object. The write method requires three parameters: `bucket`, `org`, and `record`.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
|
p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
|
||||||
write_api.write(bucket=bucket, org=org, record=p)
|
write_api.write(bucket=bucket, org=org, record=p)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Complete example write script
|
### Complete example write script
|
||||||
|
|
||||||
|
@ -104,41 +104,41 @@ write_api.write(bucket=bucket, org=org, record=p)
|
||||||
|
|
||||||
1. Instantiate the **query client**.
|
1. Instantiate the **query client**.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
query_api = client.query_api()
|
query_api = client.query_api()
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Create a Flux query.
|
2. Create a Flux query.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
query = ‘ from(bucket:"my-bucket")\
|
query = ‘ from(bucket:"my-bucket")\
|
||||||
|> range(start: -10m)\
|
|> range(start: -10m)\
|
||||||
|> filter(fn:(r) => r._measurement == "my_measurement")\
|
|> filter(fn:(r) => r._measurement == "my_measurement")\
|
||||||
|> filter(fn: (r) => r.location == "Prague")\
|
|> filter(fn: (r) => r.location == "Prague")\
|
||||||
|> filter(fn:(r) => r._field == "temperature" )‘
|
|> filter(fn:(r) => r._field == "temperature" )‘
|
||||||
```
|
```
|
||||||
|
|
||||||
The query client sends the Flux query to InfluxDB and returns a Flux object with a table structure.
|
The query client sends the Flux query to InfluxDB and returns a Flux object with a table structure.
|
||||||
|
|
||||||
3. Pass the `query()` method two named parameters:`org` and `query`.
|
3. Pass the `query()` method two named parameters:`org` and `query`.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
result = client.query_api().query(org=org, query=query)
|
result = client.query_api().query(org=org, query=query)
|
||||||
```
|
```
|
||||||
|
|
||||||
4. Iterate through the tables and records in the Flux object.
|
4. Iterate through the tables and records in the Flux object.
|
||||||
- Use the `get_value()` method to return values.
|
- Use the `get_value()` method to return values.
|
||||||
- Use the `get_field()` method to return fields.
|
- Use the `get_field()` method to return fields.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
results = []
|
results = []
|
||||||
for table in result:
|
for table in result:
|
||||||
for record in table.records:
|
for record in table.records:
|
||||||
results.append((record.get_field(), record.get_value()))
|
results.append((record.get_field(), record.get_value()))
|
||||||
|
|
||||||
print(results)
|
print(results)
|
||||||
[(temperature, 25.3)]
|
[(temperature, 25.3)]
|
||||||
```
|
```
|
||||||
|
|
||||||
**The Flux object provides the following methods for accessing your data:**
|
**The Flux object provides the following methods for accessing your data:**
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue