使用Elasticsearch获取每日的唯一用户数的插件为「elasticsearch-timefacets-plugin」

首先

我正在使用Elasticsearch将应用程序的事件日志存入,并在Kibana3中进行操作。
当查看事件日志时,我想要像Google Analytics、Mixpanel和SLASH-7一样查看唯一用户数。
然而,在Elasticsearch中,无法按时间顺序获取去重后的值计数。
日期直方图外观(Date Histogram Facet)是相似的,但只能获取计数或指定字段的总和。

弹性搜索时间分面插件

使用elasticsearch-timefacets-plugin这个Elasticsearch的插件可以实现上述功能。
虽然还有其他几个相似的插件,但只有这个插件努力追随Elasticsearch的版本更新。
截至2013年11月9日,ElasticSearch的版本是0.90.6,但elasticsearch-timefacets-plugin支持的版本只到0.90.5。0.90.6是在11月4日发布的,所以无可奈何。

顺便说一下,当我尝试升级到0.90.6版本时,Elasticsearch无法启动了。非常不稳定。

安装步骤

基础

    • Elasticsearch 0.90.5をインストール済み

 

    Mavenをインストール済み

下载源文件

$ git clone git@github.com:crate/elasticsearch-timefacets-plugin.git

由于0.10.0标签与Elasticsearch 0.90.5兼容,因此要检出该标签。

$ cd elasticsearch-timefacets-plugin
$ git checkout -b 0.10.0 0.10.0

编译

$ mvn clean package -DskipTests=true

安装插件

如果在Ubuntu上安装了deb软件包的Elasticsearch,那么Elasticsearch将安装在/usr/share/elasticsearch/目录中。请根据需要进行适当修改。

$ /usr/share/elasticsearch/bin/plugin -install elasticsearch-timefacets-plugin -url file:///<カレントディレクトリのフルパス>/target/elasticsearch-timefacets-plugin-0.10.0.jar

重新启动Elasticsearch

$ sudo service elasticsearch restart

尝试一下

假设已经有一个logstash格式的数据,如下所示:
这是一个描述用户999登录事件日志的图像,时间是2013年10月30日13时50分。

{
  "_index": "logstash-2013.10.30",
  "_type": "logstash",
  "_id": "999999WmQkeDmokM9AIfud",
  "_score": null,
  "_source": {
    "@log_name": "co-meeting.event",
    "@timestamp": "2013-10-30T13:50:53+09:00",
    "user_id": "user999",
    "event": "login"
  },
  "sort": [
    1383108999000
  ]
}

让我们使用Distinct Date Histogram Facet来获取10月19日至21日每天的唯一用户数量。

$ curl -XPOST 'http://localhost:9200/logstash-2013.10.19,logstash-2013.10.20,logstash-2013.10.21/_search?pretty' -d '{
    "query" : {
        "match_all" : {}
    },
    "facets" : {
        "0" : {
            "distinct_date_histogram" : {
                "field" : "@timestamp",
                "value_field" : "user_id",
                "interval" : "day"
            }
        }
    }
}
'

然后得到了这样的结果,我们发现,19日有297人访问,20日有322人访问,21日有754人访问。

{
  "took" : 232,
  "timed_out" : false,
  "_shards" : {
    "total" : 15,
    "successful" : 15,
    "failed" : 0
  },
  "hits" : {
    "total" : 105334,
    "max_score" : 1.0,
    "hits" : [ 
      ...(中略)...
    ]
  },
  "facets" : {
    "0" : {
      "_type" : "distinct_date_histogram",
      "entries" : [{
        "time" : 1382140800000,
        "count" : 297
      }, {
        "time" : 1382227200000,
        "count" : 322
      }, {
        "time" : 1382313600000,
        "count" : 754
      } ],
      "count" : 924
    }
  }
}

通过以下方法,您还可以获取每小时登录的唯一用户数量,日期为10月19日。

$ curl -XPOST 'http://localhost:9200/logstash-2013.10.19/_search?pretty' -d '{
    "query" : {
        "match_all" : {}
    },
    "facets" : {
        "0" : {
            "distinct_date_histogram" : {
                "field" : "@timestamp",
                "value_field" : "user_id",
                "interval" : "1h"
            },
           "facet_filter": {
             "term": { "event" : "login" }
           }
        }
    }
}
'

真方便啊。

然而,要在Kibana3中显示这个,就必须对Kibana3进行一些调整…

广告
将在 10 秒后关闭
bannerAds