云量指数:每个时间段的唯一身份用户数 [英] cloudant index: count number of unique users per time period
问题描述
此处发表了与此问题非常相似的帖子.在cloudant中,我有一个文档结构,用于存储用户访问应用程序时的内容,如下所示:
A very similar post was made about this issue here. In cloudant, I have a document structure storing when users access an application, that looks like the following:
{"username":"one","timestamp":"2015-10-07T15:04:46Z"}
--- |同一天
{"username":"one","timestamp":"2015-10-07T19:22:00Z"}
--- ^
{"username":"one","timestamp":"2015-10-25T04:22:00Z"}
{"username":"two","timestamp":"2015-10-07T19:22:00Z"}
{"username":"one","timestamp":"2015-10-07T15:04:46Z"}
---| same day
{"username":"one","timestamp":"2015-10-07T19:22:00Z"}
---^
{"username":"one","timestamp":"2015-10-25T04:22:00Z"}
{"username":"two","timestamp":"2015-10-07T19:22:00Z"}
我想知道的是计算给定时间段内的唯一身份用户数.例如:
What I want to know is to count the # of unique users for a given time period. Ex:
2015-10-07 = {"count": 2}
2015年10月7日访问了两个不同的用户
2015-10-25 = {"count": 1}
2015年10月25日访问了另一个用户
2015 = {"count" 2}
2015年访问了两个不同的用户
2015-10-07 = {"count": 2}
two different users accessed on 2015-10-07
2015-10-25 = {"count": 1}
one different user accessed on 2015-10-25
2015 = {"count" 2}
two different users accessed in 2015
这一切都变得很棘手,因为例如在2015-10-07上,用户名:一个具有两次访问时间的记录,但它只应返回不重复总数的1用户.
This all just becomes tricky because for example on 2015-10-07, username: one has two records of when they accessed, but it should only return a count of 1 to the total of unique users.
我尝试过:
function(doc) {
var time = new Date(Date.parse(doc['timestamp']));
emit([time.getUTCFullYear(),time.getUTCMonth(),time.getUTCDay(),doc.username], 1);
}
这有几个问题,耶稣·阿尔瓦(Jesus Alva)在我上面链接的帖子中发表了评论.
This suffers from several issues, which are highlighted by Jesus Alva who commented in the post I linked to above.
谢谢!
推荐答案
也许有更好的方法,但是却无济于事...
There's probably a better way of doing this, but off the top of my head ...
您可以尝试为每个粒度级别发出一个索引:
You could try emitting an index for each level of granularity:
function(doc) {
var time = new Date(Date.parse(doc['timestamp']));
var year = time.getUTCFullYear();
var month = time.getUTCMonth()+1;
var day = time.getUTCDate();
// day granularity
emit([year,month,day,doc.username], null);
// year granularity
emit([year,doc.username], null);
}
// reduce function - `_count`
每日查询(2015-10-07):
Day query (2015-10-07):
inclusive_end=true&
start_key=[2015, 10, 7, "\u0000"]&
end_key=[2015, 10, 7, "\uefff"]&
reduce=true&
group=true
日查询结果-您的应用程序代码将计算行数:
Day query result - your application code would count the number of rows:
{"rows":[
{"key":[2015,10,7,"one"],"value":2},
{"key":[2015,10,7,"two"],"value":1}
]}
年度查询:
inclusive_end=true&
start_key=[2015, "\u0000"]&
end_key=[2015, "\uefff"]&
reduce=true&
group=true
查询结果-您的应用程序代码将计算行数:
Query result - your application code would count the number of rows:
{"rows":[
{"key":[2015,"one"],"value":3},
{"key":[2015,"two"],"value":1}
]}
这篇关于云量指数:每个时间段的唯一身份用户数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!