使用RavenDB索引按小时计数(Counting by hour with a RavenDB index)

编程入门 行业动态 更新时间:2024-10-07 00:16:40
使用RavenDB索引按小时计数(Counting by hour with a RavenDB index)

我正在使用RavenDB存储一系列事件。 这些事件有一个我用来按日分组的日期(DateTime.Date)。 我试图按小时添加一些统计数据,但我似乎找不到干净利落的方法。

简单的方法:

public class DailyStats : AbstractIndexCreationTask<Incident, DateStat> { public DailyStats() { Map = docs => from doc in docs select new { doc.OccuredOn, Hour0 = doc.OccuredOn.Hour == 0 ? 1 : 0 Hour1 = doc.OccuredOn.Hour == 1 ? 1 : 0 //.... }; Reduce = mapped => from m in mapped group m by new { m.Date.Date } into g select new { g.Key.Date, Hour0 = g.Sum(x => x.Hour0), Hour1 = g.Sum(x => x.Hour1) //.... } } }

但这是非常重复的。 相反,我正在尝试使用字典:

public class DailyStats : AbstractIndexCreationTask<Incident, DateStat> { public DailyStats() { Map = docs => from doc in docs select new { doc.OccuredOn, IncidentsByHour = Enumerable.Range(0, 24).ToDictionary(h => h, h => doc.IncidentDate.Hour == h ? 1 : 0), }; Reduce = mapped => from m in mapped group m by new { m.Date.Date } into g select new { g.Key.Date, IncidentsByHour = Enumerable.Range(0, 24).Select(h => g.Sum(x => x.IncidentsByHour[h])), } } }

抛出异常:

第201行,第22位:错误CS1502 - “System.Linq.Enumerable.ToDictionary(System.Collections.Generic.IEnumerable,System.Func,System.Collections.Generic.IEqualityComparer)”的最佳重载方法匹配具有一些无效的参数Line 201,位置72:错误CS1503 - 参数2:无法从'System.Func'转换为'System.Func'第201行,第106位:错误CS1503 - 参数3:无法从'System.Func'转换为'System.Collections .Generic.IEqualityComparer'第274行,第22位:错误CS1928 - 'System.Collections.Generic.IEnumerable'不包含'Select'的定义和最佳扩展方法重载'System.Linq.Enumerable.Select(System.Collections) .Generic.IEnumerable,System.Func)'有一些无效的参数第274行,第54位:错误CS1503 - 参数2:无法从'System.Func'转换为'System.Func'

我不知道如何解决这个异常,因为它发生在Raven方面。

白天分组的原因是我需要提取365天的统计数据,但仍然需要一小时的基本信息。 相反,有两个索引,一天一个,一个一个小时(总共365 + 24个记录加载。更好吗?我的理解是更大但更少的索引是最好的)?

I'm using RavenDB to store a collection of incidents. These incidents have a date that I'm using to group by day (DateTime.Date). I'm trying to add some stats by hour, but I can't seem to find a way to do it cleanly.

the simple way:

public class DailyStats : AbstractIndexCreationTask<Incident, DateStat> { public DailyStats() { Map = docs => from doc in docs select new { doc.OccuredOn, Hour0 = doc.OccuredOn.Hour == 0 ? 1 : 0 Hour1 = doc.OccuredOn.Hour == 1 ? 1 : 0 //.... }; Reduce = mapped => from m in mapped group m by new { m.Date.Date } into g select new { g.Key.Date, Hour0 = g.Sum(x => x.Hour0), Hour1 = g.Sum(x => x.Hour1) //.... } } }

but this is horribly repetitive. Instead, I'm trying to use a dictionary:

public class DailyStats : AbstractIndexCreationTask<Incident, DateStat> { public DailyStats() { Map = docs => from doc in docs select new { doc.OccuredOn, IncidentsByHour = Enumerable.Range(0, 24).ToDictionary(h => h, h => doc.IncidentDate.Hour == h ? 1 : 0), }; Reduce = mapped => from m in mapped group m by new { m.Date.Date } into g select new { g.Key.Date, IncidentsByHour = Enumerable.Range(0, 24).Select(h => g.Sum(x => x.IncidentsByHour[h])), } } }

which throws the exception:

Line 201, Position 22: Error CS1502 - The best overloaded method match for 'System.Linq.Enumerable.ToDictionary(System.Collections.Generic.IEnumerable, System.Func, System.Collections.Generic.IEqualityComparer)' has some invalid arguments Line 201, Position 72: Error CS1503 - Argument 2: cannot convert from 'System.Func' to 'System.Func' Line 201, Position 106: Error CS1503 - Argument 3: cannot convert from 'System.Func' to 'System.Collections.Generic.IEqualityComparer' Line 274, Position 22: Error CS1928 - 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Select' and the best extension method overload 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments Line 274, Position 54: Error CS1503 - Argument 2: cannot convert from 'System.Func' to 'System.Func'

I'm not sure how to resolve this exception, since it's happening on the Raven side.

The reason for grouping by day is I need to pull 365 days worth of stats, but still have some basic information by hour. Would it be better to instead have two indexes, one by day and one by hour (for a total of 365 + 24 records loaded. My understanding is that bigger but fewer indices are best)?

最满意答案

尝试这个:

public class DailyStats : AbstractIndexCreationTask<Incident, DateStat> { public DailyStats() { Map = docs => from doc in docs select new { Date = doc.OccuredOn, IncidentsByHour = new Dictionary<int, int> { { doc.OccuredOn.Hour, 1 } } }; Reduce = mapped => from m in mapped group m by new { m.Date.Date } into g select new { Date = g.Key, IncidentsByHour = g.SelectMany(x => x.IncidentsByHour) .GroupBy(x => x.Key) .OrderBy(x => x.Key) .ToDictionary(x => x.Key, x => x.Sum(y => y.Value)) }; } }

这里唯一的区别是,在没有事件的几个小时内,您不会在字典中获得任何项目。

Raven确实存在某种错误。 地图应该能够写成:

IncidentsByHour = Enumerable.Range(0, 24) .ToDictionary(h => h, h => doc.OccuredOn.Hour == h ? 1 : 0)

但它出于一些奇怪的原因失败了。 我会报告这是一个错误。

是的,与许多小型索引相比,拥有更少的大型索引通常会更好。

Try this:

public class DailyStats : AbstractIndexCreationTask<Incident, DateStat> { public DailyStats() { Map = docs => from doc in docs select new { Date = doc.OccuredOn, IncidentsByHour = new Dictionary<int, int> { { doc.OccuredOn.Hour, 1 } } }; Reduce = mapped => from m in mapped group m by new { m.Date.Date } into g select new { Date = g.Key, IncidentsByHour = g.SelectMany(x => x.IncidentsByHour) .GroupBy(x => x.Key) .OrderBy(x => x.Key) .ToDictionary(x => x.Key, x => x.Sum(y => y.Value)) }; } }

The only difference here is that you won't get any items in your dictionary for hours that have no incidents.

There is indeed some kind of bug with Raven still. The map should be able to be written with this:

IncidentsByHour = Enumerable.Range(0, 24) .ToDictionary(h => h, h => doc.OccuredOn.Hour == h ? 1 : 0)

But it fails for some strange reason. I'll report that as a bug.

And yes, it is usually better to have fewer larger indexes than many small ones.

更多推荐

本文发布于:2023-08-07 13:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   小时   RavenDB   index   hour

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!