【问题标题】:Need to extract the timestamp from a logstash elasticsearch cluster需要从logstash elasticsearch集群中提取时间戳
【发布时间】:2015-06-02 21:12:40
【问题描述】:

我正在尝试确定我的 logstash 集群中最新记录的新鲜度,但我在消化 Elasticsearch DSL 时遇到了一些麻烦。

现在我正在做这样的事情来提取时间戳: curl -sX GET 'http://localhost:9200/logstash-2015.06.02/' -d'{"query": {"match_all": {} } }' | json_pp | grep 时间戳

这让我着迷; "@timestamp" : "2015-06-02T00:00:28.371+00:00",

我想直接使用 elasticsearch 查询,没有 grep hackiness。

原始 JSON(截断长度)如下所示:

{
   "took" : 115,
   "timed_out" : false,
   "hits" : {
      "hits" : [
         {
            "_index" : "logstash-2015.06.02",
            "_source" : {
               "type" : "syslog",
               "@timestamp" : "2015-06-02T00:00:28.371+00:00",
               "tags" : [
                  "sys",
                  "inf"
               ],
               "message" : "    2015/06/02 00:00:28 [INFO] serf: EventMemberJoin: generichost.example.com 10.1.1.10",
               "file" : "/var/log/consul.log",
               "@version" : 1,
               "host" : "generichost.example.com"
            },
            "_id" : "AU4xcf51cXOri9NL1hro",
            "_score" : 1,
            "_type" : "syslog"
         },
      ],
      "total" : 8605141,
      "max_score" : 1
   },
   "_shards" : {
      "total" : 50,
      "successful" : 50,
      "failed" : 0
   }
}

任何帮助将不胜感激。我知道查询很简单,我只是不知道它是什么。

【问题讨论】:

    标签: json parsing elasticsearch logstash dsl


    【解决方案1】:

    您不需要为此使用 DSL。你可以简单地把所有东西都塞进URL query string,像这样:

    curl -s XGET 'localhost:9200/logstash-2015.06.02/_search?_source=@timestamp&size=1&sort=@timestamp:desc&format=yaml'
    

    所以:

    • _source=@timestamp 表示我们只对获取 @timestamp 值感兴趣
    • size=1 表示我们只需要一个结果
    • sort=@timestamp:desc 表示我们要按 @timestamp 降序排序(即最新的优先)
    • format=yaml 将为您提供 YAML 格式的结果,在您的情况下,它比 JSON 更简洁

    输出如下所示:

    - _index: "logstash-2015.06.02"
      _type: "syslog"
      _id: "AU4xcf51cXOri9NL1hro"
      _score: 1.0
      _source:
        @timestamp: "2015-06-02T00:00:28.371+00:00"
    

    您不再需要json_pp,您仍然可以简单地 grep @timestamp 来获取您需要的数据。

    请注意,在 1.6.0 中,将有一种方法可以过滤掉所有元数据(即_index_type_id_score)并且只获取_source 的搜索结果在 URL 中使用 filter_path parameter

    【讨论】:

    猜你喜欢
    • 2017-05-18
    • 1970-01-01
    • 2021-09-23
    • 2015-11-20
    • 1970-01-01
    • 2015-10-27
    • 2015-11-27
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多