LoginSignup
68
40

More than 5 years have passed since last update.

【Embulk】使い方あれこれ

Last updated at Posted at 2017-12-17

概要

ディップAdventCalendar2017、18日目です。

アドベントカレンダー初参加です。
拙い記事ですが、宜しくお願いいたします。

最近データ周りの開発を行っていて、そこでCSVファイルの加工と転送でEmbulkというツールを導入することになりました。
Embulk関連の記事はそこまで多いわけではなく、なかなか導入まで苦労したので、更に理解を深める為に、今回アドベントカレンダーにEmbulk導入から実用的な使い方(小技?に近いですが..)を紹介したいと思います。

Embulkとは?

Embulkは、ファイルやデータベースからデータ抽出を行い、別のストレージやデータベースにデータ転送するためのツールです。
今回は内容量が多いので、ここでの詳しい説明は割愛させていただきます。
詳しくはこちらの開発者である古橋さんの記事をご覧ください。

Embulk環境構築

早速手を動かしながらやってみましょう。
環境は下記です。

種類 バージョン
OS centos6.7
embulk 0.8.35
java 1.8.0_144
ruby 2.3.2

仮想環境はVagrantを使用しています
ハンズオン形式でみたい方はこちらの【Embulk】Vagrantで環境構築をご覧ください。

これだけでまずはEmbulkを動かす準備ができます。

使い方

前提条件

csvファイルの中身

sample_01.csv
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 filter
4,11270,2015-01-29 11:54:36,20150129,embulk plugin

config.yml

config.yml
in:
  type: file
  path_prefix: '/home/vagrant/sample/csv/sample_01.csv'
  parser:
    charset: UTF-8
    newline: LF
    type: csv
    delimiter: ','
    quote: '"'
    escape: '"'
    null_string: "NULL"
    trim_if_not_quoted: false
    skip_header_lines: 1
    allow_extra_columns: false
    allow_optional_columns: false
    default_timezone: 'Asia/Tokyo'
    columns:
    - {name: id, type: string}
    - {name: account, type: string}
    - {name: time, type: string}
    - {name: purchase, type: string}
    - {name: comment, type: string}
out: {type: stdout}

プラグイン導入方法

環境構築でできたのはあくまで、Embulkを動かすところまで、Embulkは様々なプラグインを入れることで真骨頂を発揮します。
こちらがEmbulkのプラグインです。

プラグインの分類は全部で3つです。
種類はたくさんあり、自分もまだ氷山の一角程度しか触ったことないです。。。。

inputプラグイン

DBなどからのデータ抽出の為のプラグインです。
RDBMS、NoSQL、クラウドサービスからのデータ抽出のプラグインもあります。

outputプラグイン

DBなどへのデータ転送の為のプラグインです。
RDBMS、NoSQL、クラウドサービスへのデータ転送のプラグインもあります。

filterプラグイン

抽出したデータのデータ加工の為のプラグインです。

プラグインインストール

肝心のプラグインの導入ですが、至って簡単でインストールしたいプラグイン名を下記コマンドの{プラグイン名}に指定して、実行するだけ

embulk gem install {プラグイン名}

今回はfilterプラグインのembulk-filter-ruby_procをインストールします。

$ embulk gem install embulk-filter-ruby_proc
2017-12-08 11:16:41.609 +0000: Embulk v0.8.36

********************************** INFORMATION **********************************
  Join us! Embulk-announce mailing list is up for IMPORTANT annoucement such as
    compatibility-breaking changes and key feature updates.
  https://groups.google.com/forum/#!forum/embulk-announce
*********************************************************************************


Gem plugin path is: /home/vagrant/.embulk/jruby/2.3.0

Successfully installed embulk-filter-ruby_proc-0.7.0
1 gem installed

また、プラグインの検索も行うことができます。

プラグイン検索

embulk gem search {検索したい文字列} -r

$ embulk gem search embulk-filter -r
2017-12-08 10:51:13.844 +0000: Embulk v0.8.36
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
                     省略
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜

*** REMOTE GEMS ***

embulk-filter-add_time (0.2.0)
embulk-filter-affix (0.1.0)
embulk-filter-amazon_rekognition (0.1.2)
embulk-filter-azure_computer_vision_api (0.1.3)
embulk-filter-azure_text_analytics (0.2.0)
embulk-filter-azure_translator_api (0.1.0)
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
                     省略
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜

実行コマンド

guess

【Embulk】Vagrantで環境構築という記事をご覧になった方はご存知でしょうが、設定ファイルの自動生成をしてくれます。

$ embulk guess seed.yml -o {任意のyamlファイル名}

実際はseed.ymlのinputタスクのpath_prefixに指定したファイルの設定ファイルを自動生成背てくれています。

preview

dryrunコマンドです。
実行する前にdryrunをしてdebugを行うことができます。

正常系

$ embulk preview config.yml
2017-12-08 11:18:47.404 +0000: Embulk v0.8.36

********************************** INFORMATION **********************************
  Join us! Embulk-announce mailing list is up for IMPORTANT annoucement such as
    compatibility-breaking changes and key feature updates.
  https://groups.google.com/forum/#!forum/embulk-announce
*********************************************************************************

2017-12-08 11:18:52.876 +0000 [INFO] (0001:preview): Listing local files at directory '/home/vagrant/sample/csv' filtering filename by prefix 'sample_01.csv'
2017-12-08 11:18:52.879 +0000 [INFO] (0001:preview): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-12-08 11:18:52.883 +0000 [INFO] (0001:preview): Loading files [/home/vagrant/sample/csv/sample_01.csv]
2017-12-08 11:18:52.896 +0000 [INFO] (0001:preview): Try to read 32,768 bytes from input source
+-----------+----------------+---------------------+-----------------+----------------+
| id:string | account:string |         time:string | purchase:string | comment:string |
+-----------+----------------+---------------------+-----------------+----------------+
|         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 filter |
|         4 |          11270 | 2015-01-29 11:54:36 |        20150129 |  embulk plugin |
+-----------+----------------+---------------------+-----------------+----------------+

異常系

  • csvファイルを想定外の形式に修正する
sample_01.csv
- id,account,time,purchase,comment
+ id,account,time,purchase
- 1,32864,2015-01-27 19:23:49,20150127,embulk
+ 1,32864,2015-01-27 19:23:49,20150127
- 2,14824,2015-01-27 19:01:23,20150127,embulk jruby
+ 2,14824,2015-01-27 19:01:23,20150127
- 3,27559,2015-01-28 02:20:02,20150128,embulk-filter
+ 3,27559,2015-01-28 02:20:02,20150128
- 4,11270,2015-01-29 11:54:36,20150129,plugin
+ 4,11270,2015-01-29 11:54:36,20150129
  • dryrun
$ embulk preview config.yml
2017-12-08 11:22:27.829 +0000: Embulk v0.8.36

********************************** INFORMATION **********************************
  Join us! Embulk-announce mailing list is up for IMPORTANT annoucement such as
    compatibility-breaking changes and key feature updates.
  https://groups.google.com/forum/#!forum/embulk-announce
*********************************************************************************

2017-12-08 11:22:33.387 +0000 [INFO] (0001:preview): Listing local files at directory '/home/vagrant/sample/csv' filtering filename by prefix 'sample_01.csv'
2017-12-08 11:22:33.393 +0000 [INFO] (0001:preview): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-12-08 11:22:33.400 +0000 [INFO] (0001:preview): Loading files [/home/vagrant/sample/csv/sample_01.csv]
2017-12-08 11:22:33.413 +0000 [INFO] (0001:preview): Try to read 32,768 bytes from input source
2017-12-08 11:22:33.638 +0000 [WARN] (0001:preview): Skipped line 2 (Too few columns): 1,32864,2015-01-27 19:23:49,20150127
2017-12-08 11:22:33.639 +0000 [WARN] (0001:preview): Skipped line 3 (Too few columns): 2,14824,2015-01-27 19:01:23,20150127
2017-12-08 11:22:33.639 +0000 [WARN] (0001:preview): Skipped line 4 (Too few columns): 3,27559,2015-01-28 02:20:02,20150128
2017-12-08 11:22:33.639 +0000 [WARN] (0001:preview): Skipped line 5 (Too few columns): 4,11270,2015-01-29 11:54:36,20150129

〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
                               省略
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
Error: No input records to preview
  • 抜粋すると読み込もうとしているcsvファイルのカラムが設定ファイルで指定しているよりもカラムが少ないって言われています。
2017-12-08 11:22:33.638 +0000 [WARN] (0001:preview): Skipped line 2 (Too few columns): 1,32864,2015-01-27 19:23:49,20150127
2017-12-08 11:22:33.639 +0000 [WARN] (0001:preview): Skipped line 3 (Too few columns): 2,14824,2015-01-27 19:01:23,20150127
2017-12-08 11:22:33.639 +0000 [WARN] (0001:preview): Skipped line 4 (Too few columns): 3,27559,2015-01-28 02:20:02,20150128
2017-12-08 11:22:33.639 +0000 [WARN] (0001:preview): Skipped line 5 (Too few columns): 4,11270,2015-01-29 11:54:36,20150129

