LoginSignup
19
12

More than 5 years have passed since last update.

環境変数を使ってEmbulkで変数を

Posted at

Embulkの導入など最低限のことを以前にまとめたのはこちら
Embulkについてまとめてみた 2017/08

Embulkのバージョンは0.8系(0.9系がリリースされましたので、そちらはまた別途)

Embulkでは、環境変数を使って、環境毎に値を変えて差し込み、実行することが可能。
テンプレートエンジン liquid を使って可能(Embulk0.7以降で利用可能)

ディレクトリ構成はこんな感じ

ディレクトリ構成
embulk
├ config.yml.liquid
├  exec_embulk.sh
├ setparams
└ env 
 ├ setenv_localhost
 ├ setenv_staging
 └ setenv_product
config.yml.liquid
in:
  type: http
  url: {{ env.DOMAIN }}/api/report/
  params:
    - {name: accesskey, value: {{ env.ACCESS_KEY }}}
    - {name: year, value: {{ env.YEAR }}}
    - {name: month, value: {{ env.MONTH }}}
  read_timeout: 300000
  method: get
  parser:
    charset: UTF-8
    newline: LF
    type: csv
    delimiter: ','
    quote: '"'
    escape: '"'
    trim_if_not_quoted: false
    skip_header_lines: 1
    allow_extra_columns: false
    allow_optional_columns: false
    default_timezone: "Asia/Tokyo"
    columns:
    - {name: date, type: timestamp, format: '%Y/%m/%d'}
    - {name: total, type: long}
    - {name: item1, type: string}
    - {name: item2, type: string}
out:
  type: mysql
  host: {{env.DB_HOST}}
  user: {{env.DB_USER}}
  password: {{env.DB_PASSWORD}}
  database: test_db
  table: test_table
  mode: merge

※上記のdefault timezoneは、Embulk0.9より使えなくなってるらしい(タイムゾーンの呪いの書)

使い方は、 exec_embulk ($profile) ($month) ($year)
profile : localhost, staging, product など

exec_embulk.sh
echo "profile:$1, year:$2 month:$3"

source $EMBULK_HOME/env/setenv_$1
source $EMBULK_HOME/setparams $2 $3
embulk run $EMBULK_HOME/config.yml.liquid
exit 0
setenv.sh
export YEAR=$1
export MONTH=$2
env/setenv_loalhost
export DOMAIN=https://hogehoge.org/api
export EMBULK_HOME=/tmp/embulk/
export ACCESS_KEY=*********************
export DB_HOST=localhost
export DB_USER=embulk
export DB_PASSWORD=*********
19
12
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
19
12