LoginSignup
2
2

More than 5 years have passed since last update.

Embulkでsyukujitsu.csvを読み込む

Last updated at Posted at 2017-03-01

元ネタ
http://www.softantenna.com/wp/webservice/naikakufu-shukujitsu-csv-format/

本当は embulk-parser-syukujitsu を作りたかったんですが、まともなフォーマットになってしまい不要になってしまったので残念です...旬を逃すとはまさにこの事

ということで、普通にEmbulkでCSVをロードしてきてパースしてみました、Embulkのバージョンは0.8.16。結論から言うと guess がすごく便利でした。

これで国民の祝日を定期的にDBに登録しておけますね!

1. 以下の設定ファイルを準備する

Gemfile
source 'https://rubygems.org/'

gem 'embulk-input-http', '~> 0.0.13'
config.yml
in:
  type: http
  url: http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv
  parser:
    type: csv
out: {type: stdout}

embulk bundle で必要なプラグインを読み込んでおきます

% embulk bundle --path ./vendor/bundle 
2017-03-01 16:19:02.057 +0900: Embulk v0.8.16
Resolving dependencies...
Using embulk-input-http 0.0.13
Using bundler 1.10.6
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into ./vendor/bundle.

2. embulk guessでparserの設定を埋める

% embulk guess config.yml -o config.yml --bundle .
2017-03-01 16:12:59.940 +0900: Embulk v0.8.16
2017-03-01 16:13:00.598 +0900 [INFO] (0001:guess): Loaded plugin embulk-input-http (0.0.13)
2017-03-01 16:13:00.739 +0900 [INFO] (0001:guess): GET "http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv"
2017-03-01 16:13:00.934 +0900 [INFO] (0001:guess): Loaded plugin embulk/guess/gzip from a load path
2017-03-01 16:13:00.943 +0900 [INFO] (0001:guess): Loaded plugin embulk/guess/bzip2 from a load path
2017-03-01 16:13:00.953 +0900 [INFO] (0001:guess): Loaded plugin embulk/guess/json from a load path
2017-03-01 16:13:00.958 +0900 [INFO] (0001:guess): Loaded plugin embulk/guess/csv from a load path
in:
  type: http
  url: http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv
  parser:
    type: csv
    charset: MS932
    newline: CRLF
    delimiter: ','
    quote: '"'
    escape: '"'
    trim_if_not_quoted: false
    skip_header_lines: 1
    allow_extra_columns: false
    allow_optional_columns: false
    columns:
    - {name: 国民の祝日月日, type: timestamp, format: '%Y-%m-%d'}
    - {name: 国民の祝日名称, type: string}
out: {type: stdout}
Created 'config.yml' file.

こんな感じのファイルができあがります

in:
  type: http
  url: http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv
  parser:
    type: csv
    charset: MS932
    newline: CRLF
    delimiter: ','
    quote: '"'
    escape: '"'
    trim_if_not_quoted: false
    skip_header_lines: 1
    allow_extra_columns: false
    allow_optional_columns: false
    columns:
    - {name: 国民の祝日月日, type: timestamp, format: '%Y-%m-%d'}
    - {name: 国民の祝日名称, type: string}
out: {type: stdout}

3. embulk runしてみる

% embulk run config.yml --bundle .
2017-03-01 16:31:31.738 +0900: Embulk v0.8.16
2017-03-01 16:31:32.902 +0900 [INFO] (0001:transaction): Loaded plugin embulk-input-http (0.0.13)
2017-03-01 16:31:32.978 +0900 [INFO] (0001:transaction): Using local thread executor with max_threads=16 / output tasks 8 = input tasks 1 * 8
2017-03-01 16:31:32.983 +0900 [INFO] (0001:transaction): {done:  0 / 1, running: 0}
2017-03-01 16:31:33.110 +0900 [INFO] (0017:task-0000): GET "http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv"
2016-01-01,元日
2016-01-11,成人の日
2016-02-11,建国記念の日
2016-03-20,春分の日
2016-04-29,昭和の日
2016-05-03,憲法記念日
2016-05-04,みどりの日
2016-05-05,こどもの日
2016-07-18,海の日
2016-08-11,山の日
2016-09-19,敬老の日
2016-09-22,秋分の日
2016-10-10,体育の日
2016-11-03,文化の日
2016-11-23,勤労感謝の日
2016-12-23,天皇誕生日
2017-01-01,元日
2017-01-09,成人の日
2017-02-11,建国記念の日
2017-03-20,春分の日
2017-04-29,昭和の日
2017-05-03,憲法記念日
2017-05-04,みどりの日
2017-05-05,こどもの日
2017-07-17,海の日
2017-08-11,山の日
2017-09-18,敬老の日
2017-09-23,秋分の日
2017-10-09,体育の日
2017-11-03,文化の日
2017-11-23,勤労感謝の日
2017-12-23,天皇誕生日
2018-01-01,元日
2018-01-08,成人の日
2018-02-11,建国記念の日
2018-03-21,春分の日
2018-04-29,昭和の日
2018-05-03,憲法記念日
2018-05-04,みどりの日
2018-05-05,こどもの日
2018-07-16,海の日
2018-08-11,山の日
2018-09-17,敬老の日
2018-09-23,秋分の日
2018-10-08,体育の日
2018-11-03,文化の日
2018-11-23,勤労感謝の日
2018-12-23,天皇誕生日
2017-03-01 16:31:33.447 +0900 [INFO] (0001:transaction): {done:  1 / 1, running: 0}
2017-03-01 16:31:33.456 +0900 [INFO] (main): Committed.
2017-03-01 16:31:33.456 +0900 [INFO] (main): Next config diff: {"in":{},"out":{}}
2
2
1

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