LoginSignup
7
6

More than 5 years have passed since last update.

InfluxDBをPythonから使う

Posted at

InfluxDBをPythonから使う

InfluxDataがInfluxDB-Pythonというライブラリを用意してくれているのでそれを使えば良いだけ。

参考サイト

InfluxDB-Pythonのインストール

pip3 install influxdb --user
pip3 install --upgrade influxdb --user

依存関係

InfluxDB-PythonディストリビューションはPython 2.7, 3.5, 3.6, 3.7, そしてPyPyとPyPy3上ででサポートされていてテスト済みである。

注意: Python <3.5 は現在テストされていない。

主な依存関係は:

他の依存関係は:

ドキュメント

ドキュメントはココ

>>> from influxdb import InfluxDBClient
>>> json_body = [
     {
         "measurement": "cpu_load_short",
         "tags": {
             "host": "server01",
             "region": "us-west"
         },
         "time": "2009-11-10T23:00:00Z",
         "fields": {
             "value": 0.64
         }
     }
 ]
>>> 
>>> json_body
[{'measurement': 'cpu_load_short', 'tags': {'host': 'server01', 'region': 'us-west'}, 'time': '2009-11-10T23:00:00Z', 'fields': {'value': 0.64}}]
>>> client = InfluxDBClient('localhost', 8086, 'root', 'root', 'example')
>>> client
<influxdb.client.InfluxDBClient object at 0x7f4fea0c4ef0>
>>> client.create_database('example')
>>> client.write_points(json_body)
True
>>> result = client.query('select value from cpu_load_short;')
>>> print("Result: {0}".format(result))
Result: ResultSet({'('cpu_load_short', None)': [{'time': '2009-11-10T23:00:00Z', 'value': 0.64}]})

テスト

toxをインストールする:

$ pip3 install tox --user

複数のバージョンのPythonに対してinfluxdb-pythonをテストするためには、Toxを使うことが出来る:

$ tox
7
6
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
7
6