embulkからelasticsearch2系にデータを流す

この記事はembulk-output-elasticsearchプラグインが正式に2系をサポートする前にとりあえずlocalで試したいという人向けです。

追記

以下の記事のやり方の方がスマートだったので載せておきます。

qiita.com

追記ここまで

fileから大量データをelasticsearchに読み込ませるためにembulkを使います。

install embulk

linux環境でQuick Start通りにやってすんなりと入りました。

github.com

$ curl --create-dirs -o ~/.embulk/bin/embulk -L "http://dl.embulk.org/embulk-latest.jar"
$ chmod +x ~/.embulk/bin/embulk
$ echo 'export PATH="$HOME/.embulk/bin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc

$ embulk --version
embulk 0.7.10

embulk-output-elasticsearch

elasticsearchプラグインは以下のものを使います。

github.com

現在、リリースされているv0.1.8はelasticsearch v1.5.2を使っており、2系に対応していません。 リポジトリには2系対応がコミットされているのでとりあえず試したい場合はそれを使うとよいです。

$ embulk gem install embulk-output-elasticsearch
$ cd ~/.embulk/jruby/2.2.0/gems/embulk-output-elasticsearch-0.1.8
$ git init
$ git remote add origin https://github.com/muga/embulk-output-elasticsearch.git
$ git pull origin master
$ ./gradlew build
$ ./gradlew classpath

example

embulkではexample用のデータセットが簡単に得られるのでそれで試してみます。

$ embulk example ./try1
$ embulk guess   ./try1/example.yml -o config.yml
$ embulk preview config.yml

設定の一部をelasticsearch向けのものに変更します。

in:
  type: file
  path_prefix: /home/vagrant/try1/csv/sample_
  decoders:
  - {type: gzip}
  parser:
    charset: UTF-8
    newline: CRLF
    type: csv
    delimiter: ','
    quote: '"'
    trim_if_not_quoted: false
    skip_header_lines: 1
    allow_extra_columns: false
    allow_optional_columns: false
    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}
out:
  type: elasticsearch
  nodes:
  - {host: localhost, port: 9300}
  cluster_name: your-cluster-name
  index: try1
  index_type: test

これを実行してみます。

$ embulk run config.yml
2015-12-07 18:10:27.686 +0900: Embulk v0.7.10
2015-12-07 18:10:31.394 +0900 [INFO] (transaction): Loaded plugin embulk-output-elasticsearch (0.1.8)
2015-12-07 18:10:31.524 +0900 [INFO] (transaction): Listing local files at directory '/home/vagrant/try1/csv' filtering filename by prefix 'sample_'
2015-12-07 18:10:31.532 +0900 [INFO] (transaction): Loading files [/home/vagrant/try1/csv/sample_01.csv.gz]
2015-12-07 18:10:31.777 +0900 [INFO] (transaction): [Luke Cage] loaded [], sites []
2015-12-07 18:10:32.986 +0900 [INFO] (transaction): {done:  0 / 1, running: 0}
2015-12-07 18:10:33.006 +0900 [INFO] (task-0000): [Air-Walker] loaded [], sites []
2015-12-07 18:10:34.031 +0900 [INFO] (task-0000): Execute 4 bulk actions
2015-12-07 18:10:34.564 +0900 [INFO] (elasticsearch[Air-Walker][listener][T#1]): 4 bulk actions succeeded
2015-12-07 18:10:34.665 +0900 [INFO] (transaction): {done:  1 / 1, running: 0}
2015-12-07 18:10:34.678 +0900 [INFO] (main): Committed.
2015-12-07 18:10:34.678 +0900 [INFO] (main): Next config diff: {"in":{"last_path":"/home/vagrant/try1/csv/sample_01.csv.gz"},"out":{}}

無事、try1にデータが入りました。

※ちゃんと使う場合はelasticsearchプラグインがリリースされるまで待った方が良いと思います。