tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

td-agent(fluentd)の運用でTreasure Data使ってみたメモ④ - tdコマンドでいろいろやってみる

はじめに

①~③でいくつかやりましたが、
td-agentをインストールした際にtdコマンドが利用できるようになっています。
という話の、うる覚自分用メモ!

treasure ataの関連記事はこちら

① - とにかく使ってみる
② - Application Supportを使ってみる(fluent-logger-perl)
③ - Application Supportを使ってみる(fluent-logger-php)

補足

そんなにテクニカルでも有用でもないカモですがこちらにメモまとめをメモっておきました →td-agent(fluentd)を本番サ―ビスで小さく使ってみたメモ-まとめ

流れ

  1. tdコマンドのインストール&アカウント認証
  2. Treasure Dataに入ってるsampleに対してqueryってみる
  3. データベース新規作成してみる

1. tdコマンドのインストール&アカウント認証

インストール

まず、インストールについてですがtd-agentで入れた場合は同梱のようなので不要です。
もし無い場合はインストールを実行

sudo /usr/lib64/fluent/ruby/bin/gem install td
認証

tdコマンドを使うためにアカウントの認証を行います
td account -fと打つとTreasure Dataに登録したアドレスとパスワードを聞かれるので答えるだけです

# td account -f
Enter your Treasure Data credentials.
Email: hogehoge@hoge.com
Password (typing will be hidden):
Authenticated successfully.
Use 'td db:create <db_name>' to create a database.

2. Treasure Dataに入ってるsampleに対してqueryってみる

自分でcreate tableってからでも良いのですが、参照のほうがイメージしやすいと思うので参照系からやってみます。

  • dtabaseやtable、jobを見てみる
  • help見る
  • いくつかHiveクエリ

dtabaseやtable、jobを見てみる

database
# td databases
+-----------+-------+
| Name      | Count |
+-----------+-------+
| sample_db | 5000  |
| test_db   | 6     |
+-----------+-------+
2 rows in set

td dbsでもいけますし、td database:listでも同じ結果になります

table
# td tables
+-----------+------------+------+-------+--------+---------------------------+---------------------------+----------------------------------------------------------------------------------------------------------+
| Database  | Table      | Type | Count | Size   | Last import               | Last log timestamp        | Schema                                                                                                   |
+-----------+------------+------+-------+--------+---------------------------+---------------------------+----------------------------------------------------------------------------------------------------------+
| sample_db | test_table | log  | 0     | 0.0 GB |                           |                           |                                                                                                          |
| sample_db | www_access | log  | 5,000 | 0.0 GB | 2014-05-16 20:53:53 +0900 | 2013-09-07 10:13:45 +0900 | host:string, path:string, method:string, referer:string, code:long, agent:string, user:string, size:long |
| test_db   | follow     | log  | 1     | 0.0 GB | 2014-05-19 22:36:22 +0900 | 2014-05-19 22:33:54 +0900 | to:string, from:string                                                                                   |
| test_db   | test_table | log  | 5     | 0.0 GB | 2014-05-19 22:35:56 +0900 | 2014-05-19 22:33:54 +0900 | entry1:string, entry2:long, hello:string                                                                 |
+-----------+------------+------+-------+--------+---------------------------+---------------------------+----------------------------------------------------------------------------------------------------------+
4 rows in set

こちらもtd table:lisでもいけます
また、td table:lis sample_dbとデータベースを指定することもできます

job
+----------+---------+---------------------------+---------------+----------+--------+------+-----------+------------------------------+
| JobID    | Status  | Start                     | Elapsed       | Priority | Result | Type | Database  | Query                        |
+----------+---------+---------------------------+---------------+----------+--------+------+-----------+------------------------------+
| 10606546 | success | 2014-05-20 15:02:18 +0900 |         46sec | NORMAL   |        | hive | sample_db | SELECT code, COUNT(1) AS ... |
| 10580080 | success | 2014-05-19 16:24:29 +0900 |      1m  2sec | NORMAL   |        | hive | sample_db | SELECT \r\n  code AS http... |
| 10580079 | success | 2014-05-19 16:23:34 +0900 |      1m 13sec | NORMAL   |        | hive | sample_db | SELECT \r\n  code AS http... |
| 10580076 | success | 2014-05-19 16:23:17 +0900 |      1m 12sec | NORMAL   |        | hive | sample_db | SELECT \r\n  code AS http... |
| 10580073 | killed  | 2014-05-19 16:22:54 +0900 |          9sec | NORMAL   |        | hive | sample_db | SELECT \r\n  code AS http... |
| 10580010 | success | 2014-05-19 16:16:08 +0900 |      1m  9sec | NORMAL   |        | hive | sample_db | SELECT \r\n  code AS http... |
| 10579982 | success | 2014-05-19 16:14:33 +0900 |      1m  5sec | NORMAL   |        | hive | sample_db |  SELECT\r\n   code AS htt... |
| 10578959 | success | 2014-05-19 15:10:22 +0900 |         46sec | NORMAL   |        | hive | sample_db | SELECT code, COUNT(1) AS ... |
| 10578461 | success | 2014-05-19 14:46:45 +0900 |      1m  4sec | NORMAL   |        | hive | sample_db |  SELECT\r\n   code AS htt... |
| 10499851 | success | 2014-05-16 20:56:54 +0900 |         45sec | NORMAL   |        | hive | sample_db | SELECT code, COUNT(1) AS ... |
+----------+---------+---------------------------+---------------+----------+--------+------+-----------+------------------------------+

td job:listでも同じ結果です

help見る