こんな感じにdryrunをしながら異常があれば設定ファイルを修正していきましょう。
ちなみに今回の場合はcsvファイルを正とするなら、config.ymlのinputタスクのcolumnsからカラムを消しましょう。

run

さて下記のように設定ファイルを修正したら実際に実行してみましょう

config.yml
in:
  type: file
  path_prefix: '/home/vagrant/sample/csv/sample_01.csv'
  parser:
    charset: UTF-8
    newline: LF
    type: csv
    delimiter: ','
    quote: '"'
    escape: '"'
    null_string: "NULL"
    trim_if_not_quoted: false
    skip_header_lines: 1
    allow_extra_columns: false
    allow_optional_columns: false
    default_timezone: 'Asia/Tokyo'
    columns:
    - {name: id, type: string}
    - {name: account, type: string}
    - {name: time, type: string}
    - {name: purchase, type: string}
-     - {name: comment, type: string}
+ #    - {name: comment, type: string}
out: {type: stdout}

実行

$ embulk run config.yml
2017-12-08 11:32:04.137 +0000: Embulk v0.8.36
2017-12-08 11:32:13.396 +0000 [INFO] (0001:transaction): Listing local files at directory '/home/vagrant/sample/csv' filtering filename by prefix 'sample_01.csv'
2017-12-08 11:32:13.399 +0000 [INFO] (0001:transaction): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-12-08 11:32:13.404 +0000 [INFO] (0001:transaction): Loading files [/home/vagrant/sample/csv/sample_01.csv]
2017-12-08 11:32:13.486 +0000 [INFO] (0001:transaction): Using local thread executor with max_threads=4 / output tasks 2 = input tasks 1 * 2
2017-12-08 11:32:13.495 +0000 [INFO] (0001:transaction): {done:  0 / 1, running: 0}
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-filter
4,11270,2015-01-29 11:54:36,20150129,plugin
2017-12-08 11:32:13.750 +0000 [INFO] (0001:transaction): {done:  1 / 1, running: 0}
2017-12-08 11:32:13.755 +0000 [INFO] (main): Committed.
2017-12-08 11:32:13.755 +0000 [INFO] (main): Next config diff: {"in":{"last_path":"/home/vagrant/sample/csv/sample_01.csv"},"out":{}}
  • わかりにくいが、抜粋すると下記のようにcsvファイルの中身が標準出力されている
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-filter
4,11270,2015-01-29 11:54:36,20150129,plugin

ヘッダーを除いて、csvファイルの中身が出力されていることがわかる

共通化

Embulkは例えば、下記のように読み込む対象のファイルが2つあり、設定ファイルが2つあったとして、共通の部分を別ファイルに書き出し、共通化することができます。

csv/sample_01.csv
id,account,time,purchase
1,32864,2015-01-27 19:23:49,20150127
2,14824,2015-01-27 19:01:23,20150127
3,27559,2015-01-28 02:20:02,20150128
4,11270,2015-01-29 11:54:36,20150129
csv/sample_02.csv
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-filter
4,11270,2015-01-29 11:54:36,20150129,plugin
config01.yml
in:
  type: file
  path_prefix: '/home/vagrant/sample/csv/sample_01.csv'
  parser:
    charset: UTF-8
    newline: LF
    type: csv
    delimiter: ','
    quote: '"'
    escape: '"'
    null_string: "NULL"
    trim_if_not_quoted: false
    skip_header_lines: 1
    allow_extra_columns: false
    allow_optional_columns: false
    default_timezone: 'Asia/Tokyo'
    columns:
    - {name: id, type: string}
    - {name: account, type: string}
    - {name: time, type: string}
    - {name: purchase, type: string}
out: {type: stdout}
config02.yml
in:
  type: file
  path_prefix: '/home/vagrant/sample/csv/sample_02.csv'
  parser:
    charset: UTF-8
    newline: LF
    type: csv
    delimiter: ','
    quote: '"'
    escape: '"'
    null_string: "NULL"
    trim_if_not_quoted: false
    skip_header_lines: 1
    allow_extra_columns: false
    allow_optional_columns: false
    default_timezone: 'Asia/Tokyo'
    columns:
    - {name: id, type: string}
    - {name: account, type: string}
    - {name: time, type: string}
    - {name: purchase, type: string}
    - {name: comment, type: string}
