Created
September 1, 2011 09:24
-
-
Save Andrea/1185789 to your computer and use it in GitHub Desktop.
Map reduce by Date by source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
OtherStuff(); | |
} | |
private static void OtherStuff() | |
{ | |
var store = new DocumentStore { Url = "http://bfsuser01:8080/" }; | |
store.Initialize(); | |
IndexCreation.CreateIndexes(typeof(PriceDocuments_ByDateBySource).Assembly, store); | |
Guid productId = Guid.NewGuid(); | |
CreateAndStoreProductPriceWithSameDateAndSameSource(store, productId); | |
using (var storeSession = store.OpenSession()) | |
{ | |
var productPriceDocuments = storeSession.Query<PriceDocument, PriceDocuments_ByDateBySource>(); | |
Console.WriteLine("Prices :"+ productPriceDocuments.Count()); | |
} | |
Console.ReadLine(); | |
} | |
private static void CreateAndStoreProductPriceWithSameDateAndSameSource(DocumentStore store, Guid productId) | |
{ | |
using (var storeSession = store.OpenSession()) | |
{ | |
var bloomberg = "Moody"; | |
var pricingDate = new DateTime(2011, 4, 1, 8, 0, 0); | |
var productPriceDocument = new PriceDocument { AskPrice = 1m, BidPrice = 1m, Id = productId + "/1", PricingDate = pricingDate, PriceId = productId, Source = bloomberg, Version = 1 }; | |
var productPriceDocument1 = new PriceDocument { AskPrice = 1m, BidPrice = 1m, Id = productId + "/2", PricingDate = new DateTime(2011, 4, 1, 9, 0, 0), PriceId = productId, Source = bloomberg, Version = 2 }; | |
storeSession.Store(productPriceDocument); | |
storeSession.Store(productPriceDocument1); | |
storeSession.SaveChanges(); | |
} | |
} | |
public class PriceDocument | |
{ | |
public Guid PriceId { get; set; } | |
public decimal AskPrice { get; set; } | |
public decimal BidPrice { get; set; } | |
public DateTime PricingDate { get; set; } | |
public string Source { get; set; } | |
public int Version { get; set; } | |
public string Id { get; set; } | |
} | |
public PriceDocuments_ByDateBySource() | |
{ | |
Map = docs => from priceDocument in docs | |
select new | |
{ | |
PricingDate = new DateTime(priceDocument.PricingDate.Year, priceDocument.PricingDate.Month, priceDocument.PricingDate.Day), | |
PricingSource = priceDocument.Source, | |
PriceId = priceDocument.PriceId, | |
ProductVersion = priceDocument.Version | |
}; | |
Reduce = results => from result in results | |
group result by | |
new | |
{ | |
result.PricingDate, | |
result.Source, | |
} | |
into price | |
select new | |
{ | |
PriceId = price.Select(x=> x.PriceId), | |
PricingDay = price.Key.PricingDate, | |
PricingSource = price.Key.Source, | |
ProductVersion = price.Max(p=> p.Version) | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment