【问题标题】:Map reduce in RavenDbRavenDb 中的映射减少
【发布时间】:2012-06-04 01:48:03
【问题描述】:

更新 1,遵循 Ayende 的回答

这是我第一次进入 RavenDb 并尝试使用它,我写了一个小 map/reduce,但不幸的是结果是空的?

我在 RavenDb 中加载了大约 160 万个文档

一份文件:

public class Tick
{
    public DateTime Time;
    public decimal Ask;
    public decimal Bid;
    public double AskVolume;
    public double BidVolume;
}

并希望获得特定时间段内的最低和最高询价。

按时间收集的定义为:

var ticks = session.Query<Tick>().Where(x => x.Time > new DateTime(2012, 4, 23) && x.Time < new DateTime(2012, 4, 24, 00, 0, 0)).ToList();

这给了我 90280 个文件,到目前为止一切顺利。

然后是map/reduce:

Map = rows => from row in rows 
                          select new
                          {
                              Max = row.Bid,
                              Min = row.Bid, 
                              Time = row.Time,
                              Count = 1
                          };

Reduce = results => from result in results
                                group result by new{ result.MaxBid, result.Count} into g
                                select new
                                {
                                    Max = g.Key.MaxBid,
                                    Min = g.Min(x => x.MaxBid),
                                    Time = g.Key.Time,
                                    Count = g.Sum(x => x.Count)

                                };

...

private class TickAggregationResult
{
    public decimal MaxBid { get; set; }
        public decimal MinBid { get; set; }
        public int Count { get; set; }

    }

然后我创建索引并尝试查询它:

Raven.Client.Indexes.IndexCreation.CreateIndexes(typeof(TickAggregation).Assembly, documentStore);


        var session = documentStore.OpenSession();

        var g1 = session.Query<TickAggregationResult>(typeof(TickAggregation).Name);


        var group = session.Query<Tick, TickAggregation>()
                         .Where(x => x.Time > new DateTime(2012, 4, 23) && 
                                     x.Time < new DateTime(2012, 4, 24, 00, 0, 0)
                                  )
            .Customize(x => x.WaitForNonStaleResults())
                                           .AsProjection<TickAggregationResult>();

但是该组只是空的:(

如您所见,我尝试了两个不同的查询,我不确定其中的区别,有人可以解释一下吗?

现在我收到一个错误:

组还是空的:(

让我解释一下我想用纯 sql 完成什么:

select min(Ask), count(*) as TickCount from Ticks 
where Time between '2012-04-23' and '2012-04-24)

【问题讨论】:

标签: c# mapreduce ravendb


【解决方案1】:

这不是您使用 Map/reduce 的方式

 from row in rows where row.Time > new DateTime(2012, 4, 23) && row.Time < new DateTime(2012, 4, 24, 00, 0, 0)

创建一个map reduce索引,将时间作为键的一部分合并到组中,然后您可以对其进行查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多