【问题标题】:datetime in Elasticsearch - How to handle timezoneElasticsearch 中的日期时间 - 如何处理时区
【发布时间】:2017-03-10 17:45:03
【问题描述】:

我尝试索引包含日期的字段。

如何索引不同时区的日期?

我已经像这样设置了我的 elasticsearch 字段: 'requested_dt': {"type": "date", "format": "date_time_no_millis"} 'local_dt': {"type": "date", "format": "date_time_no_millis"}

我试图索引这些值(local_dt): (requested_dt 为法国当前时间)

IT 2016-10-27T23:46:17Z
GB 2016-10-27T22:46:19Z

我没有通过 Kibana 得到预期的结果:

[local_dt]
IT October 28th 2016, 01:46:17.000
GB October 28th 2016, 00:46:19.000

[requested_dt]
IT October 27th 2016, 23:46:17.000
GB October 27th 2016, 23:46:19.000

所以,对于 requested_dt,我得到了我的期望。

对于 local_dt,我没有得到我想要的。

我尝试将 Z 值替换为 UTC 偏移量,但无法获得正确的输出。

是否有人能够向我解释如何为我想要的每个时区获得正确的输出?

【问题讨论】:

  • 你试过logstash中的日期过滤器吗?
  • 我没有使用logstash;只是尝试使用 python 发送值

标签: datetime elasticsearch timezone kibana timezone-offset


【解决方案1】:

根据我的阅读(以及我自己的经验),Kibana 将索引 @timestamp,假设它们以 UTC 表示,并且仅采用像 2018-04-23T10:45:13.899Z 这样的格式。请注意,我们只有毫秒,T 作为分隔符,Z 表示 UTC。

https://discuss.elastic.co/t/kibana-timezone/29270/5

所以,如果您有一个本地 datetime 对象,请尝试转换为 UTC 时间,并像上面那样格式化。

  • now()的情况下,使用django.utils.timezonetimezone.now();或者,没有 django,你有 datetime.datetime.utcnow()
  • 如果你已经有一个datetime() 对象,你可以这样做:

这个:

import pytz, datetime
local = pytz.timezone ("Europe/Paris")
local_dt = local.localize(your_datetime_object, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)

(感谢this answer

当你有对象时,格式如下:

timeStr = datetime.strftime(your_utc_time_object, "%Y-%m-%dT%H:%M:%S.%f") # %f: microseconds, 1/10^6 seconds.
timeStr = timeStr[:-3] # delete the trailing 3 digits, convert to milliseconds
toInsert["@timestamp"] = nowStr + "Z" # add 'Z' at last

【讨论】:

    【解决方案2】:

    为了像我这样通过 Google 搜索来到这里的人的利益,您不能在时间戳末尾附加 military time zone 字母并让 Elasticsearch 识别它。

    我认为会是这种情况,因为它会在 UTC 时间戳的末尾识别并输出“Z”,所以我将“R”附加到我自己的时间戳以表示它们来自 UTC-5。以下是它们彼此相邻的样子:

    "2020-04-09T07:35:15.100Z"  # parsed as UTC
    "2020-04-09T07:35:15.100R"  # illegal argument exception
    

    但是,没有一种内置格式可以识别这个额外的字母;您必须像这样指定时间偏移

    "2020-04-09T07:35:15.100-0500"
    "2020-04-09T07:35:15.100-05:00"
    

    或在管道处理器中指定时区

    {
      "pipeline": {
        "processors": [
          {
            "date": {
              "field": "raw_date",
              "formats": ["ISO8601"],
              "timezone": "America/New_York"
            }
          }]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2013-03-05
      • 2013-06-30
      相关资源
      最近更新 更多