Open2

InfluxDB入門

ピン留めされたアイテム
otokunaga2otokunaga2

時系列データベース(InfluxDB)入門

データ構造の最上位にはdatabaseがある。その下にmeasurementがあり、一番下には1つの時系列データを表すfieldがある。fieldのキー(ラベル)をfield_keyと呼ぶ。
データはデータベース→mesurementという単位で書き込むことになっている。

参考1: https://helve-blog.com/posts/database/infludb-introduction/
参考2:(めちゃくちゃ整理されていて分かりやすい):
[https://www.magata.net/memo/index.php?InfluxDB����]
参考3: https://zenn.dev/ktrueda/articles/influxdb-0002

InfluxQLというSQLlikeな文法でアクセスできる
※ただし、データの解析については以下の公式HPにも記載されている通り、文法がかなり豊富である。INTEGRAL()などの関数もサポートされている。

SELECT INTEGRAL("water_level") FROM "h2o_feet" WHERE "location" = 'santa_monica' AND  time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z'

センサデータの解像度を下げてストレージを逼迫しないように設定もできる。
https://alpacat.com/blog/influxdb-down-sampling

S3へのバックアップ

EC2やDokcerコンテナで実施している記事を発見した

This container periodically runs a backup of an InfluxDB database to an S3 bucket. It also has the ability to restore.

pandasとの連携

influxdb-pythonを利用してPandasのDataFrameを用いて操作することもできる。 詳細なAPIについては公式ドキュメントを参照のこと。

InfluxDB Python API

demo.py
# PandasのDataFrameを書き込むには、write_pointsというAPIを利用する
# DataFrameClient.write_points(dataframe, measurement,database=None)

array = np.arange(20).reshape(-1, 2)
index = pd.date_range(pd.Timestamp("20200401"), freq="1D", periods=10)
df = pd.DataFrame(array, index=index, columns=["A", "B"])  

client.write_points(df, "meas1", database="pd_test")

基本的な使い方

  • データベースの作成
    • create database demo1
  • 利用するDBの指定
    • use demo1
    • MySQLのようにデータベースを指定してからクエリコマンドを実行する

データ保持の期間について

InfluxDB にデータベースを作成すると autogen という名前の保持期間無期限のポリシーが作成されます。作成されているポリシーを確認するには、「 SHOW RETENTION POLICIES 」コマンドを実行します。
https://www.bnote.net/centos/influxdb_basic_usage01.html

# 1時間のポリシーを設定する
CREATE RETENTION POLICY "one_hour" ON "factory_env" DURATION 1h REPLICATION 1 DEFAULT

https://helve-blog.com/posts/database/infludb-introduction/

認証認可

The InfluxDB API and the command line interface (CLI), which connects to the database using the API, include simple, built-in authentication based on user credentials. When you enable authentication, InfluxDB only executes HTTP requests that are sent with valid credentials.

詳細はHP Authorization参照のこと

管理ユーザの権限

Admin users have READ and WRITE access to all databases and full access to the following administrative queries:

  • CREATE DATABASE
  • DROP DATABASE
  • DROP SERIES
  • DROP MEASUREMENT
  • CREATE RETENTION POLICY
  • ALTER RETENTION POLICY
  • DROP RETENTION POLICY
  • CREATE CONTINUOUS QUERY
  • DROP CONTINUOUS QUERY

通常ユーザの権限(非管理ユーザ)

Non-admin users can have one of the following three privileges per database:
READ, WRITE, ALL (both READ and WRITE access)

otokunaga2otokunaga2

python clientを利用してJSONデータはそのまま追記できる

client.py
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
        }
    }
]

client = InfluxDBClient('localhost', 8086, 'root', 'root', 'example')

client.create_database('example')

client.write_points(json_body)

result = client.query('select value from cpu_load_short;')

print("Result: {0}".format(result))