ようへいの日々精進XP

よかろうもん

スクショで振り返る Hosted InfluxDB 48 分チュートリアル(InfluxDB を触ってみたぜよ 2015)

tl;dr

inokara.hateblo.jp

こんなミーハー感満載の記事を書いて一年以上経過した今朝、以下のようなトゥイートに気づく。

おお、InfluxDB もホスティング時代に突入か!ということでサインアップして 60 分以内で試せる範囲で試してみた。


参考

有難うございます。


チュートリアル

サインアップ

https://customers.influxdb.com/ にアクセス。

f:id:inokara:20150926074653p:plain

必要事項を記入するとメールが届く。

f:id:inokara:20150926074754p:plain

届いたメールに記載されている「Confirm my account」をクリックしてサインアップ完了。

InfluxDB ノードの作成

Hosted InfluxDB は InfluxDB ノードの作成から始める。

f:id:inokara:20150927000800p:plain

お試しは 10GB のストレージを持った 1 ノードで提供される。

f:id:inokara:20150927000831p:plain

ノードを作成するリージョンを選択してノード作成に着手。

f:id:inokara:20150927000853p:plain

暫くお待ち下さい...

f:id:inokara:20150927000923p:plain

作成が終わると InfluxDB のエンドポイント、Web Admin URL と Grafana の URL がメールで送られてくる。

f:id:inokara:20150927000943p:plain

また、Web コンソール上にも同様の情報が表示されているのでリンクをクリックすることで Web Admin 又は Grafana にアクセスすることが出来る。

Ruby で操作する

InfluxDB の Ruby クライアントで特に難しいことをしなくても Ruby から InfluxDB を操作することが出来るので...

github.com

早速、Hosted InfluxDB を Ruby で操作してみたのが以下の教材。

github.com

InfluxDB にデータベースを作成してデータを 1 秒毎に放り込んでいく。

loop do
  data = {
    values: { value: rand(1..1000) },
    tags: { foo: "bar" }
  }

  p data
  influxdb.write_point(name, data)

  sleep 1
end

ハッシュにした datawrite_point メソッドで放り込む。

Grafana で確認

Hosted InfluxDB セットアップ後、Grafana も既にセットアップされており Grafana の Datasource に登録されている。

f:id:inokara:20150926234354p:plain

後はメトリクスをポチポチするだけで以下のようにサクッとメトリクスを確認出来る。

f:id:inokara:20150926234650p:plain

Influx Shell で操作する

InfluxDB が提供するコマンドラインツールがあるようなのでそちらも試してみる。コマンド一発で DB への接続、クエリ発行、結果取得まで出来るようだが、MySQL のようにシェルモード(勝手に命名)も備えているのが嬉しい。

influxdb.com

試した環境は以下の通り。

% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11
BuildVersion:   15A278b

インストールは brew にて。

% brew install influxdb

インストールしたらヘルプを確認。

% influx -help
Usage of influx:
  -version
       Display the version and exit.
  -host 'host name'
       Host to connect to.
  -port 'port #'
       Port to connect to.
  -database 'database name'
       Database to connect to the server.
  -password 'password'
      Password to connect to the server.  Leaving blank will prompt for password (--password '').
  -username 'username'
       Username to connect to the server.
  -ssl
        Use https for requests.
  -execute 'command'
       Execute command and quit.
  -format 'json|csv|column'
       Format specifies the format of the server responses:  json, csv, or column.
  -consistency 'any|one|quorum|all'
       Set write consistency level: any, one, quorum, or all
  -pretty
       Turns on pretty print for the json format.
  -import
       Import a previous database export from file
  -pps
       How many points per second the import will allow.  By default it is zero and will not throttle importing.
  -path
       Path to file to import
  -compressed
       Set to true if the import file is compressed

Examples:

    # Use influx in a non-interactive mode to query the database "metrics" and pretty print json:
    $ influx -database 'metrics' -execute 'select * from cpu' -format 'json' -pretty

    # Connect to a specific database on startup and set database context:
    $ influx -database 'metrics' -host 'localhost' -port '8086'

influx コマンドでシェルモードに。connect コマンドで InfluxDB に接続し操作を行う。

% influx
Connected to http://localhost:8086 version 
InfluxDB shell 0.9.4.1
> help
Usage:
        connect <host:port>   connect to another node
        auth                  prompt for username and password
        pretty                toggle pretty print
        use <db_name>         set current databases
        format <format>       set the output format: json, csv, or column
        consistency <level>   set write consistency level: any, one, quorum, or all
        settings              output the current settings for the shell
        exit                  quit 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 tag values       show tag value information

        a full list of influxql commands can be found at:
        https://influxdb.com/docs/v0.9/query_language/spec.html

Hosted InfluxDB に繋いでみる。

# ホスト名、ポート番号、データベース名、ユーザー名、パスワード、SSL での接続を指定して influx コマンドを起動
% influx \
 -host 'xxxxxxxxxxxxxxxxxxxxxxx.c.influxdb.com' \
 -port '8086' \
 -database 'hage_pika' \
 -password 'xxxxxxxxxxxxxxx' \
 -username 'influxdb' \
 -ssl 'true'
Connected to https://xxxxxxxxxxxxxxxxxxxxxxx.c.influxdb.com:8086 version 0.9.4.1
InfluxDB shell 0.9.4.1
# influx シェルモード
>

シェルモードでクエリを投げてみる。

# 引き続き Influx シェルモード
> SELECT value FROM foobar;

(snip)

2015-09-26T13:27:08Z    447
2015-09-26T13:27:09Z    437
2015-09-26T13:34:02Z    469
2015-09-26T13:34:04Z    285
2015-09-26T13:34:05Z    507

調子に乗って LIMIT 5 とかつけると...

> SELECT value FROM foobar LIMIT 5
name: foobar
------------
time                    value
2015-09-26T11:26:56Z    0
2015-09-26T11:26:58Z    0.9983341664682815
2015-09-26T11:26:59Z    1.9866933079506122
2015-09-26T11:27:01Z    2.9552020666133956
2015-09-26T11:27:02Z    3.8941834230865053

# こんなんクエリもイケる
> SELECT value FROM foobar ORDER BY time DESC LIMIT 3;
name: foobar
------------
time                    value
2015-09-26T11:34:24Z    31
2015-09-26T11:34:23Z    39
2015-09-26T11:34:22Z    676

おお。


終わり

Hosted InfluxDB は自分で InfluxDB と Grafana を構築、運用する手間を省きたい場合やには導入を検討して良いのかもしれない。デフォルトで HTTPSAPI にアクセス出来るのでちょっと安心。但し、以下の点が気になった。

  • リージョンが限定されている(日本から一番近そうなのが Singapore リージョン)→今後に期待?
  • お値段

f:id:inokara:20150927000320p:plain

お試しで利用している Singaore リージョンで 1 ノード、ストレージ 10GB で 50 ドル弱となっている。