LoginSignup
7
6

More than 5 years have passed since last update.

Java版のembulk-parser-jsonlを使ってみた。

Last updated at Posted at 2016-02-23

はじめに

embulk-parser-jsonlの0.1.0がリリースされたので、使ってみました。
(私はプラグインの作者ではないので、詳細はembulk-parser-jsonlをみてください)

0.1.0では、全面的にJavaに書き直されてjson型がサポートされるなどより便利になったようです。

使ったプラグインのバージョンは0.1.0です。

jsonlプラグイン

Embulkのjsonlパーサープラグインは、1行1JSON形式のデータを読み取って要素内のデータをカラムとして抽出するプラグインです。
文字列全体をJSON形式にする場合は、組み込みプラグインのjsonパーサープラグインを利用します。

テストに使ったデータ

{ "column1":"bbb","column2":"2015/01/01","column3":12345,"column5":true,"column6":1234.567, "column7":[1,2,3] , "column8":{"subcol":"val1"}}
{ "column1":"bbb","column2":"2015/01/01","column3":12345,"column5":true,"column6":1234.567, "column7":[1,2,3] , "column8":{"subcol":"val1"}}
{ "column1":"bbb","column2":"2015/01/01","column3":12345,"column5":true,"column6":1234.567, "column7":[1,2,3] , "column8":{"subcol":"val1"}}
{ "column1":"bbb","column2":"2015/01/01","column3":12345,"column5":true,"column6":1234.567, "column7":[1,2,3] , "column8":{"subcol":"val1"}}
{ "column1":"bbb","column2":"2015/01/01","column3":12345,"column5":true,"column6":1234.567, "column7":[1,2,3] , "column8":{"subcol":"val1"}}
{ "column1":"bbb","column2":"2015/01/01","column3":12345,"column5":true,"column6":1234.567, "column7":[1,2,3] , "column8":{"subcol":"val1"}}
{ "column1":"bbb","column2":"2015/01/01","column3":12345,"column5":true,"column6":1234.567, "column7":[1,2,3] , "column8":{"subcol":"val1"}}
{ "column1":"bbb","column2":"2015/01/01","column3":12345,"column5":true,"column6":1234.567, "column7":[1,2,3] , "column8":{"subcol":"val1"}}
{ "column1":"bbb","column2":"2015/01/01","column3":12345,"column5":true,"column6":1234.567, "column7":[1,2,3] , "column8":{"subcol":"val1"}}

guess

jsonlはguess機能が使えます。guess時に`-g jsonlを指定します。

embulk guess -g jsonl test.yml

上記のデータをguessすると次のような設定が出力されます。

in:
  path_prefix: test.json
  type: file
  parser:
    type: jsonl
    charset: UTF-8
    newline: CRLF
    columns:
    - {name: column1, type: string}
    - {name: column2, type: timestamp, format: '%Y/%m/%d'}
    - {name: column3, type: long}
    - {name: column5, type: boolean}
    - {name: column6, type: double}
    - {name: column7, type: json}
    - {name: column8, type: json}
out: {type: stdout}

カラムの定義は、columnsで設定します。(以前のバージョンではschemasとなっていましたがこれは非推奨の設定となったようです。)

  • stringだけでなく、float, long, booleanなどもちゃんと認識されます。
  • 時刻形式の場合は、timestamp型として認識されます。
  • ネストされた、配列やハッシュはjson型として認識されます。

時刻の取り扱い

時刻のフォーマットは組み込みのCSVパーサーと同様にtype: timestampと記載したカラムでformatを指定します。
(追記: 以前のバージョンでは、time_formatとなっていました。が現在は利用できないようです。)

また、上記のままだと、2016/02/01という値は、UTCの日付として取り扱われます。
日本時間だと思っていると9時間ずれてしまいます。
時刻を日本時間として取り扱いたい場合は次のように、default_timezoneを指定します。(これもCSVパーサープラグインと一緒です)

in:
  path_prefix: test.json
  type: file
  parser:
    default_timezone: "Asia/Tokyo" # <-- これ
    type: jsonl
    charset: UTF-8
    newline: CRLF
    columns:
    - {name: column1, type: string}
    - {name: column2, type: timestamp, format: '%Y/%m/%d'}
    - {name: column3, type: long}
    - {name: column5, type: boolean}
    - {name: column6, type: double}
    - {name: column7, type: json}
    - {name: column8, type: json}
out: {type: stdout}

実行結果

bbb,2015/01/01,12345,true,1234.567,[1,2,3],{"subcol":"val1"}
bbb,2015/01/01,12345,true,1234.567,[1,2,3],{"subcol":"val1"}
bbb,2015/01/01,12345,true,1234.567,[1,2,3],{"subcol":"val1"}
bbb,2015/01/01,12345,true,1234.567,[1,2,3],{"subcol":"val1"}
bbb,2015/01/01,12345,true,1234.567,[1,2,3],{"subcol":"val1"}
bbb,2015/01/01,12345,true,1234.567,[1,2,3],{"subcol":"val1"}
bbb,2015/01/01,12345,true,1234.567,[1,2,3],{"subcol":"val1"}
bbb,2015/01/01,12345,true,1234.567,[1,2,3],{"subcol":"val1"}
bbb,2015/01/01,12345,true,1234.567,[1,2,3],{"subcol":"val1"}

参考

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