out: {type: stdout}

共通化するにあたっての注意点

  • liquidテンプレートを使う(hogehoge.yml.liquid)
  • 共通化した設定ファイルは、_テンプレート名.yml.liquidという名前にします。
  • 共通化した設定ファイルは、元となる設定ファイルと同じディレクトリが起点ディレクトリになります。
  • {% include '共通ファイルまでのパス' %}で共通ファイルを指定できます。
    • ※指定する際は_yml.liquidを省略する

手順

  1. まずは共通化できる部分を見つけましょう

    • 今回は下記
    parser:
      charset: UTF-8
      newline: LF
      type: csv
      delimiter: ','
      quote: '"'
      escape: '"'
      null_string: "NULL"
      trim_if_not_quoted: false
      skip_header_lines: 1
      allow_extra_columns: false
      allow_optional_columns: false
      default_timezone: 'Asia/Tokyo'
    
  2. 共通化ファイル作成

    common/_common_parser.yml.liquid
      parser:
        charset: UTF-8
        newline: LF
        type: csv
        delimiter: ','
        quote: '"'
        escape: '"'
        null_string: "NULL"
        trim_if_not_quoted: false
        skip_header_lines: 1
        allow_extra_columns: false
        allow_optional_columns: false
        default_timezone: 'Asia/Tokyo'
    
  3. 設定ファイル修正

    • config01.yml.liquid
    config01.yml.liquid
    in:
    type: file
    path_prefix: '/home/vagrant/sample/csv/sample_01.csv'
    {% include common/common_parser %}
      columns:
      - {name: id, type: string}
      - {name: account, type: string}
      - {name: time, type: string}
      - {name: purchase, type: string}
    

out: {type: stdout}
```
- config02.yml.liquid

```yaml:config02.yml.liquid
in:
type: file
path_prefix: '/home/vagrant/sample/csv/sample_02.csv'
{% include common/common_parser %}
  columns:
  - {name: id, type: string}
  - {name: account, type: string}
  - {name: time, type: string}
  - {name: purchase, type: string}
  - {name: comment, type: string}

