0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

InfluxDB v1.8: API の使い方

Last updated at Posted at 2022-12-06

参考ページ
InfluxDB API reference

データの書き込み

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'
#

結果

$ 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 を使う

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 を使う

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")
# ------------------------------------------------------------------

データの読み取り

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

JSON を読みやすく表示

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

HTTPie を使う

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

Python3 を使う

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 の削除

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

HTTPie を使う

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

Python3 を使う

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")
# ------------------------------------------------------------------
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?