LoginSignup
1
1

More than 5 years have passed since last update.

Embulk (csv -> gzip圧縮してファイル出力へ)

Posted at

概要

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

サンプルを用意する

ターミナル
$ embulk example ./try2
$ gzip -d ./try2/csv/sample_01.csv.gz

「embulk-output-command」を追加する

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

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

## プラグイン追加
$ 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: ./csv/sample_
  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: command
  command: "cat - > task.$INDEX.$SEQID.csv.gz"
  encoders:
    - {type: gzip}
  formatter:
    type: csv

プレビュー

ターミナル
$ 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

作成されたzipファイルを確認する

ターミナル
$ ls -la | grep task
-rw-r--r--. 1 500 500 189  2月 26 15:41 task.0.0.csv.gz

以上

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