theangryangel / logstash-output-jdbc

JDBC output for Logstash
MIT License
255 stars 101 forks source link

time zone information of date field lost, how to transfer time zone #142

Closed zgjhust closed 5 years ago

zgjhust commented 5 years ago

the log item looks like

{
    "created_at" => "2018-12-17T06:57:58.000Z"
    ......
}

but when inserted into mysql, it looks like

  # select created_at from t limit 1;
    2018-12-17 06:57:58

the time zone of mysql server is +08:00, it is supposed to be 2018-12-17 14:57:58

Environment

theangryangel commented 5 years ago

Check what datatype and how you're casting your datetime string. MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME). So if you're using cast(? as timestamp) in your logstash-output-jdbc string you'll need to modify it to the equivalent that MySQL supports (presumably cast(? as datetime)).

zgjhust commented 5 years ago

resolved by this:

statement => ["insert into t1 (record_time) values ( convert_tz(?, '+00:00', '+08:00'))","record_time"]