【问题标题】:Why does Logstash put the wrong time zone in ~/.logstash_jdbc_last_run?为什么 Logstash 在 ~/.logstash_jdbc_last_run 中放错了时区?
【发布时间】:2017-03-08 09:36:41
【问题描述】:

Logstash 5.2.1

下面的配置没问题,正在部分更新。我只是误解了结果以及 Logstash 如何使用时区。

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


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

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

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

我的电脑/服务器时间:

$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

数据库数据是这样导入的:

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

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

【问题讨论】:

    标签: logstash logstash-configuration logstash-jdbc


    【解决方案1】:

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

    【讨论】:

    • 好的,那为什么我连续导入了第5条DB记录呢?查看更新问题。
    • 对不起,我之前的评论有误导性。我在 Logstash 中配置了 jdbc_default_timezone => "Europe/Rome"。所以,我必须有罗马时区,不是吗?
    • Timezone conversion. SQL does not allow for timezone data in timestamp fields. This plugin will automatically convert your SQL timestamp fields to Logstash timestamps, in relative UTC time in ISO8601 format. 所以导入后的时区总是UTC,此设置仅用于转换
    • 好的,那我需要配置什么才能获得正确的部分更新?
    【解决方案2】:

    默认为 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 字段,然后将其转换为欧洲/罗马时区。新转换的时间戳放在 timestamp_europe 字段中。

    希望现在更清楚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多