LoginSignup
1
2

More than 5 years have passed since last update.

Embulk (gzip -> 解凍:csv -> TreasureData)

Last updated at Posted at 2018-02-26

概要

Embulkを使う機会があったので、使い方について忘れないようメモしておく。
「sample_01.csv.gz」ファイルからcsvを取得し、TreasureDataへ送信するサンプルをやってみる。

サンプルを用意する

ターミナル
$ embulk example ./try3

プラグインを追加する

「embulk-output-td」を追加する。

ターミナル
## プラグインの独立したバンドルを作成する
$ embulk mkbundle bundle

## gemファイルにプラグインを追記する
$ vi bundle/Gemfile
gem 'embulk-output-td' # 追記

## プラグイン追加
$ cd bundle
$ embulk bundle
$ cd ../

config.yml作成 & 修正

ターミナル
$ embulk guess -b bundle seed.yml -o config.yml
$ vi config.yml
config.yml
in:
  type: file
  path_prefix: /root/opt/www.qlife.jp/./try3/csv/sample_
  decoders:
  - {type: gzip}
  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
    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: td
  apikey: <your apikey>
  endpoint: api.treasuredata.com
  database: hoge_fuga_tmp  # データベース名
  table: my_table          # テーブル名
  mode: append             # 転送モード: append(追加), replace(置き換え)
  # time_column: time      # インデックスに使用する時間カラム
  default_timestamp_format: '%Y-%m-%d %H:%M:%S'
  column_options:
    time: {format: '%Y-%m-%d %H:%M:%S', timezone: 'Asia/Tokyo'}

プレビュー

ターミナル
$ embulk preview -b bundle config.yml
+---------+--------------+-------------------------+-------------------------+----------------------------+
| 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 |                            |
+---------+--------------+-------------------------+-------------------------+----------------------------+

処理実行

ターミナル
$ embulk run -b bundle config.yml

TreasureDataを確認

my_tableテーブルにデータが登録されている。
screen.png

以上

参考サイト

1
2
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
1
2