out: {type: stdout}
```

実行

config01.yml.liquid

$ embulk preview config01.yml.liquid
2017-12-08 12:18:28.591 +0000: Embulk v0.8.36
2017-12-08 12:18:37.893 +0000 [INFO] (0001:preview): Listing local files at directory '/home/vagrant/sample/csv' filtering filename by prefix 'sample_01.csv'
2017-12-08 12:18:37.895 +0000 [INFO] (0001:preview): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-12-08 12:18:37.899 +0000 [INFO] (0001:preview): Loading files [/home/vagrant/sample/csv/sample_01.csv]
2017-12-08 12:18:37.915 +0000 [INFO] (0001:preview): Try to read 32,768 bytes from input source
+-----------+----------------+---------------------+-----------------+
| id:string | account:string |         time:string | purchase:string |
+-----------+----------------+---------------------+-----------------+
|         1 |          32864 | 2015-01-27 19:23:49 |        20150127 |
|         2 |          14824 | 2015-01-27 19:01:23 |        20150127 |
|         3 |          27559 | 2015-01-28 02:20:02 |        20150128 |
|         4 |          11270 | 2015-01-29 11:54:36 |        20150129 |
+-----------+----------------+---------------------+-----------------+

config02.yml.liquid

$ embulk preview config02.yml.liquid
2017-12-08 12:17:25.368 +0000: Embulk v0.8.36
2017-12-08 12:17:33.316 +0000 [INFO] (0001:preview): Listing local files at directory '/home/vagrant/sample/csv' filtering filename by prefix 'sample_02.csv'
2017-12-08 12:17:33.318 +0000 [INFO] (0001:preview): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-12-08 12:17:33.323 +0000 [INFO] (0001:preview): Loading files [/home/vagrant/sample/csv/sample_02.csv]
2017-12-08 12:17:33.338 +0000 [INFO] (0001:preview): Try to read 32,768 bytes from input source
+-----------+----------------+---------------------+-----------------+----------------+
| id:string | account:string |         time:string | purchase:string | comment:string |
+-----------+----------------+---------------------+-----------------+----------------+
|         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-filter |
|         4 |          11270 | 2015-01-29 11:54:36 |        20150129 |         plugin |
+-----------+----------------+---------------------+-----------------+----------------+

どちらも正常に実行でき、設定ファイルもスッキリしました!

変数渡し

さて、共通化するだけでもかなりスッキリ書くことができましたが、変数を渡すことによってさらにスッキリさせることができます。

変数の渡し方は2つの方法があります。

assignを使って変数を定義する方法

  • {% assign 変数名 = '値' %}で変数をセット
  • {% include 共通化ファイルまでのパス with 変数名 %}で変数をinclude時に変数を渡すことができる
{% assign file_prefix = '/home/vagrant/sample/csv/sample_01.csv' %}
{% include 'common/common_parser' with file_prefix %}

includeと同時に変数を渡す

  • {% include 'common/common_parser',
      変数名: '値' %}でinclude時に変数を直接渡す。
{% include '共通ファイルまでのパス' ,
   file_prefix: '/home/vagrant/sample/csv/sample_01.csv' %}

こちらの書き方の方が個人的にスッキリしているので今回はこちらを使う

変数渡しするにあたっての注意点

基本は共通化と同じですが、プラスで補足すると

  • 変数を展開する際は{{ 変数名 }}で展開することができる。

手順

  1. 共通ファイルを作成

    common/_common_parser.yml.liquid
    in:
      type: file
      path_prefix: {{ path_prefix }}
      parser:
        charset: UTF-8
        newline: LF
        type: csv
        delimiter: ','
        quote: '"'
        escape: '"'
        null_string: "NULL"
        trim_if_not_quoted: false
        skip_header_lines: 1
        allow_extra_columns: false
        allow_optional_columns: false
        default_timezone: 'Asia/Tokyo'
    
  2. 設定ファイル修正

    • config01.yml.liquid
    config01.yml.liquid
    {% include 'common/common_input_task',
       path_prefix: '/home/vagrant/sample/csv/sample_01.csv' %}
        columns:
        - {name: id, type: string}
        - {name: account, type: string}
        - {name: time, type: string}
        - {name: purchase, type: string}
    out: {type: stdout}
    
    • config02.yml.liquid
    config02.yml.liquid
    {% include 'common/common_input_task',
       path_prefix: '/home/vagrant/sample/csv/sample_02.csv' %}
        columns:
        - {name: id, type: string}
        - {name: account, type: string}
        - {name: time, type: string}
        - {name: purchase, type: string}
        - {name: comment, type: string}
    out: {type: stdout}
    

実行

さて、ファイルもスッキリしたところで実行してみましょう

config01.yml.liquid

$ embulk preview config01.yml.liquid
2017-12-08 12:42:11.584 +0000: Embulk v0.8.36
2017-12-08 12:42:18.543 +0000 [INFO] (0001:preview): Listing local files at directory '/home/vagrant/sample/csv' filtering filename by prefix 'sample_01.csv'
2017-12-08 12:42:18.547 +0000 [INFO] (0001:preview): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-12-08 12:42:18.552 +0000 [INFO] (0001:preview): Loading files [/home/vagrant/sample/csv/sample_01.csv]
2017-12-08 12:42:18.569 +0000 [INFO] (0001:preview): Try to read 32,768 bytes from input source
+-----------+----------------+---------------------+-----------------+
| id:string | account:string |         time:string | purchase:string |
+-----------+----------------+---------------------+-----------------+
|         1 |          32864 | 2015-01-27 19:23:49 |        20150127 |
|         2 |          14824 | 2015-01-27 19:01:23 |        20150127 |
|         3 |          27559 | 2015-01-28 02:20:02 |        20150128 |
|         4 |          11270 | 2015-01-29 11:54:36 |        20150129 |
+-----------+----------------+---------------------+-----------------+

config02.yml.liquid

$ embulk preview config02.yml.liquid
2017-12-08 12:45:34.495 +0000: Embulk v0.8.36
2017-12-08 12:45:42.126 +0000 [INFO] (0001:preview): Listing local files at directory '/home/vagrant/sample/csv' filtering filename by prefix 'sample_02.csv'
2017-12-08 12:45:42.129 +0000 [INFO] (0001:preview): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-12-08 12:45:42.133 +0000 [INFO] (0001:preview): Loading files [/home/vagrant/sample/csv/sample_02.csv]
2017-12-08 12:45:42.145 +0000 [INFO] (0001:preview): Try to read 32,768 bytes from input source
+-----------+----------------+---------------------+-----------------+----------------+
| id:string | account:string |         time:string | purchase:string | comment:string |
+-----------+----------------+---------------------+-----------------+----------------+
|         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-filter |
|         4 |          11270 | 2015-01-29 11:54:36 |        20150129 |         plugin |
+-----------+----------------+---------------------+-----------------+----------------+

条件分岐

さてここまできたらcolumns:も省略したいところ、ですが、、ここに来て手詰まり....
っとふと、Ansibleをいじってた時のことを思い出して...もしかしたらjinja2のテンプレートエンジンが使えるかも..っと思い試してみたところ見事できたのでご紹介します。

ちなみに...jinja2とは

  1. common/_common_input_task.yml.liquid

    common/_common_input_task.yml.liquid
    in:
      type: file
      path_prefix: {{ path_prefix }}
      parser:
        charset: UTF-8
        newline: LF
        type: csv
        delimiter: ','
        quote: '"'
        escape: '"'
        null_string: "NULL"
        trim_if_not_quoted: false
        skip_header_lines: 1
        allow_extra_columns: false
        allow_optional_columns: false
        default_timezone: 'Asia/Tokyo'
        columns:
        - {name: id, type: string}
        - {name: account, type: string}
        - {name: time, type: string}
        - {name: purchase, type: string}
    {% if path_prefix == '/home/vagrant/sample/csv/sample_02.csv' %}
        - {name: comment, type: string}
    {% endif %}
    
  2. config01.yml.liquid

    config01.yml.liquid
    {% include 'common/common_input_task',
    path_prefix: '/home/vagrant/sample/csv/sample_01.csv' %}
    

out: {type: stdout}
```

  1. config02.yml.liquid

    config02.yml.liquid
    {% include 'common/common_input_task',
    path_prefix: '/home/vagrant/sample/csv/sample_02.csv' %}
    

