RavenDb中的地图缩小

时间:2012-05-29 20:04:23

标签: c# mapreduce ravendb

根据Ayende的回答

更新1

这是我第一次进入RavenDb并试验它我写了一个小地图/ reduce,但不幸的是结果是空的?

我有大约160万个文档加载到RavenDb

文件:

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 = 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>();

但该小组只是空的:(

正如你所看到我尝试了两种不同的查询,我不确定区别,有人可以解释一下吗?

现在收到错误消息: enter image description here

该组仍然是空的:(

让我解释一下我在纯sql中想要完成的事情:

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

1 个答案:

答案 0 :(得分: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)

创建一个地图缩小索引,该索引将时间作为按键分组的一部分,然后您可以对其进行查询。