InfluxDB v2: Python Client の使い方 （時系列データ)

## 準備
トークンを調べる

```bash
influx auth list
```

バケットの作成 (dbtest というバケットを作成）

```bash
influx bucket create -n dbtest -r 72h
```

バケットの削除

```bash
influx bucket delete -n dbtest
```

バケットの一覧

```bash
influx bucket list
```

ライブラリーのインストール

```bash
pip3 install influxdb-client
```

環境設定ファイルの作成

```text:.env
bucket = "dbtest"
org = "ekzemplaro"
token = '***-_nfenN6m5LhKUXNcrQ_H3dZSQo2pVpyyJg6nskHEwcl6bMsjc1vyzY9b11hSyGiQMOg=='
url="http://localhost:8086"
```

バケットの初期化

```bash:bucket_reset.sh
influx bucket delete -n dbtest
influx bucket create -n dbtest -r 72h
#
influx bucket list
```

## データの書き込み

```py:write_temperature.py
#! /usr/bin/python
#
#	write_temperature.py
#
#						Jan/24/2023
#
# ------------------------------------------------------------------
import	os
import	sys
import	json
import	requests
import	time
from dotenv import load_dotenv
# ------------------------------------------------------------------
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
# ------------------------------------------------------------------
def write_proc(id,temp_in):
	print(temp_in)
	pp = influxdb_client.Point(id).field("temperature", temp_in)
	write_api.write(bucket=bucket, org=org, record=pp)
# ------------------------------------------------------------------
dotenv_path = '.env'
load_dotenv(dotenv_path)
bucket = os.environ.get("bucket")
org = os.environ.get("org")
token = os.environ.get("token")
#
url = os.environ.get("url")
#
sys.stderr.write("*** 開始 ***\n")
client = influxdb_client.InfluxDBClient(
	url=url,
	token=token,
	org=org
)

write_api = client.write_api(write_options=SYNCHRONOUS)

# ------------------------------------------------------------------
#
temps=[[20.2,23.4,25.4],[23.4,24.9,26.9],[21.7,24.4,26.5],[22.2,24.9,25.1]]
index=0
for temp_uu in temps:
	write_proc("id01",temp_uu[0])
	write_proc("id02",temp_uu[1])
	write_proc("id03",temp_uu[2])
	time.sleep(3.0)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
```

実行結果

```text
$ ./write_temperature.py 
*** 開始 ***
20.2
23.4
25.4
23.4
24.9
26.9
21.7
24.4
26.5
22.2
24.9
25.1
*** 終了 ***
```

## データの読み込み

```py:select_temperature.py
#! /usr/bin/python
#
#	select_temperature.py
#
#					Jan/24/2023
# ------------------------------------------------------------------
import os
import sys
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
from dotenv import load_dotenv
# ------------------------------------------------------------------
dotenv_path = '.env'
load_dotenv(dotenv_path)
bucket = os.environ.get("bucket")
org = os.environ.get("org")
token = os.environ.get("token")
#
url = os.environ.get("url")

sys.stderr.write("*** 開始 ***\n")
client = influxdb_client.InfluxDBClient(
	url=url,
	token=token,
	org=org
)

query_api = client.query_api()
query = 'from(bucket:"' + bucket + '") |> range(start: -2h)'
#
result = query_api.query(org=org, query=query)
print(result)
print()
results = []
for table in result:
	for record in table.records:
		results.append((record.get_time().strftime("%Y-%m-%d %H:%M:%S"),record.get_field(), record.get_value(),record.get_measurement()))

#
for unit in results:
	print(unit)
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
```

実行結果

```text
$ ./select_temperature.py 
*** 開始 ***
[<FluxTable: 8 columns, 4 records>, <FluxTable: 8 columns, 4 records>, <FluxTable: 8 columns, 4 records>]

('2023-01-25 01:26:00', 'temperature', 20.2, 'id01')
('2023-01-25 01:26:03', 'temperature', 23.4, 'id01')
('2023-01-25 01:26:06', 'temperature', 21.7, 'id01')
('2023-01-25 01:26:09', 'temperature', 22.2, 'id01')
('2023-01-25 01:26:00', 'temperature', 23.4, 'id02')
('2023-01-25 01:26:03', 'temperature', 24.9, 'id02')
('2023-01-25 01:26:06', 'temperature', 24.4, 'id02')
('2023-01-25 01:26:09', 'temperature', 24.9, 'id02')
('2023-01-25 01:26:00', 'temperature', 25.4, 'id03')
('2023-01-25 01:26:03', 'temperature', 26.9, 'id03')
('2023-01-25 01:26:06', 'temperature', 26.5, 'id03')
('2023-01-25 01:26:09', 'temperature', 25.1, 'id03')
*** 終了 ***
```

## 確認したバージョン

```text
$ influx version
Influx CLI 2.6.1 (git: 61c5b4d) build_date: 2022-12-29T15:41:09Z

$ python --version
Python 3.10.7
```

## データを Grafana で表示
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/880504c7-04d2-dd24-7179-7bf08ab1829e.png)



## 関連情報
[InfluxDB v2: Python Client の使い方](https://qiita.com/ekzemplaro/items/fae139282170ced0f4b0)