InfluxDB v1.8: API の使い方

参考ページ
[InfluxDB API reference](https://docs.influxdata.com/influxdb/v1.8/tools/api/)

## データの書き込み

```sh:write.sh
#
URL="http://example.com:8086/write?db=example&precision=s"
#
curl -i -XPOST $URL \
	--data-binary 'Data,temperature=20.1 IDX=1'
#
sleep 1
curl -i -XPOST $URL \
	--data-binary 'Data,temperature=21.8 IDX=1'
#
sleep 1
curl -i -XPOST $URL \
	--data-binary 'Data,temperature=22.3 IDX=1'
#
````

結果

```text
$ influx -precision rfc3339
Connected to http://localhost:8086 version 1.8.10
InfluxDB shell version: 1.8.10
> use example
Using database example
> select * from Data
name: Data
time                 IDX temperature
----                 --- -----------
2022-12-06T10:39:01Z 1   20.1
2022-12-06T10:39:03Z 1   21.8
2022-12-06T10:39:04Z 1   22.3
>
```

HTTPie を使う

```bash:http_write.sh
URL="http://example.com:8086/write?db=example"
#
http POST $URL --raw 'Data,temperature=20.1 IDX=1'
#
sleep 1
http POST $URL --raw 'Data,temperature=21.8 IDX=1'
#
sleep 1
http POST $URL --raw 'Data,temperature=22.3 IDX=1'
```

Python3 を使う

```py:write.py
#! /usr/bin/python
#
#	write.py
#
#						Dec/07/2022
#
# ------------------------------------------------------------------
import	sys
import	json
import	requests
import	time
# ------------------------------------------------------------------
def write_proc(temp_in):
	print(temp_in)
	url="http://example.com:8086/write?db=example"
	raw_data='Data,temperature=%f IDX=1' % temp_in
	rr=requests.post(url,data=raw_data)
	print(rr)
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
temps=[20.3,21.4,22.5,23.7]
for temp in temps:
	write_proc(temp)
	time.sleep(1.0)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
```

## データの読み取り

```bash:select.sh
#
curl -G 'http://example.com:8086/query?db=example' \
       	--data-urlencode 'q=SELECT * FROM Data'
```

JSON を読みやすく表示

```bash
#
curl -G 'http://example.com:8086/query?db=example&pretty=true' \
       	--data-urlencode 'q=SELECT * FROM Data'
```

HTTPie を使う

```bash:http_select.sh
URL='http://example.com:8086/query?db=example'
#
http $URL'&q=SELECT * FROM Data'
```

Python3 を使う

```py:select.py
#! /usr/bin/python
#
#	select.py
#
#						Dec/07/2022
#
# ------------------------------------------------------------------
import	sys
import	json
import	requests
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url="http://example.com:8086/query?db=example"
url_target=url+'&q=SELECT * FROM Data'
print(url_target)
args={}
#
rr=requests.post(url_target,args)
dict_data = json.loads(rr.text)
#
print(dict_data)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
```


## Measurement の削除

```bash:drop_measurement.sh
URL="http://example.com:8086/query?db=example"
#
curl -XPOST $URL \
	--data-urlencode 'q=DROP measurement "Data"'
```

HTTPie を使う

```bash:http_drop.sh
URL='http://examploe.com:8086/query?db=example'
#
http POST $URL'&q=DROP measurement "Data"'
```

Python3 を使う

```py:drop_measurement.py
#! /usr/bin/python
#
#	drop_measurement.py
#
#						Dec/07/2022
#
# ------------------------------------------------------------------
import	sys
import	json
import	requests
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url="http://example.com:8086/query?db=example"
url_target=url+'&q=DROP measurement "Data"'
print(url_target)
args={}
#
rr=requests.post(url_target,args)
dict_data = json.loads(rr.text)
#
print(dict_data)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
```