为什么 Logstash 将错误的时区放在 ~/.logstash_jdbc_last_run 中?

Why does Logstash put the wrong time zone in ~/.logstash_jdbc_last_run?

Logstash 5.2.1

下面配置没问题,局部更新正常。我只是误解了结果以及 Logstash 使用时区的方式。

jdbc_default_timezone 时区转换。 SQL 不允许在时间戳字段中使用时区数据。此插件会自动将您的 SQL 时间戳字段转换为 Logstash 时间戳,采用 ISO8601 格式的相对 UTC 时间。 使用此设置将手动分配指定的时区偏移量,而不是使用本地计算机的时区设置。您必须使用规范时区,例如 Europe/Rome。


我想在 Logstash 的帮助下将一些数据从 PostgreSQL 索引到 Elasticseach。部分更新应该有效。

但就我而言,Logstash 在 ~/.logstash_jdbc_last_run 中设置了错误的时区。

$cat ~/.logstash_jdbc_last_run 
--- 2017-03-08 09:29:00.259000000 Z

我的PC/Server时间:

$date
mer  8 mar 2017, 10.29.31, CET
$cat /etc/timezone 
Europe/Rome

我的 Logstash 配置。:

input {
  jdbc {
    # Postgres jdbc connection string to our database, mydb
    jdbc_connection_string => "jdbc:postgresql://localhost:5432/postgres"
    # The user we wish to execute our statement as
    jdbc_user => "logstash"
    jdbc_password => "logstashpass"
    # The path to our downloaded jdbc driver
    jdbc_driver_library => "/home/trex/Development/ship_to_elasticsearch/software/postgresql-42.0.0.jar"
    # The name of the driver class for Postgresql
    jdbc_driver_class => "org.postgresql.Driver"
    jdbc_default_timezone => "Europe/Rome"
    # our query
    statement => "SELECT * FROM contacts WHERE timestamp > :sql_last_value"
    # every 1 min
    schedule => "*/1 * * * *"
  }
}
output {
  stdout { codec => json_lines }
  elasticsearch {
    hosts => [ "localhost:9200" ]
    index => "database.%{+yyyy.MM.dd.HH}"
  }
}

没有jdbc_default_timezone时区也是错误的。

我的PostgeSQL数据:

postgres=# select * from "contacts";                                                                                               uid |         timestamp          |          email          | first_name | last_name
-----+----------------------------+-------------------------+------------+------------
   1 | 2017-03-07 18:09:25.358684 | jim@example.com         | Jim        | Smith
   2 | 2017-03-07 18:09:25.3756   |                         | John       | Smith
   3 | 2017-03-07 18:09:25.384053 | carol@example.com       | Carol      | Smith
   4 | 2017-03-07 18:09:25.869833 | sam@example.com         | Sam        |
   5 | 2017-03-08 10:04:26.39423  | trex@example.com        | T          | Rex

DB数据是这样导入的:

INSERT INTO contacts(timestamp, email, first_name, last_name) VALUES(current_timestamp, 'sam@example.com', 'Sam', null);

为什么 Logstash 在 ~/.logstash_jdbc_last_run 中输入了错误的时区?以及如何修复它?

2017-03-08 09:29:00.259000000 Z表示UTC时区,正确。

默认为UTC时间。如果您想将其存储在不同的时区,您可以通过添加过滤器来转换时间戳,如下所示:

filter {
    mutate {
    add_field => {
        # Create a new field with string value of the UTC event date
        "timestamp_extract" => "%{@timestamp}"
    }
    }

    date {
    # Parse UTC string value and convert it to my timezone into a new field
    match => [ "timestamp_extract", "yyyy-MM-dd HH:mm:ss Z" ]
    timezone => "Europe/Rome"
    locale => "en"
    remove_field => [ "timestamp_extract" ]
    target => "timestamp_europe"
    }
}

这将转换时区,首先将时间戳提取到 timestamp_extract 字段中,然后将其转换为 Europe/Rome 时区。并将新转换的时间戳放在 timestamp_europe 字段中。

希望现在更清楚了。