使用.NET Client的RavenDB Map-Reduce示例 [英] RavenDB Map-Reduce Example using .NET Client

查看:81
本文介绍了使用.NET Client的RavenDB Map-Reduce示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个如何在RavenDB .NET Client中实现和使用Map-Reduce的示例.

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.

将存储在RavenDB中的样本文档:

A sample document that would be stored within RavenDB:

public class StatisticsEntry
{
    public string Id { get; set; }
    public string UserId { get; set; }
}

我可以弄清楚如何使用Map创建标准索引,但是对于如何实际使用Reduce函数然后检索结果却一无所知.

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.

不幸的是,RavenDB网站上提供的示例并未说明发生了什么我可以理解如何通过.NET API使用它,并且这些示例似乎根本没有使用.NET API来实现.

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.

推荐答案

地图缩小索引只是我要分组依据"的另一种说法,只有分组依据是预先定义的,RavenDB会在后台高效地对其进行处理,因此在查询时您正在查找预先计算的结果.

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)

 var results = from doc in docs
 group doc by doc.UserId into g
 select new
 {
      g.UserId,
      g.Count()
 }

忽略创建的数组的实际内容,我们可以通过询问获得总结果

Ignoring the actual contents of the created array, we can get the total results by asking for

 results.Length

如您所愿.

在RavenDB中,您将此功能拆分为Map和Reduce,最后得到了

In RavenDB, you split out this function into a Map and a Reduce, and you end up with

public class UniqueVisitorsResult
{
     public string UserId { get; set; }
     public int Count { get; set; }
}

public class UniqueVisitorsIndex : AbstractIndexCreationTask<StatisticsEntry, UniqueVisitorsResult>
{
    public UniqueVisitorsIndex ()
    {
        Map = docs=> from doc in docs
                         select new 
                         { 
                             UserId = doc.UserId, 
                             Count = 1 
                         };
        Reduce = results => from result in results
                        group result by result.UserId into g
                        select new 
                        { 
                            UserId = g.Key, 
                            Count = g.Sum(x=>x.Count) 
                        };
    }
}

从本质上讲,这与上面的相同-但您已将其变成了MapReduce函数;-)

In essence, this is the same as the above - but you've turned it into a MapReduce function ;-)

 session.Query<StatisticEntry, UniqueVisitorsIndex>().Count();

假设Count已在LINQ提供程序中正确实现(我认为是iirc),就会给您唯一身份访问者的总数

Will give you the total number of unique visitors, assuming Count has been implemented properly in the LINQ provider (iirc I think it has)

条目总数仅为

 session.Query<StatisticEntry>().Count();

如您所愿(无需映射/减少)

As you'd expect (No map/reduce required)

注意:该索引还可以用于查看特定用户的点击次数,因为在索引中正在计算计数,如果您不关心计数,则删除MapReduce的该部分并执行

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

public class UniqueVisitorsIndex : AbstractIndexCreationTask<StatisticsEntry>
{
    public UniqueVisitorsIndex ()
    {
        Map = docs=> from doc in docs
                     select new 
                     { 
                         UserId = doc.UserId
                     };
        Reduce = results => from result in results
                    group result by result.UserId into g
                    select new 
                    { 
                        UserId = g.Key
                    };
    }
}

这篇关于使用.NET Client的RavenDB Map-Reduce示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