使用 .NET 客户端的 RavenDB Map-Reduce 示例

发布于 2024-10-03 13:16:27 字数 482 浏览 5 评论 0原文

我正在寻找如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-10-10 13:16:27

MapReduce索引只是“我想做一个分组依据”的另一种方式,只有分组依据是预先定义的,RavenDB将在后台以有效的方式处理它,所以在查询时你正在查找预先计算的结果。

将以下内容视为普通分组的答案(对于唯一用户)

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

询问来获得总结果

 results.Length

忽略创建的数组的实际内容,我们可以通过按照您的预期

。在 RavenDB 中,您将此函数拆分为 Map 和 Reduce,最终得到

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 函数 ;-)

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

将为您提供总数唯一访问者的数量,假设 Count 已在 LINQ 提供程序中正确实现(iirc 我认为它已实现)

条目总数正如

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

您所期望的那样(无需映射/减少)

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

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
                    };
    }
}

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

as you'd expect.

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) 
                        };
    }
}

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

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

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

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

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

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
                    };
    }
}
梦里南柯 2024-10-10 13:16:27

以下是如何为唯一访问者构建索引:

public class Statistics_UniqueVisitors : AbstractIndexCreationTask<StatisticsEntry>
{
    public Statistics_UniqueVisitors()
    {
        Map = entries => from entry in entries
                         select new { entry.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) };
    }
}

然后您可以使用以下方法进行查询:

var numberOfUniqueVisitors = s.Query<StatisticEntry, Statistics_UniqueVisitors>().Count();

对于访问者总数,您可以使用:

var numberOfVisitors = s.Query<StatisticEntry>().Count();

Here is how you can build an index for unique visitors:

public class Statistics_UniqueVisitors : AbstractIndexCreationTask<StatisticsEntry>
{
    public Statistics_UniqueVisitors()
    {
        Map = entries => from entry in entries
                         select new { entry.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) };
    }
}

You can then query this using:

var numberOfUniqueVisitors = s.Query<StatisticEntry, Statistics_UniqueVisitors>().Count();

For total count of visitors, you can use:

var numberOfVisitors = s.Query<StatisticEntry>().Count();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文