使用 .NET 客户端的 RavenDB Map-Reduce 示例
我正在寻找如何在 RavenDB .NET 客户端中实现和使用 Map-Reduce 的示例。
我想将其应用到特定场景:生成唯一访客数和总访客数。
将存储在 RavenDB 中的示例文档:
public class StatisticsEntry
{
public string Id { get; set; }
public string UserId { get; set; }
}
我可以弄清楚如何使用 Map 创建标准索引,但我不知道如何实际使用 Reduce 函数,然后检索结果。
不幸的是,RavenDB 网站上提供的 示例 没有解释发生了什么,以便我可以了解如何通过 .NET API 使用它,并且示例似乎根本没有使用 .NET API 来实现这一点。
I'm looking for an example of how to implement and use Map-Reduce within the RavenDB .NET Client.
I'd like to apply it to a specific scenario: generating unique and total visitor counts.
A sample document that would be stored within RavenDB:
public class StatisticsEntry
{
public string Id { get; set; }
public string UserId { get; set; }
}
I can figure out how to create a standard index using Map, but I'm lost as to how to actually use the Reduce function, and then retrieve the results.
Unfortunately, the example provided on the RavenDB Site doesn't explain what's going on so that I can understand how to use it via the .NET API, and the samples don't seem to implement this at all using the .NET API.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MapReduce索引只是“我想做一个分组依据”的另一种方式,只有分组依据是预先定义的,RavenDB将在后台以有效的方式处理它,所以在查询时你正在查找预先计算的结果。
将以下内容视为普通分组的答案(对于唯一用户)
询问来获得总结果
忽略创建的数组的实际内容,我们可以通过按照您的预期
。在 RavenDB 中,您将此函数拆分为 Map 和 Reduce,最终得到
本质上,这与上面的相同 - 但您已将其转换为 MapReduce 函数 ;-)
将为您提供总数唯一访问者的数量,假设 Count 已在 LINQ 提供程序中正确实现(iirc 我认为它已实现)
条目总数正如
您所期望的那样(无需映射/减少)
注意:此索引也可用于查看特定用户的点击次数,因为计数是在索引中计算的,如果您不关心计数,则删除 MapReduce 的该部分并执行
A map reduce index is just another way of saying "I want to do a group by", only the group by is pre-defined up front and RavenDB will process it in an efficient manner in the background so at query time you are looking up a pre-calculated result.
Consider the following as an answer as an ordinary group by (for unique users)
Ignoring the actual contents of the created array, we can get the total results by asking for
as you'd expect.
In RavenDB, you split out this function into a Map and a Reduce, and you end up with
In essence, this is the same as the above - but you've turned it into a MapReduce function ;-)
Will give you the total number of unique visitors, assuming Count has been implemented properly in the LINQ provider (iirc I think it has)
The total number of entries is simply
As you'd expect (No map/reduce required)
Note: this index can also be used to see the number of hits by a specific user, as the Count is being calculated in the index, if you don't care about the count then drop that part of the MapReduce and do
以下是如何为唯一访问者构建索引:
然后您可以使用以下方法进行查询:
对于访问者总数,您可以使用:
Here is how you can build an index for unique visitors:
You can then query this using:
For total count of visitors, you can use: