TreasureDataの古橋さん達が開発しているFluentdのバッチ処理版、(調べずに言っています)、であるEmbulkを動かしてみたのでメモしておきます。
環境
- OS: Mac X 10.9.3
- java version: "1.8.0_40"
インストール
Githubに記載している方法でインストールします。
$ curl --create-dirs -o ~/.embulk/bin/embulk -L https://bintray.com/artifact/download/embulk/maven/embulk-0.5.0.jar
$ chmod +x ~/.embulk/bin/embulk
$ echo 'export PATH="$HOME/.embulk/bin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc
$ which embulk
/Users/hogehoge/.embulk/bin/embulk
$
ディレクトリを作成して、実行権限を与えて、パスを設定しています。
Examples
Githubに記載されているTrying examples
に記載されている手順を実施してみます。
example
このコマンドで例となるデータと設定ファイルを作成しているみたいですね。
$ embulk example ./try1
2015-03-05 13:50:08.074 +0900: Embulk v0.5.0
Creating ./try1 directory...
Creating ./try1/
Creating ./try1/csv/
Creating ./try1/csv/sample_01.csv.gz
Creating ./try1/example.yml
Run following subcommands to try embulk:
1. guess ./try1/example.yml -o config.yml
2. preview config.yml
3. run config.yml
$
ymlとデータが作成されているっぽいですね。
まずはymlの中身を見てみます。
in:
type: file
path_prefix: "/Users/hogehoge/try1/csv/sample_"
out:
type: stdout
非常にシンプルでいいですね。
次にデータの中身を見てみます。
$ gzcat ./try1/csv/sample_01.csv.gz
id,account,time,purchase,comment
1,32864,2015-01-27 19:23:49,20150127,embulk
2,14824,2015-01-27 19:01:23,20150127,embulk jruby
3,27559,2015-01-28 02:20:02,20150128,"Embulk ""csv"" parser plugin"
4,11270,2015-01-29 11:54:36,20150129,NULL
$
CSV形式ですね。
guess
次にguessというサブコマンドを実行してみます。
$ embulk guess ./try1/example.yml -o config.yml
2015-03-05 13:55:44.984 +0900: Embulk v0.5.0
2015-03-05 13:55:45.698 +0900 [INFO] (guess): Listing local files at directory '/Users/aueno/workspace/tmp/try1/csv' filtering filename by prefix 'sample_'
2015-03-05 13:55:45.705 +0900 [INFO] (guess): Loading files [/Users/aueno/workspace/tmp/try1/csv/sample_01.csv.gz]
/Users/aueno/.embulk/bin/embulk!/embulk/guess/schema_guess.rb:81 warning: already initialized constant TRUE_STRINGS
/Users/aueno/.embulk/bin/embulk!/embulk/guess/schema_guess.rb:89 warning: already initialized constant TYPE_COALESCE
in:
type: file
path_prefix: /Users/hogehoge/try1/csv/sample_
decoders:
- {type: gzip}
parser:
charset: UTF-8
newline: CRLF
type: csv
delimiter: ','
quote: '"'
escape: ''
null_string: 'NULL'
header_line: true
columns:
- {name: id, type: long}
- {name: account, type: long}
- {name: time, type: timestamp, format: '%Y-%m-%d %H:%M:%S'}
- {name: purchase, type: timestamp, format: '%Y%m%d'}
- {name: comment, type: string}
exec: {}
out: {type: stdout}
Created 'config.yml' file.
$
出力の最後の方を見るとパースをしれくれているみたいですね。
カラム名とデータの型を定義してくれているのかな。日付のフォーマットも推測してくれるのはいいですね。
作成されたconfig.yml
は以下。
in:
type: file
path_prefix: /Users/aueno/workspace/tmp/try1/csv/sample_
decoders:
- {type: gzip}
parser:
charset: UTF-8
newline: CRLF
type: csv
delimiter: ','
quote: '"'
escape: ''
null_string: 'NULL'
header_line: true
columns:
- {name: id, type: long}
- {name: account, type: long}
- {name: time, type: timestamp, format: '%Y-%m-%d %H:%M:%S'}
- {name: purchase, type: timestamp, format: '%Y%m%d'}
- {name: comment, type: string}
exec: {}
out: {type: stdout}
内容は先ほど標準出力の後半部分に出力された内容と同じ。
preview
さてguessの結果には問題なさそうだったので、previewというサブコマンドを実行してみる。
$ embulk preview config.yml
2015-03-05 14:01:35.855 +0900: Embulk v0.5.0
2015-03-05 14:01:36.529 +0900 [INFO] (preview): Listing local files at directory '/Users/aueno/workspace/tmp/try1/csv' filtering filename by prefix 'sample_'
2015-03-05 14:01:36.532 +0900 [INFO] (preview): Loading files [/Users/aueno/workspace/tmp/try1/csv/sample_01.csv.gz]
+---------+--------------+-------------------------+-------------------------+----------------------------+
| id:long | account:long | time:timestamp | purchase:timestamp | comment:string |
+---------+--------------+-------------------------+-------------------------+----------------------------+
| 1 | 32,864 | 2015-01-27 19:23:49 UTC | 2015-01-27 00:00:00 UTC | embulk |
| 2 | 14,824 | 2015-01-27 19:01:23 UTC | 2015-01-27 00:00:00 UTC | embulk jruby |
| 3 | 27,559 | 2015-01-28 02:20:02 UTC | 2015-01-28 00:00:00 UTC | Embulk "csv" parser plugin |
| 4 | 11,270 | 2015-01-29 11:54:36 UTC | 2015-01-29 00:00:00 UTC | |
+---------+--------------+-------------------------+-------------------------+----------------------------+
$
テーブル出力でわかりやすいですね。
ただUTCの時刻ではなくJSTの時刻を表示させたいと思ったのでちょっと変更します。
srcを見るとdefault_timezone
で指定できるっぽいので以下のように変更した。
$ diff config.yml config.yml.org
9d8
< default_timezone: Asia/Tokyo
$
設定後に再度previewコマンド実行。
$ embulk preview config.yml
2015-03-05 14:49:28.434 +0900: Embulk v0.5.0
2015-03-05 14:49:29.108 +0900 [INFO] (preview): Listing local files at directory '/Users/aueno/workspace/tmp/try1/csv' filtering filename by prefix 'sample_'
2015-03-05 14:49:29.112 +0900 [INFO] (preview): Loading files [/Users/aueno/workspace/tmp/try1/csv/sample_01.csv.gz]
+---------+--------------+-------------------------+-------------------------+----------------------------+
| id:long | account:long | time:timestamp | purchase:timestamp | comment:string |
+---------+--------------+-------------------------+-------------------------+----------------------------+
| 1 | 32,864 | 2015-01-27 10:23:49 UTC | 2015-01-26 15:00:00 UTC | embulk |
| 2 | 14,824 | 2015-01-27 10:01:23 UTC | 2015-01-26 15:00:00 UTC | embulk jruby |
| 3 | 27,559 | 2015-01-27 17:20:02 UTC | 2015-01-27 15:00:00 UTC | Embulk "csv" parser plugin |
| 4 | 11,270 | 2015-01-29 02:54:36 UTC | 2015-01-28 15:00:00 UTC | |
+---------+--------------+-------------------------+-------------------------+----------------------------+
$
JSTでは表示されなかったですが、入力データを日本時間として認識してUTCの時間に変換してくれているのでいいかと。
結局Unixタイムでは同じなので。
run
runコマンドを実行すると以下のように表示されるはずです。
$ embulk run config.yml
2015-03-05 14:53:02.995 +0900: Embulk v0.5.0
2015-03-05 14:53:04.202 +0900 [INFO] (transaction): Listing local files at directory '/Users/aueno/workspace/tmp/try1/csv' filtering filename by prefix 'sample_'
2015-03-05 14:53:04.206 +0900 [INFO] (transaction): Loading files [/Users/aueno/workspace/tmp/try1/csv/sample_01.csv.gz]
2015-03-05 14:53:04.303 +0900 [INFO] (transaction): {done: 0 / 1, running: 0}
1,32864,2015-01-27 10:23:49,20150126,embulk
2,14824,2015-01-27 10:01:23,20150126,embulk jruby
3,27559,2015-01-27 17:20:02,20150127,Embulk "csv" parser plugin
4,11270,2015-01-29 02:54:36,20150128,
2015-03-05 14:53:04.385 +0900 [INFO] (transaction): {done: 1 / 1, running: 0}
2015-03-05 14:53:04.407 +0900 [INFO] (main): Committed.
2015-03-05 14:53:04.408 +0900 [INFO] (main): Next config diff: {"in":{"last_path":"/Users/aueno/workspace/tmp/try1/csv/sample_01.csv.gz"},"out":{}}
uenoakirasurudo-no-MacBook-Pro:tmp aueno$
$
outのプラグインがstdoutなので上記のように出力されます。
利用可能なプラグイン
Embulkでは設定によってinput,outputを切り替えることができる柔軟な設計になっています。
利用可能なプラグインは以下のコマンドで参照することができます。
$ embulk gem list -r embulk-
2015-03-05 15:48:51.004 +0900: Embulk v0.5.0
*** REMOTE GEMS ***
embulk-decoder-commons-compress (0.3.0)
embulk-formatter-jsonl (0.1.3)
embulk-input-apache-dummy-log (0.1.0)
embulk-input-command (0.1.2)
embulk-input-gcs (0.1.1)
embulk-input-jdbc (0.3.0)
embulk-input-mysql (0.3.0)
embulk-input-pcapng-files (0.1.2)
embulk-input-postgresql (0.3.0)
embulk-input-random (0.0.2)
embulk-input-redshift (0.3.0)
embulk-input-s3 (0.1.3)
embulk-input-slack-history (0.1.1)
embulk-output-command (0.1.2)
embulk-output-elasticsearch (0.1.3)
embulk-output-jdbc (0.2.0)
embulk-output-mysql (0.2.0)
embulk-output-parquet (0.1.0)
embulk-output-postgres-json (0.2.0)
embulk-output-postgresql (0.2.0)
embulk-output-redshift (0.2.0)
embulk-parser-pcapng (0.1.0)
embulk-plugin-filter-convert (0.0.1)
embulk-plugin-filter-grep (0.0.2)
embulk-plugin-input-hbase (0.0.2 java, 0.0.1)
embulk-plugin-input-jstat (0.0.2)
embulk-plugin-input-pcapng-files (0.0.2)
embulk-plugin-input-random (0.0.2)
embulk-plugin-input-roo-excel (0.1.1)
embulk-plugin-input-sfdc-event-log-files (0.0.7)
embulk-plugin-mysql (0.0.3)
embulk-plugin-redis (0.1.5)
embulk-plugin-sqlite3 (0.0.1)
embulk-plugin-twitterstream (0.0.2)
embulk-plugin-vertica (0.0.2)
embulk-plugin-vim (0.0.2)
$
個人的にはembulk-parser-pcapng
が気になりますね。
次はプラグインの利用方法について記載したいと思います。