helpで結構教えてくれます。
たとえばtd job:???について知りたいとき

※job:???はどんなのがあるかhelp
# td help job
Additional commands, type "td help COMMAND" for more details:

  job:show <job_id>                          # Show status and result of a job
  job:status <job_id>                        # Show status progress of a job
  job:list [max]                             # Show list of jobs
  job:kill <job_id>                          # Kill or cancel a job

※'kill'があると知って'kill'の使い方を見る
usage:
  $ td job:kill <job_id>

example:
  $ td job:kill 1461

description:
  Kill or cancel a job

いくつかHiveクエリ

ほとんどお試しレベルですがselectしてみます

  • 適当にselect
  • pathでグルーピングして多い順に上位3件を表示
  • 期間指定して、pathでグルーピングして多い順に上位3件を表示
適当にselect
カラムを指定してselect
# td query -w -d sample_db \
  "SELECT v['time'], v['path'], v['code'] FROM www_access LIMIT 3"

Job 10611863 is queued.
Use 'td job:show 10611863' to show the status.
queued...
  started at 2014-05-20T11:36:08Z
  Hive history file=/mnt/hive/tmp/xxxx/hive_job_log__133208641.txt
  **

・・・省略・・・

  Total CPU Time: 2670
  Time taken: 16.399 seconds
Status     : success
Result     :
+------------+----------------------+------+
| time       | path                 | code |
+------------+----------------------+------+
| 1378511996 | /category/networking | 200  |
| 1378511987 | /category/books      | 200  |
| 1378511978 | /item/finance/3775   | 200  |
+------------+----------------------+------+
3 rows in set
カラムを指定&時間をフォーマットしてselect
# td query -w -d sample_db \
  "SELECT TD_TIME_FORMAT(time, 'yyyy-MM-dd', 'PDT') AS day, v['path'], v['code'] FROM www_access  LIMIT 3"
Result     :
+------------+-----------------------+-----+
| day        | _c1                   | _c2 |
+------------+-----------------------+-----+
| 2013-09-06 | /category/software    | 200 |
| 2013-09-06 | /category/electronics | 200 |
| 2013-09-06 | /category/jewelry     | 200 |
+------------+-----------------------+-----+
3 rows in set
where句を指定してselect
# td query -w -d sample_db \
  "SELECT v['time'], v['path'], v['code'] FROM www_access WHERE v['code']=500"
Result     :
+------------+---------------------+-----+
| _c0        | _c1                 | _c2 |
+------------+---------------------+-----+
| 1378514706 | /category/software  | 500 |
| 1378514699 | /item/software/2754 | 500 |
+------------+---------------------+-----+
2 rows in set
pathでグルーピングして多い順に上位3件を表示
# td query -w -d sample_db \
  "SELECT v['path'] AS path, COUNT(1) AS cnt \
   FROM www_access \
   GROUP BY v['path'] ORDER BY cnt DESC LIMIT 3"
   
Result     :
+-----------------------+-----+
| path                  | cnt |
+-----------------------+-----+
| /category/electronics | 639 |
| /category/software    | 416 |
| /category/books       | 285 |
+-----------------------+-----+
3 rows in set
期間指定して、pathでグルーピングして多い順に上位3件を表示
# td query -w -d sample_db \
  "SELECT v['path'] AS path, COUNT(1) AS cnt \
   FROM www_access \
   WHERE TD_TIME_RANGE(time, '2013-09-06 04:23:20', '2013-09-06 04:23:30', 'PDT') \
   GROUP BY v['path'] ORDER BY cnt DESC LIMIT 3"
   
Result     :
+------------------+-----+
| path             | cnt |
+------------------+-----+
| /item/music/1001 | 1   |
+------------------+-----+
1 row in set

3. データベース新規作成してみる

こちらもサンプルに載ってるようなことですがさらっと流し勧めます

  • db作成、table作成
  • sample作成コマンドでサンプル作成
  • 作成したサンプルをimport

db作成、table作成

db作成
# td db:create hoge_db
Database 'hoge_db' is created.
Use 'td table:create hoge_db <table_name>' to create a table.

次に何するか(今回はtable作る)も教えてくれてるのでそのままtable作成

table作成
# td table:create hoge_db www_access
Table 'hoge_db.www_access' is created.

sample作成コマンドでサンプル作成

サンプル作るコマンドですが、
サンプルのjsonのファイルを作成するので作成できる権限がある場所でやります。

# td sample:apache apache.json

# ls -al
-rw-r--r--  1 hoge hoge 1335502  5月 20 21:44 2014 apache.json

作成したサンプルをimport

# td table:import hoge_db www_access --json apache.json
importing apache.json...
  uploading 117309 bytes...
  imported 5000 entries from apache.json.
done.
helpをちょっとみる

helpを見るとjson意外にもimportの形式が選べます

# td help table:import
usage:
  $ td table:import <db> <table> <files...>

example:
  $ td table:import example_db table1 --apache access.log
  $ td table:import example_db table1 --json -t time - < test.json

description:
  Parse and import files to a table

options:
      --format FORMAT              file format (default: apache)
      --apache                     same as --format apache; apache common log format
      --syslog                     same as --format syslog; syslog
      --msgpack                    same as --format msgpack; msgpack stream format
      --json                       same as --format json; LF-separated json format
  -t, --time-key COL_NAME          time key name for json and msgpack format (e.g. 'created_at')
      --auto-create-table          Create table and database if doesn't exist

まとめ

ってことで、だいたいはサンプル流してみた程度ですが
こんな感じってわかれば程度のメモでした

補足でサンプル:Command-line Interface