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: python で接続

Last updated at Posted at 2018-11-13

こちらで作成したデータベースに python で接続します。
InfluxDB の cli の使い方

influxdb_query.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	influxdb_query.py
#
#						Nov/13/2018
# ------------------------------------------------------------------
import sys
import requests
import json

# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
args = {
	"q": 'SELECT * FROM "tochigi"'
	}
url = "http://localhost:8086/query?db=city"
#
rr=requests.get(url,args)
# print(rr.text)
dict_aa = json.loads(rr.text)
# print(dict_aa.keys())
llx = len(dict_aa['results'])
sys.stderr.write("len(dict_aa['results']) = %d\n" % llx)
# print(dict_aa['results'][0])
# print()
# print(dict_aa['results'][0]['series'])
lly = len(dict_aa['results'][0]['series'])
sys.stderr.write("lly = %d\n" % lly)
# print(dict_aa['results'][0]['series'][0])
# print()
array_values = dict_aa['results'][0]['series'][0]['values']
llz = len(array_values)
sys.stderr.write("llz = %d\n" % llz)
print(array_values)
print()
#
for unit in array_values:
	print(unit[0],unit[1],unit[2],unit[3],unit[4])
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./influxdb_query.py 
*** 開始 ***
len(dict_aa['results']) = 1
lly = 1
llz = 3
[['2018-11-13T08:14:33.488107961Z', '2003-8-12', 't0921', '宇都宮', 34981], ['2018-11-13T08:14:39.379152598Z', '2003-7-20', 't0922', '小山', 51982], ['2018-11-13T08:14:44.529336419Z', '2003-9-8', 't0923', '佐野', 75984]]

2018-11-13T08:14:33.488107961Z 2003-8-12 t0921 宇都宮 34981
2018-11-13T08:14:39.379152598Z 2003-7-20 t0922 小山 51982
2018-11-13T08:14:44.529336419Z 2003-9-8 t0923 佐野 75984
*** 終了 ***

次のバージョンで確認しました。

$ influx -version
InfluxDB shell version: 1.8.10

$ python --version
Python 3.10.7
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?