1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CSV内の日時文字列(CST)をEmbulkでTreasureDataにUNIX timeとして入れてRubyで結果を確認する

Last updated at Posted at 2016-12-26

まずはEmbulkのプラグインを入れる

$ embulk gem install embulk-filter-timestamp_format

次にEmbulkの設定ファイルを書く
in(type: csv)では当該列はtype:stringとする
そして以下のフィルタを記述する

filters:
  - type: timestamp_format
    default_from_timestamp_format: ['%Y-%m-%d %H:%M:%S']
    default_from_timezone        : 'America/Chicago'
    stop_on_invalid_record       : true
    columns:
      - {name: mycolname, type: long}

これで実行すると、例えばCSVに2016-12-22 05:27:36と書いてある場合、TreasureDataには1482406056が入る
これを以下のようにRubyで確認すると、UTCなら6時間、JSTなら15時間ずれていることが確認できる

$ ruby -e 'puts Time.at(1482406056).utc'
2016-12-22 11:27:36 UTC
$ ruby -e 'puts Time.at(1482406056)'
2016-12-22 20:27:36 +0900
$ ruby -e "require 'time'; puts Time.parse('2016-12-22 20:27:36 +0900').to_i"   # おまけ
1482406056
$ ruby -e "require 'time'; puts Time.parse('2016-12-22 20:27:36 JST').to_i"     # おまけ
1482406056
$ ruby -e "require 'time'; puts Time.parse('2016-12-22 05:27:36 CST').getlocal" # おまけ
2016-12-22 20:27:36 +0900

わざわざフィルタを入れなくてもembulk-output-tdプラグインの以下のオプションで行けそうに思うが、値は1482406056になるものの、型がstring型になってしまい、断念

  default_timestamp_type_convert_to: sec

環境

$ embulk gem list|grep -i embulk
2016-12-26 15:44:33.104 +0900: Embulk v0.8.15
embulk-filter-timestamp_format (0.2.4)
embulk-input-td (0.1.0)
embulk-output-td (0.3.12)

stringになっちゃう件は、先にtd table:createtd schema:setをやっておけばいいのかも
プラグイン入れるのとどっちが楽かなぁ

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?