#author("2019-12-08T12:39:11+00:00","","") #mynavi() #setlinebreak(on); * 概要 [#jafbab15] #html(<div class="pl10">) オープンソースの時系列データベースである InfluxDB の簡単な使用方法等について記載する。 #html(</div>) * 目次 [#cd6e33ac] #contents * インストール/起動 [#fec51e12] #html(<div class="pl10">) ここでは公開されているdockerイメージを使用する事とする。 作業用ディレクトリ作成 #myterm2(){{ mkdir myinfluxdb cd myinfluxdb }} influxdb 及び chronograf 用のディレクトリ作成 #myterm2(){{ mkdir influxdb mkdir etc/myinfluxdb mkdir chronograf }} influxdb 用の設定ファイル作成 ※デフォルト設定でよければ作らなくても良い。 ./etc/influxdb/influxdb.conf #mycode2(){{ [meta] dir = "/var/lib/influxdb/meta" [data] dir = "/var/lib/influxdb/data" engine = "tsm1" wal-dir = "/var/lib/influxdb/wal" [http] auth-enabled = true }} docker-compose.yml #mycode2(){{ version: "3" services: influxdb: image: influxdb volumes: - ./influxdb:/var/lib/influxdb - ./etc/influxdb:/etc/influxdb ports: - 8086:8086 chronograf: image: chronograf:alpine volumes: - ./chronograf:/var/lib/chronograf ports: - 8888:8888 links: - influxdb depends_on: - influxdb }} #TODO(telegraf など足りない) 起動 #myterm2(){{ docker-compose up }} 手っ取り早く必要なものが揃うサンドボックス用の docker-compose.yml が公開されているので、そちらを使用しても良い。 https://github.com/influxdata/sandbox #html(</div>) * コマンドを使用して操作する [#l2f4e579] #html(<div class="pl10">) コンテナIDを確認 #myterm2(){{ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES baeed6a9bce9 chronograf:alpine "/entrypoint.sh chro…" 4 minutes ago Up 3 minutes 0.0.0.0:8888->8888/tcp influxdb_chronograf_1 e66c42c48178 influxdb "/entrypoint.sh infl…" 4 minutes ago Up 3 minutes 0.0.0.0:8086->8086/tcp influxdb_influxdb_1 }} コンテナに入る #myterm2(){{ docker exec -it e66c42c48178 /bin/bash }} InfluxDB shellを起動する #myterm2(){{ influx -precision rfc3339 Connected to http://localhost:8086 version 1.7.9 InfluxDB shell version: 1.7.9 > }} ※ユーザ作成後は influx -username 'user名' -password 'パスワード' -precision rfc3339 で接続可能。 以降は InfluxDB shell から InfluxQL を使用して各種操作を行う事ができる。 詳細は、以下の URL 及び 後述を参照。 | - | ドキュメントURL |h | コマンドラインIF | https://docs.influxdata.com/influxdb/v1.7/tools/shell/ | | データベース管理 | https://docs.influxdata.com/influxdb/v1.7/query_language/database_management/ | | データ検索 | https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/ | ** データベースの作成 データの登録/検索まで [#j9a6aaa9] #html(<div class="pl10">) 細かなコマンドの詳細は後述する事として、まずはデータベースを作成し、データの登録/検索を行う所までを行ってみる。 データベースの作成 #myterm2(){{ > create database mydb1 }} データベースの一覧を確認 #myterm2(){{ > show databases name: databases name ---- _internal mydb1 }} 作成したデータベースに切り替える #myterm2(){{ > use mydb1 Using database mydb1 }} データを登録する #myterm2(){{ > insert sample,server=server1,ym=201912 load_average=0.5 }} データを検索する #myterm2(){{ > select * from sample name: sample time load_average server ym ---- ------------ ------ -- 2019-12-08T11:00:54.1441902Z 0.5 server1 201912 }} #html(</div>) #html(</div>) * 主なオブジェクトなど [#f82a4dbf] #html(<div class="pl10">) https://docs.influxdata.com/influxdb/v1.7/concepts/glossary/ ** Retention policy [#e61d8a22] #html(<div class="pl10">) InfluxDB では、データを保持する期間、クラスターに保存するデータのコピーの数(レプリケーションファクター)、および シャードグループがカバーする時間範囲などを名前を付けて Retention policy として定義する事ができる。 Retention policy は各レコードの insert 時にレコード毎に指定する事ができ、省略された場合はデフォルトの Retention policy が使用される。 ※ NoSQL や キーバリュー型のデータストアでいう所の TTL のような物。 #html(</div>) ** Measurement [#e17a856b] #html(<div class="pl10">) RDBでいう所のテーブルのようなもの。 ただし、InfluxDB ではレコード毎に列が不揃いでもよく列定義も不要な為か、事前にテーブル定義などは必要無い。 #html(</div>) ** Series [#ga075544] #html(<div class="pl10">) #TODO #html(</div>) ** Tag [#g58d16db] #html(<div class="pl10">) レコードにおけるキー項目。 insert 時に指定する事ができる。 #html(</div>) ** Field [#c17865fa] #html(<div class="pl10">) レコードにおけるキー以外の項目。 insert 時に指定する事ができる。 #html(</div>) ** Timestamp [#nc65740e] #html(<div class="pl10">) レコードに必ず付与する日時列。(省略時は現在日時が使用される) 検索時は time 列として表示される。 #html(</div>) ** データ型 [#ca6e11c8] #html(<div class="pl10">) https://docs.influxdata.com/influxdb/v1.7/write_protocols/line_protocol_reference/#data-types | データ型 | 説明 |h | Float | 64ビット浮動小数点数。デフォルトの数値型 。| | Integer | 符号付き64ビット整数(-9223372036854775808から9223372036854775807)。数値の末尾にiを付けて整数を指定する。例:1i | | String | 文字列。Measurements, tag keys, tag values, field keys, field values で使用できる。最大 64KB。 | | Boolean | 真偽値。真は [t、T、true、True、TRUE]。 偽は [f、F、false、False、FALSE] で表現する。| | Timestamp | Unixナノ秒タイムスタンプ。 | #html(</div>) #html(</div>) * 操作方法 (管理/登録系) [#e8e1011d] #html(<div class="pl10">) https://docs.influxdata.com/influxdb/v1.7/administration/authentication_and_authorization/ https://docs.influxdata.com/influxdb/v1.7/query_language/database_management/ https://docs.influxdata.com/influxdb/v1.7/query_language/schema_exploration/ https://docs.influxdata.com/influxdb/v1.7/tools/shell/ ** ユーザの作成 [#y90d7bef] #html(<div class="pl10">) #mycode2(){{ CREATE USER <username> WITH PASSWORD '<password>' WITH ALL PRIVILEGES # 管理者ユーザの場合 }} #html(</div>) ** データベースの作成 [#f385b608] #html(<div class="pl10">) コマンド #mycode2(){{ CREATE DATABASE <database_name> [WITH [DURATION <duration>] [REPLICATION <n>] [SHARD DURATION <duration>] [NAME <retention-policy-name>]] }} 使用例) #myterm2(){{ > create database mydb1 > show databases name: databases name ---- _internal mydb1 }} #html(</div>) ** データベースの削除 [#z2363d5c] #html(<div class="pl10">) コマンド #mycode2(){{ DROP DATABASE <database_name> }} 使用例 ) #myterm2(){{ > drop database mydb1 > show databases name: databases name ---- _internal }} #html(</div>) ** 計測データの作成 [#f8cdc28c] #html(<div class="pl10">) RDBのように事前にテーブルを作成する必要はなく、データを直接投入する事ができる。 また retention policy を省略した場合は、デフォルトの retention policy が使用される。 #mycode2(){{ INSERT INTO [retention policy] <measurement>[,<tag_key>=<tag_value>[,<tag_key>=<tag_value>]] <field_key>=<field_value>[,<field_key>=<field_value>] [<timestamp>] }} https://docs.influxdata.com/influxdb/v1.7/write_protocols/line_protocol_reference/ *** 注意 [#zee0cc4f] (1) INSERT時に文字列をクォーテーションで囲む必要はない。 insert時にクォーテーションで囲うとクォーテーションを込みで登録される為、特別な理由がない限りクォーテーションで囲う必要はない。 #myterm2(){{ > insert sample,server=server1,ym=201912 load_average=0.5 > insert sample,server='server1',ym=201912 load_average=1.0 > > select * from sample name: sample time load_average server ym ---- ------------ ------ -- 2019-12-08T11:11:48.5337018Z 0.5 server1 201912 2019-12-08T11:11:57.214271Z 1 'server1' 201912 }} server='server1' のデータのみを検索。 #myterm2(){{ > select * from sample where server='server1' name: sample time load_average server ym ---- ------------ ------ -- 2019-12-08T11:11:48.5337018Z 0.5 server1 201912 }} server='\'server1\'' のデータのみを検索。(クォーテーションまで条件に含めて検索) #myterm2(){{ > select * from sample where server='\'server1\'' name: sample time load_average server ym ---- ------------ ------ -- 2019-12-08T11:11:57.214271Z 1 'server1' 201912 }} (2) field 値には数値以外は指定できない。 #myterm2(){{ > insert sample,server=server1,ym=201912 load_average='0.5' ERR: {"error":"unable to parse 'sample,server=server1,ym=201912 load_average='0.5'': invalid boolean"} > insert sample,server=server1,ym=201912 load_average="0.5" ERR: {"error":"partial write: field type conflict: input field \"load_average\" on measurement \"sample\" is type string, already exists as type float dropped=1"} }} #html(</div>) ** 計測データの削除 [#k557db20] #html(<div class="pl10">) #mycode2(){{ DROP MEASUREMENT <measurement_name> もしくは DELETE FROM <measurement_name> where .... }} #html(</div>) ** RETENTION POLICY の作成 [#dde42157] #html(<div class="pl10">) #mycode2(){{ CREATE RETENTION POLICY <retention_policy_name> ON <database_name> DURATION <duration> REPLICATION <n> [SHARD DURATION <duration>] [DEFAULT] }} #html(</div>) ** RETENTION POLICY の削除 [#dfb68183] #html(<div class="pl10">) #mycode2(){{ DROP RETENTION POLICY <retention_policy_name> ON <database_name> }} #html(</div>) #html(</div>) * 操作方法 (検索系) [#de4b19f8] #html(<div class="pl10">) https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/ ** 計測データの検索 [#d7136032] #html(<div class="pl10">) RDB のように select 文を from 句 や where 句 を使用して記述する事ができる。 また group by や limit, offset も使用可能。 #mycode2(){{ SELECT <field_key>[,<field_key>,<tag_key>] FROM <measurement_name>[,<measurement_name>] SELECT_clause FROM_clause WHERE <conditional_expression> [(AND|OR) <conditional_expression> [...]] }} 尚、from 句は以下の形式で指定する事が可能。 | 構文 | 説明 |h | FROM <measurement_name> | 現在のDBの指定した measurement からデフォルトのリテンションポリシーのデータを検索。 | | FROM <database_name>.<retention_policy_name>.<measurement_name> | 指定した DB の 指定した measurement から指定したリテンションポリシーのデータを検索 | | FROM <database_name>..<measurement_name> | 指定した DB の指定した measurement からデフォルトのリテンションポリシーのデータを検索 | #html(</div>) ** 計測データから別の計測データを作成する [#r6b12fbf] #html(<div class="pl10">) #mycode2(){{ SELECT_clause INTO <measurement_name> FROM_clause [WHERE_clause] [GROUP_BY_clause] }} 尚、into 句は以下の形式で指定する事が可能。 | 構文 | 説明 |h | INTO <measurement_name> | 指定した measurement にinsert | | INTO <database_name>.<retention_policy_name>.<measurement_name> | 指定した db の measurement に、指定したリテンションポリシーで insert | | INTO <database_name>..<measurement_name> | 指定した db の measurement に、デフォルトのリテンションポリシーで insert | | INTO <database_name>.<retention_policy_name>.:MEASUREMENT | FROM句の正規表現に一致する、ユーザー指定のデータベースと保持ポリシーのすべての測定値にデータを書き込む | #html(</div>) #html(</div>) * CLI から使用できるコマンド [#ub76ace6] #html(<div class="pl10">) help を入力すると使用できるコマンドが表示される。 使用頻度の高い use や show などはもちろん、表示/整形に有用な pretty や format なども覚えておきたい。 https://docs.influxdata.com/influxdb/v1.7/tools/shell/ https://docs.influxdata.com/influxdb/v1.7/query_language/schema_exploration/ #myterm2(){{ > help Usage: connect <host:port> connects to another node specified by host:port auth prompts for username and password pretty toggles pretty print for the json format chunked turns on chunked responses from server chunk size <size> sets the size of the chunked responses. Set to 0 to reset to the default chunked size use <db_name> sets current database format <format> specifies the format of the server responses: json, csv, or column precision <format> specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns consistency <level> sets write consistency level: any, one, quorum, or all history displays command history settings outputs the current settings for the shell clear clears settings such as database or retention policy. run 'clear' for help exit/quit/ctrl+d quits the influx shell show databases show database names show series show series information show measurements show measurement information show tag keys show tag key information show field keys show field key information A full list of influxql commands can be found at: https://docs.influxdata.com/influxdb/latest/query_language/spec/ }} 以下、サンプルをいくつか記載する。 ** データベースの一覧 [#sc0701dc] #myterm2(){{ show databases }} ** measurement の一覧 [#o28fc611] #myterm2(){{ show measurements }} ** tag の一覧 [#b3719481] #myterm2(){{ show tag keys show tag keys from <measurement> }} ** field の一覧 [#of1bc3ef] #myterm2(){{ show field keys show field keys from <measurement> }} ** retention policy の一覧 [#vc9dd5ea] #myterm2(){{ show retention policies }} #html(</div>) * Web画面(Chronograf)から操作する [#v1b84721] #html(<div class="pl10">) #TODO #html(<!--) ** ユーザ作成 [#db23580f] #html(<div class="pl10">) 事前にユーザを作成しておく。 https://docs.influxdata.com/influxdb/v1.7/administration/authentication_and_authorization/#user-management-commands #myterm2(){{ CREATE USER ユーザ名 WITH PASSWORD 'パスワード' WITH ALL PRIVILEGES }} #html(</div>) ** ブラウザから localhost:8888 にアクセス [#v94ce5d5] #html(<div class="pl10">) #html(</div>) #html(-->) #html(</div>)