2017-03-08 219 views
1

Logstash 5.2.1为什么Logstash将错误的时区放在〜/ .logstash_jdbc_last_run中?下面

的结构是OK,则局部更新沃金。我只是误解了结果以及Logstash如何使用时区。

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


我想指数从一个PostgreSQL的一些数据与Logstash帮助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 | [email protected]   | Jim  | Smith 
    2 | 2017-03-07 18:09:25.3756 |       | John  | Smith 
    3 | 2017-03-07 18:09:25.384053 | [email protected]  | Carol  | Smith 
    4 | 2017-03-07 18:09:25.869833 | [email protected]   | Sam  | 
    5 | 2017-03-08 10:04:26.39423 | [email protected]  | T   | Rex 

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

INSERT INTO contacts(timestamp, email, first_name, last_name) VALUES(current_timestamp, '[email protected]', 'Sam', null); 

为什么Logstash把错误的时区~/.logstash_jdbc_last_run?以及如何解决它?

回答

1

2017-03-08 09:29:00.259000000 Z的意思是UTC时区,这是正确的。

+0

好的,那么为什么我会连续导入第5个DB记录?看看更新问题。 – trex

+0

对不起,我以前的评论是误导。我在Logstash中配置了'jdbc_default_timezone =>“Europe/Rome”''。所以,我必须有罗马时区,不是吗? – trex

+0

'时区转换。 SQL不允许timestamp字段中的时区数据。此插件会自动将您的SQL时间戳字段转换为Logstash时间戳,相对UTC时间采用ISO8601格式。' 所以导入后的时区总是'UTC',这个设置只用于转换 –

1

它默认为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字段中。

希望它现在更清晰。

相关问题