out: {type: stdout}
```

実行

めちゃくちゃスッキリしましたね(笑)もはやもともとの設定全て共通ファイルに書き出せました(笑)

ってことで実行してみましょう

config01.yml.liquid

$ embulk preview config01.yml.liquid
2017-12-08 12:58:18.236 +0000: Embulk v0.8.36
2017-12-08 12:58:27.095 +0000 [INFO] (0001:preview): Listing local files at directory '/home/vagrant/sample/csv' filtering filename by prefix 'sample_01.csv'
2017-12-08 12:58:27.099 +0000 [INFO] (0001:preview): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-12-08 12:58:27.105 +0000 [INFO] (0001:preview): Loading files [/home/vagrant/sample/csv/sample_01.csv]
2017-12-08 12:58:27.119 +0000 [INFO] (0001:preview): Try to read 32,768 bytes from input source
+-----------+----------------+---------------------+-----------------+
| id:string | account:string |         time:string | purchase:string |
+-----------+----------------+---------------------+-----------------+
|         1 |          32864 | 2015-01-27 19:23:49 |        20150127 |
|         2 |          14824 | 2015-01-27 19:01:23 |        20150127 |
|         3 |          27559 | 2015-01-28 02:20:02 |        20150128 |
|         4 |          11270 | 2015-01-29 11:54:36 |        20150129 |
+-----------+----------------+---------------------+-----------------+

config02.yml.liquid

$ embulk preview config02.yml.liquid
2017-12-08 12:58:35.320 +0000: Embulk v0.8.36
2017-12-08 12:58:44.118 +0000 [INFO] (0001:preview): Listing local files at directory '/home/vagrant/sample/csv' filtering filename by prefix 'sample_02.csv'
2017-12-08 12:58:44.121 +0000 [INFO] (0001:preview): "follow_symlinks" is set false. Note that symbolic links to directories are skipped.
2017-12-08 12:58:44.125 +0000 [INFO] (0001:preview): Loading files [/home/vagrant/sample/csv/sample_02.csv]
2017-12-08 12:58:44.138 +0000 [INFO] (0001:preview): Try to read 32,768 bytes from input source
+-----------+----------------+---------------------+-----------------+----------------+
| id:string | account:string |         time:string | purchase:string | comment:string |
+-----------+----------------+---------------------+-----------------+----------------+
|         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-filter |
|         4 |          11270 | 2015-01-29 11:54:36 |        20150129 |         plugin |
+-----------+----------------+---------------------+-----------------+----------------+

無事正常終了 :tada:

終わりに

今回はinputしたファイルを標準出力するっという単純なものだったので設定ファイルがスッカラカンにできましたが、実際は使うプラグインが違かったりして、ここまでは共通化はできませんが、これを参考に実業務に活かしていただけると幸いです

68
40
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
68
40