public int AnalyzeReviews(string jobid) { BsonJavaScript map = @" function() { var data = this.reviews.reviews; var jid = this.jobid; var src = this.source; data.forEach(function(d){ var key = { rating : d.rating, jobid : jid, source : src}; var value = { count: 1 }; emit(key, value); }); }"; BsonJavaScript reduce = @" function(key, values) { var result = {count : 0}; values.forEach(function(value){ result.count += value.count; }); return result; }"; var collection = _database.GetCollection<BsonDocument>("Reviews"); var options = new MapReduceOptions<BsonDocument, BsonDocument>(); options.OutputOptions = MapReduceOutputOptions.Merge("ReviewResults"); var filter = Builders<BsonDocument>.Filter.Eq("jobid", jobid); options.Filter = filter; var asyncresults = collection.MapReduceAsync<BsonDocument>(map, reduce, options); while (!asyncresults.IsCompleted) ; return asyncresults.Exception == null ? 1 : -1; }
public static void testMongo() { //Create a client and collect tot database var _client = new MongoClient (); //replace "test" with your own database var _database = _client.GetDatabase ("test"); //Get a collection to prcess //replace the collection name with your own var actors = _database.GetCollection<BsonDocument> ("actors"); //Create filters to select specific code //select * filter //var filter = new BsonDocument (); //select * from actors where name = equal to "actor name" var anotherFilter = Builders<BsonDocument>.Filter.Eq ("name", "actor name"); //Select from acotrs collection based on the filer rule //var result = actors.Find (anotherFilter).ToList (); //Select from acotrs collection based on the filer rule and get the cursor from the server //var cursor = actors.Find (new BsonDocument ()).ToCursor(); //Iterate throu result set //foreach (var document in cursor.ToEnumerable ()) { // Console.WriteLine (document); //} //Mapper function for the collection BsonJavaScript actor_map = "function() { " + "emit(this.actor_id, {'name' : this.name});}"; //Reduce function which does nothing than returning the result BsonJavaScript reduce = "function(key, values) { " + "values.forEach(" + "function(value) {" + "return value;" + "});" + "}"; //Options for the result of the output var options = new MapReduceOptions<BsonDocument, BsonDocument> (); options.OutputOptions = MapReduceOutputOptions.Inline; //Excute map and reduce functions var resultMR = actors.MapReduce (actor_map,reduce, options); //print first result only Console.WriteLine(resultMR.First ()); }
public async Task RunAsync() { BsonJavaScript map = @"function() { for (var idx = 0; idx < this.threads.length; idx++) { var tags = this.threads[idx].tags; if (tags === undefined) { continue; } for (var j = 0; j < tags.length; j++) { var key = { user: this.id, tag: tags[j], month: '" + _month + @"' }; var value = { count: 1, }; emit(key, value); } } }"; BsonJavaScript reduce = @"function(key, values) { reducedVal = { count: 0 }; for (var idx = 0; idx < values.length; idx++) { reducedVal.count += values[idx].count; } return reducedVal; }"; var options = new MapReduceOptions<BsonDocument, BsonDocument>(); options.OutputOptions = MapReduceOutputOptions.Merge(_repository + "_user_tags"); options.Filter = "{month: '" + _month +"'}"; await _userActivityCollection.MapReduceAsync(map, reduce, options); }
public static double[] RsiMR(string code, DateTime targetDate, int period = 14, int numberOfData = 1) { if (period <= 0) { throw new IndicatorException("Periyot pozitif sayı olmalıdır."); } else if (numberOfData <= 0) { throw new IndicatorException("Gösterilecek veri sayısı pozitif sayı olmalıdır."); } int dataCount = IndicatorService.DataCount(code, targetDate); if (dataCount < period) { throw new IndicatorException(IndicatorException.DATA_NOT_ENOUGH_MESSAGE); } if (dataCount < numberOfData + period - 1) { numberOfData = dataCount - period + 1; } int limit = numberOfData + period - 1; var filter = new BsonDocument { { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) } }; var sort = new BsonDocument("Tarih", -1); var projection = new BsonDocument { { "_id", 0 }, { "Tarih", 1 } }; var startingDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, numberOfData - 1).GetElement(0).Value.ToLocalTime(); var lastDate = MongoDBService.GetService().FindOneSortProject(filter, sort, projection).GetElement(0).Value.ToLocalTime(); var limitDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, limit - 1).GetElement(0).Value.ToLocalTime(); var dates = IndicatorService.GetData(code, targetDate, "Tarih", limit).Select(p => p.GetElement(0).Value.ToLocalTime()).ToArray(); MapReduceOptions <BsonDocument, BsonDocument> options = new MapReduceOptions <BsonDocument, BsonDocument> { Filter = new BsonDocument { { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) } }, Sort = new BsonDocument("Tarih", -1), Scope = new BsonDocument { { "startingDate", startingDate }, { "lastDate", lastDate }, { "limitDate", limitDate }, { "period", period }, { "numberOfData", numberOfData }, { "binarySearch", MapReduceHelpers.BinarySearch }, { "equalityFunction", MapReduceHelpers.IsDatesEqual }, { "dates", new BsonArray(dates) } }, OutputOptions = MapReduceOutputOptions.Replace("rsiOut", "financialData", false) }; double[] avg; BsonJavaScript mapper = new BsonJavaScript(@" function() { if (!equalityFunction(this.Tarih, limitDate) && this.Tarih < limitDate) return; else if (!equalityFunction(this.Tarih, lastDate) && this.Tarih > lastDate) return; var dateIndex; dateIndex = binarySearch(dates, this.Tarih); if (dateIndex == -1) return; for (var i = 0; i < period && dateIndex >= 0; i++, dateIndex--) { if (dates[dateIndex] > startingDate || equalityFunction(dates[dateIndex], startingDate)) emit(dates[dateIndex], {date: this.Tarih, headClose : this.Kapanis, tailClose : this.Kapanis, gain: 0 , loss: 0}); } } "); BsonJavaScript reducer = new BsonJavaScript(@" function(key, values) { values.sort(function(a, b) { return a.date < b.date; }); sumGain = values[0].gain; sumLoss = values[0].loss; var diff; for(var i = 0; i < values.length - 1; i++) { sumGain += values[i+1].gain; sumLoss += values[i+1].loss; diff = values[i].tailClose - values[i+1].headClose; if(diff > 0) sumGain += diff; else sumLoss -= diff; } return {date: values[0].date, headClose: values[0].headClose, tailClose: values[values.length-1].tailClose, gain: sumGain, loss: sumLoss}; } "); BsonJavaScript finalizer = new BsonJavaScript(@" function(key, reducedValue) { if (reducedValue.loss == 0) return 100; reducedValue = 100 - 100 / (1 + (reducedValue.gain / reducedValue.loss)); return reducedValue; } "); options.Finalize = finalizer; List <BsonDocument> resultSet = MongoDBService.GetService().MapReduceMany(mapper, reducer, options); avg = new double[numberOfData]; for (int i = 0, j = numberOfData - 1; i < numberOfData; i++, j--) { avg[i] = resultSet.ElementAt(j).GetElement(1).Value.ToDouble(); } return(avg); }
public void TypedMapReduceOptionSetsCollectionName() { var options = new MapReduceOptions<ReduceProduct>(); Assert.Equal(typeof(ReduceProduct).Name, options.CollectionName); }
public abstract IAsyncCursor <TResult> MapReduce <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TDocument, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken));
private static void Mr1() { var map = @" function() { var movie = this; emit(movie.Category, { count: 1, totalMinutes: movie.Minutes }); }"; var reduce = @" function(key, values) { var result = {count: 0, totalMinutes: 0 }; values.forEach(function(value){ result.count += value.count; result.totalMinutes += value.totalMinutes; }); return result; }"; var finalize = @" function(key, value){ value.average = value.totalMinutes / value.count; return value; }"; //var x = MongoDB.Bson.BsonJavaScript.Create() var client = new MongoClient("mongodb://*****:*****@ds041188.mongolab.com:41188/testreduce1"); var database = client.GetDatabase("testreduce1"); //var m = new MapReduceOptions<> var collection = database.GetCollection <Movie>("movies"); //var builder = new MapReduceOutputToCollectionOperation() //var y = new MapReduceOperation<> var options = new MapReduceOptions <Movie, BsonDocument>(); //new MapReduceOptionsBuilder(); //options.JavaScriptMode = true; //options.OutputOptions.ToJson(); //var col = new MapReduceOutputToCollectionOperation(new CollectionNamespace("testreduce1", "movies"), // new CollectionNamespace("testreduce1", "reduce1"), map, reduce, new MessageEncoderSettings()); //var x = col.ExecuteAsync() //options.OutputOptions. //options. //options.OutputOptions.ToJson(new JsonWriterSettings()); //options. //options.SetFinalize(finalize); //options.SetOutput(MapReduceOutput.Inline); var results = collection.MapReduceAsync(map, reduce, options); results.Wait(); var x = results; var y = x.Result.MoveNextAsync().ToBsonDocument(); //var h = results; //results.Start(); //results.Wait(); //var x = results.Result; //results.Wait(); //results. //options //var results = collection.MapReduce(map, reduce, options); //foreach (var result in results.GetResults()) //{ // Console.WriteLine(result.ToJson()); //} }
/// <inheritdoc/> public IAsyncCursor <TResult> MapReduce <TResult>(IClientSessionHandle session, BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <T, TResult> options = null, CancellationToken cancellationToken = default) { return(_actualCollection.MapReduce(session, map, reduce, options, cancellationToken)); }
public IAsyncCursor <TResult> MapReduce <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <T, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken)) { return(collection.MapReduce(map, reduce, options, cancellationToken)); }
public Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <InformationEntry, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken)) { throw new System.NotImplementedException(); }
private static void Mr1() { var map = @" function() { var movie = this; emit(movie.Category, { count: 1, totalMinutes: movie.Minutes }); }"; var reduce = @" function(key, values) { var result = {count: 0, totalMinutes: 0 }; values.forEach(function(value){ result.count += value.count; result.totalMinutes += value.totalMinutes; }); return result; }"; var finalize = @" function(key, value){ value.average = value.totalMinutes / value.count; return value; }"; //var x = MongoDB.Bson.BsonJavaScript.Create() var client = new MongoClient("mongodb://*****:*****@ds041188.mongolab.com:41188/testreduce1"); var database = client.GetDatabase("testreduce1"); //var m = new MapReduceOptions<> var collection = database.GetCollection<Movie>("movies"); //var builder = new MapReduceOutputToCollectionOperation() //var y = new MapReduceOperation<> var options = new MapReduceOptions<Movie, BsonDocument>(); //new MapReduceOptionsBuilder(); //options.JavaScriptMode = true; //options.OutputOptions.ToJson(); //var col = new MapReduceOutputToCollectionOperation(new CollectionNamespace("testreduce1", "movies"), // new CollectionNamespace("testreduce1", "reduce1"), map, reduce, new MessageEncoderSettings()); //var x = col.ExecuteAsync() //options.OutputOptions. //options. //options.OutputOptions.ToJson(new JsonWriterSettings()); //options. //options.SetFinalize(finalize); //options.SetOutput(MapReduceOutput.Inline); var results = collection.MapReduceAsync(map, reduce, options); results.Wait(); var x = results; var y = x.Result.MoveNextAsync().ToBsonDocument(); //var h = results; //results.Start(); //results.Wait(); //var x = results.Result; //results.Wait(); //results. //options //var results = collection.MapReduce(map, reduce, options); //foreach (var result in results.GetResults()) //{ // Console.WriteLine(result.ToJson()); //} }
public virtual IAsyncCursor <TResult> MapReduce <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TVal, TResult> options = null, CancellationToken cancellationToken = new CancellationToken()) => throw new NotImplementedException();
public void TypedMapReduceOptionSetsCollectionName() { var options = new MapReduceOptions <ReduceProduct>(); Assert.Equal(typeof(ReduceProduct).Name, options.CollectionName); }
static async Task MainAsync() { List <User> luser = new List <User>(); var client = new MongoClient(); IMongoDatabase db = client.GetDatabase("db"); var collection = db.GetCollection <BsonDocument>("Users"); using (IAsyncCursor <BsonDocument> cursor = await collection.FindAsync(new BsonDocument())) { while (await cursor.MoveNextAsync()) { IEnumerable <BsonDocument> batch = cursor.Current; foreach (var document in batch) { var user = new User { id = (ObjectId)document[0], name = (string)document[1], surname = (string)document[2], address = (string)document[3], phone = (string)document[4], CC_Info = document[5], purchases = document[6], }; luser.Add(user); } } } //EMBBEDED Console.WriteLine("VISU KLIENTU KREDITINIU INFORMACIJA "); foreach (var user in luser) { BsonValue dimVal = user.CC_Info; List <CC> d = BsonSerializer.Deserialize <List <CC> >(dimVal.ToJson()); Console.WriteLine("Klientas: " + user.name); foreach (var b in d) { Console.WriteLine("Card Number: " + b.Card_Number); Console.WriteLine("CVV: " + b.CVV); Console.WriteLine("Expiration Date: " + b.Expires + '\n'); } } //AGGREGATE Console.WriteLine("BENDRA VISU KLIENTU PIRKINIU SUMA "); double sum = 0; foreach (var user in luser) { BsonValue dimVal = user.purchases; List <purchases> d = BsonSerializer.Deserialize <List <purchases> >(dimVal.ToJson()); foreach (var b in d) { sum += b.total_price; } } Console.WriteLine("SUMA: " + sum); var coll = db.GetCollection <User>("Users"); string map = @"function(){ for (var i = 0; i< this.purchases.length; i++){ var value = this.purchases[i].total_price; emit(this.purchases[i].product, value); } }"; string reduce = @"function(product, total_price){ return Array.sum(total_price); } "; var options = new MapReduceOptions <User, BsonDocument> { OutputOptions = MapReduceOutputOptions.Inline }; var results = coll.MapReduce(map, reduce, options).ToList(); var rr = results.ToJson(); foreach (var r in results) { Console.WriteLine(r); } }
//public async Task<IList<AlertVehicles>> Get(DateTime? fromDate = null, DateTime? toDate = null, string cameraName = null, string sourceLocation = null, string licensePlateNumber = null) //{ // FilterDefinition<AlertVehicles> filter = Builders<AlertVehicles>.Filter.Ne(c => c.CamName, null); ; // if (cameraName != null) // { // filter = filter & Builders<AlertVehicles>.Filter.Eq(c => c.CamName, cameraName); // } // if (sourceLocation != null) // { // filter = filter & Builders<AlertVehicles>.Filter.Eq(c => c.SourceLocation, sourceLocation); // } // if (licensePlateNumber != null) // { // filter = filter & Builders<AlertVehicles>.Filter.Eq(c => c.LicensePlateNumber, licensePlateNumber); // } // if (fromDate != null && toDate != null) // { // filter = filter & Builders<AlertVehicles>.Filter.Gte(c => c.TimeStamp, fromDate) & Builders<AlertVehicles>.Filter.Lte(x => x.TimeStamp, toDate); // } // return await _context.AlertVehicle.Find(filter).ToListAsync(); //} private List <TrafficListMain> MapReduceAlarm(DateTime?fromDate, DateTime?toDate) { //emit(1, { LPDImage: alarm.LPDImage,MappedRLVDCamera: alarm.MappedRLVDCamera,Confidence: alarm.Confidence,GeneratedReport: alarm.GeneratedReport,DayMonthYearInt: alarm.DayMonthYearInt }); string map = @" function() { var key = {CamGUID:this.CamGUID}; var value = { TrafficCount: 1, ViolationOverallCount : (this.LPDImage != null && this.MappedRLVDCamera != null ? 1 : 0), ViolationHighConfidenceCount : ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport != null) ? 1 : 0), SuccessCount: ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport == 'Y') ? 1 : 0), FailureCount: ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport == 'F') ? 1 : 0), VehicleNotFoundCount: ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport == 'V') ? 1 : 0), InvalidRegNoCount: ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport == 'I') ? 1 : 0), SevenDaysCount: ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport == 'L') ? 1 : 0), MoreOfficeCount: ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport == 'M') ? 1 : 0), ImgNotFoundCount: ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport == 'G') ? 1 : 0), ExclusiveCount: ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport == 'E') ? 1 : 0), NullCountHighConfidence: ((this.Confidence == '1.0' && this.LPDImage != null && this.MappedRLVDCamera != null && this.GeneratedReport == null) ? 1 : 0) }; emit( key, value ); };"; //var result = { ViolationOverallCount:0, ViolationHighConfidenceCount: 0,SuccessCount: 0,FailureCount: 0,VehicleNotFoundCount: 0, InvalidRegNoCount: 0, SevenDaysCount: 0,MoreOfficeCount: 0, ImgNotFoundCount: 0,ExclusiveCount: 0,NullCountHighConfidence: 0,NullCountLowConfidence: 0}; string reduce = @" function(key, values) { var reducedObject = { TrafficCount:0, ViolationOverallCount:0, ViolationHighConfidenceCount:0, SuccessCount:0, FailureCount:0, VehicleNotFoundCount:0, InvalidRegNoCount:0, SevenDaysCount:0, MoreOfficeCount:0, ImgNotFoundCount:0, ExclusiveCount:0, NullCountHighConfidence:0 }; values.forEach( function(value) { reducedObject.TrafficCount += value.TrafficCount ; reducedObject.ViolationOverallCount += value.ViolationOverallCount; reducedObject.ViolationHighConfidenceCount+=value.ViolationHighConfidenceCount; reducedObject.SuccessCount+=value.SuccessCount; reducedObject.FailureCount+=value.FailureCount; reducedObject.VehicleNotFoundCount+=value.VehicleNotFoundCount; reducedObject.InvalidRegNoCount+=value.InvalidRegNoCount; reducedObject.SevenDaysCount+=value.SevenDaysCount; reducedObject.MoreOfficeCount+=value.MoreOfficeCount; reducedObject.ImgNotFoundCount+=value.ImgNotFoundCount; reducedObject.ExclusiveCount+=value.ExclusiveCount; reducedObject.NullCountHighConfidence+=value.NullCountHighConfidence; } ); return reducedObject; }"; var options = new MapReduceOptions <Alarm, TrafficListMain> { OutputOptions = MapReduceOutputOptions.Inline, Filter = "{ DayMonthYearHrMin: {'$gte': " + String.Format("{0:yyyyMMddHHmm}", fromDate) + ", '$lte': " + String.Format("{0:yyyyMMddHHmm}", toDate) + "}, DuplicateAlarm : NumberInt(1), AlarmName:{$regex: \".*LICENSE_PLATE.*\"} }" //Filter= "{ DayMonthYearInt: {'$gte': 20180813, '$lte':20180813}, DuplicateAlarm : NumberInt(1), AlarmName:{$regex: \".*LICENSE_PLATE.*\"} }" //Filter = "{ DayMonthYearInt:20180813,DuplicateAlarm : NumberInt(1), Hour: { \"$gte\":9, \"$lt\":10}, LPDImage: { \"$ne\": null}, AlarmName: {$regex: \".*LICENSE_PLATE.*\"}" }; var alarmCollection = _context.AlarmCollection; var results = alarmCollection.MapReduce(map, reduce, options).ToList <TrafficListMain>(); return(results); }
public Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <AnimatedImage, TResult> options = null, CancellationToken cancellationToken = default) { throw new NotImplementedException(); }
public IAsyncCursor <TResult> MapReduce <TResult>(IClientSessionHandle session, BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <AnimatedImage, TResult> options = null, CancellationToken cancellationToken = default) { throw new NotImplementedException(); }
public IAsyncCursor <TResult> MapReduce <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <Vehicle, TResult> options = null, CancellationToken cancellationToken = default) { throw new NotImplementedException(); }
public static double[] ExponentialMR(string code, DateTime targetDate, int period = 14, int numberOfData = 30) { if (period <= 0) { throw new IndicatorException("Period must be positive."); } int dataCount = IndicatorService.DataCount(code, targetDate); if (dataCount < numberOfData) { numberOfData = dataCount; } double alpha = 2.0 / (1 + period); double beta = 1 - alpha; double minimumCoefficient = 0.001; BsonJavaScript mapper = new BsonJavaScript(@" function() { if (!equalityFunction(this.Tarih, limitDate) && this.Tarih < limitDate) return; else if (!equalityFunction(this.Tarih, lastDate) && this.Tarih > lastDate) return; var dateIndex = binarySearch(dates, this.Tarih); if (dateIndex == -1) return; var epoch = Math.log(minimumCoefficient / (alpha * this.Kapanis)) / Math.log(beta); var value; if (dateIndex == numberOfData-1) { value = this.Kapanis; for (var i = dateIndex, j = 0; j < epoch && i >= 0; i--, j++) { emit(dates[i], value); value = value * beta; } } else { value = this.Kapanis * alpha; for (var i = dateIndex, j = 0; j < epoch && i >= 0; i--, j++) { emit(dates[i], value); value = value * beta; } } } "); BsonJavaScript reducer = new BsonJavaScript(@" function(key,values){ return Array.sum(values); } "); var filter = new BsonDocument { { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) } }; var sort = new BsonDocument("Tarih", -1); var mongoService = MongoDBService.GetService(); var projection = new BsonDocument { { "_id", 0 }, { "Tarih", 1 } }; var lastDate = MongoDBService.GetService().FindOneSortProject(filter, sort, projection).GetElement(0).Value.ToLocalTime(); var limitDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, numberOfData - 1).GetElement(0).Value.ToLocalTime(); var dates = IndicatorService.GetData(code, targetDate, "Tarih", numberOfData).Select(p => p.GetElement(0).Value.ToLocalTime()).ToArray(); var scope = new BsonDocument { { "numberOfData", numberOfData }, { "minimumCoefficient", minimumCoefficient }, { "alpha", alpha }, { "beta", beta }, { "lastDate", lastDate }, { "limitDate", limitDate }, { "dates", new BsonArray(dates) }, { "binarySearch", MapReduceHelpers.BinarySearch }, { "equalityFunction", MapReduceHelpers.IsDatesEqual } }; MapReduceOptions <BsonDocument, BsonDocument> options = new MapReduceOptions <BsonDocument, BsonDocument> { Filter = filter, Sort = sort, Scope = scope, OutputOptions = MapReduceOutputOptions.Replace("emaOut", "financialData", false) }; double[] values = MongoDBService.GetService().MapReduceMany(mapper, reducer, options).Select(p => p.GetElement("value").Value.ToDouble()).ToArray(); Array.Reverse(values); return(values); }
/// <inheritdoc/> public Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(IClientSessionHandle session, BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <T, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken)) { throw new System.NotImplementedException(); }
public Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TModel, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken)) { return(_collection.MapReduceAsync(map, reduce, options, cancellationToken)); }
private static void Mr() { var map = @" function() { var movie = this; emit(movie.Category, { count: 1, totalMinutes: movie.Minutes }); }"; var reduce = @" function(key, values) { var result = {count: 0, totalMinutes: 0 }; values.forEach(function(value){ result.count += value.count; result.totalMinutes += value.totalMinutes; }); return result; }"; var finalize = @" function(key, value){ value.average = value.totalMinutes / value.count; return value; }"; var client = new MongoClient("mongodb://*****:*****@ds041188.mongolab.com:41188/testreduce1"); var database = client.GetDatabase("testreduce1"); //var m = new MapReduceOptions<> var collection = database.GetCollection<Movie>("movies"); //shoule DAO be a BsonDocument? var options = new MapReduceOptions<Movie, Dao>(); //new MapReduceOptionsBuilder(); options.JavaScriptMode = true; options.OutputOptions.ToJson(); //options. options.OutputOptions.ToJson(new JsonWriterSettings()); //options. //options.SetFinalize(finalize); //options.SetOutput(MapReduceOutput.Inline); var results = collection.MapReduceAsync(map, reduce, options); //results.Start(); results.Wait(); var x = results.Result; //results.Wait(); //results. //options //var results = collection.MapReduce(map, reduce, options); //foreach (var result in results.GetResults()) //{ // Console.WriteLine(result.ToJson()); //} }
public void TypedMapReduceOptionSetsCollectionName() { var options = new MapReduceOptions<Product>(); Assert.Equal("Product", options.CollectionName); }
public virtual Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(IClientSessionHandle session, BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TVal, TResult> options = null, CancellationToken cancellationToken = new CancellationToken()) => throw new NotImplementedException();
public MapReduceResult MapReduce <T>(IMongoQuery query, string map, string reduce, string finalize) { var _options = MapReduceOptions.SetFinalize(finalize).SetOutput(MapReduceOutput.Inline); return(MapReduce <T>(query, map, reduce, _options)); }
public Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(IClientSessionHandle session, BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <T, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken)) { return(collection.MapReduceAsync(session, map, reduce, options, cancellationToken)); }
public MapReduceResult MapReduce <T>(IMongoQuery query, string map, string reduce) { return(MapReduce <T>(query, map, reduce, MapReduceOptions.SetOutput(MapReduceOutput.Inline))); }
private static void Mr() { var map = @" function() { var movie = this; emit(movie.Category, { count: 1, totalMinutes: movie.Minutes }); }"; var reduce = @" function(key, values) { var result = {count: 0, totalMinutes: 0 }; values.forEach(function(value){ result.count += value.count; result.totalMinutes += value.totalMinutes; }); return result; }"; var finalize = @" function(key, value){ value.average = value.totalMinutes / value.count; return value; }"; var client = new MongoClient("mongodb://*****:*****@ds041188.mongolab.com:41188/testreduce1"); var database = client.GetDatabase("testreduce1"); //var m = new MapReduceOptions<> var collection = database.GetCollection <Movie>("movies"); //shoule DAO be a BsonDocument? var options = new MapReduceOptions <Movie, Dao>(); //new MapReduceOptionsBuilder(); options.JavaScriptMode = true; options.OutputOptions.ToJson(); //options. options.OutputOptions.ToJson(new JsonWriterSettings()); //options. //options.SetFinalize(finalize); //options.SetOutput(MapReduceOutput.Inline); var results = collection.MapReduceAsync(map, reduce, options); //results.Start(); results.Wait(); var x = results.Result; //results.Wait(); //results. //options //var results = collection.MapReduce(map, reduce, options); //foreach (var result in results.GetResults()) //{ // Console.WriteLine(result.ToJson()); //} }
private void CalculateFastK_MR(int dataCount) { if (dataCount < NumberOfData + FastKPeriod - 1) { NumberOfData = dataCount - FastKPeriod + 1; } var limit = FastKPeriod + NumberOfData - 1; var filter = new BsonDocument { { "Kod", Code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(TargetDate)) } }; var sort = new BsonDocument("Tarih", -1); var projection = new BsonDocument { { "_id", 0 }, { "Tarih", 1 } }; BsonJavaScript mapper = new BsonJavaScript(@" function() { if (!equalityFunction(this.Tarih, limitDate) && this.Tarih < limitDate) return; else if (!equalityFunction(this.Tarih, lastDate) && this.Tarih > lastDate) return; var dateIndex = binarySearch(dates, this.Tarih); if (dateIndex == -1) return; for (var i = 0; i < period && dateIndex >= 0; i++, dateIndex--) { if (dates[dateIndex] > startingDate || equalityFunction(dates[dateIndex], startingDate)) emit(dates[dateIndex], {close: this.Kapanis, low: this.Dusuk, high: this.Yuksek}); } } "); BsonJavaScript reducer = new BsonJavaScript(@" function(key, values) { var result = {close: values[0].close, low: values[0].low, high: values[0].high}; for (var i = 1; i < values.length; i++) { result.low = values[i].low < result.low ? values[i].low : result.low; result.high = values[i].high > result.high ? values[i].high : result.high; } return result; } "); BsonJavaScript finalizer = new BsonJavaScript(@" function(key, reducedValue) { reducedValue = 100 * (reducedValue.close - reducedValue.low) / (reducedValue.high - reducedValue.low); return reducedValue; } "); var startingDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, NumberOfData - 1).GetElement(0).Value.ToLocalTime(); var lastDate = MongoDBService.GetService().FindOneSortProject(filter, sort, projection).GetElement(0).Value.ToLocalTime(); var limitDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, limit - 1).GetElement(0).Value.ToLocalTime(); var dates = IndicatorService.GetData(Code, TargetDate, "Tarih", limit).Select(p => p.GetElement(0).Value.ToLocalTime()).ToArray(); var scope = new BsonDocument { { "startingDate", startingDate }, { "lastDate", lastDate }, { "limitDate", limitDate }, { "period", FastKPeriod }, { "numberOfData", NumberOfData }, { "binarySearch", MapReduceHelpers.BinarySearch }, { "equalityFunction", MapReduceHelpers.IsDatesEqual }, { "dates", new BsonArray(dates) } }; var options = new MapReduceOptions <BsonDocument, BsonDocument>() { Filter = filter, Sort = sort, Scope = scope, OutputOptions = MapReduceOutputOptions.Replace("stochasticsOut", "financialData", false), Finalize = finalizer }; FastK = new double[NumberOfData]; var resultSet = MongoDBService.GetService().MapReduceMany(mapper, reducer, options); for (int i = 0, j = resultSet.Count - 1; i < NumberOfData; i++, j--) { FastK[i] = resultSet.ElementAt(j).GetElement(1).Value.ToDouble(); } }
/* ------------------------------------------- */ /* MapReduce implementations of the indicators */ /* ------------------------------------------- */ public static double[] SimpleMR(string code, DateTime targetDate, int period = 14, int numberOfData = 30) { if (period <= 0) { throw new IndicatorException("Periyot pozitif sayı olmalıdır."); } else if (numberOfData <= 0) { throw new IndicatorException("Gösterilecek veri sayısı pozitif sayı olmalıdır."); } int dataCount = IndicatorService.DataCount(code, targetDate); if (dataCount < period) { throw new IndicatorException(IndicatorException.DATA_NOT_ENOUGH_MESSAGE); } if (dataCount < numberOfData + period - 1) { numberOfData = dataCount - period + 1; } int limit = numberOfData + period - 1; var filter = new BsonDocument { { "Kod", code }, { "Tarih", new BsonDocument("$lte", new BsonDateTime(targetDate)) } }; var sort = new BsonDocument("Tarih", -1); var projection = new BsonDocument { { "_id", 0 }, { "Tarih", 1 } }; var startingDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, numberOfData - 1).GetElement(0).Value.ToLocalTime(); var lastDate = MongoDBService.GetService().FindOneSortProject(filter, sort, projection).GetElement(0).Value.ToLocalTime(); var limitDate = MongoDBService.GetService().FindOneSortProjectSkip(filter, sort, projection, limit - 1).GetElement(0).Value.ToLocalTime(); var dates = IndicatorService.GetData(code, targetDate, "Tarih", limit).Select(p => p.GetElement(0).Value.ToLocalTime()).ToArray(); MapReduceOptions <BsonDocument, BsonDocument> options = new MapReduceOptions <BsonDocument, BsonDocument> { Filter = filter, Sort = sort, Scope = new BsonDocument { { "startingDate", startingDate }, { "lastDate", lastDate }, { "limitDate", limitDate }, { "period", period }, { "numberOfData", numberOfData }, { "binarySearch", MapReduceHelpers.BinarySearch }, { "equalityFunction", MapReduceHelpers.IsDatesEqual }, { "dates", new BsonArray(dates) } }, OutputOptions = MapReduceOutputOptions.Replace("smaOut", "financialData", false) }; BsonJavaScript mapper = new BsonJavaScript(@" function() { if (!equalityFunction(this.Tarih, limitDate) && this.Tarih < limitDate) return; else if (!equalityFunction(this.Tarih, lastDate) && this.Tarih > lastDate) return; var dateIndex; dateIndex = binarySearch(dates, this.Tarih); if (dateIndex == -1) return; for (var i = 0; i < period && dateIndex >= 0; i++, dateIndex--) { if (dates[dateIndex] > startingDate || equalityFunction(dates[dateIndex], startingDate)) emit(dates[dateIndex], this.Kapanis); } } "); BsonJavaScript reducer = new BsonJavaScript(@" function(key, values) { return Array.sum(values); } "); BsonJavaScript finalizer = new BsonJavaScript(@" function(key, reducedValue) { return reducedValue / period; } "); options.Finalize = finalizer; double[] avg = new double[numberOfData]; List <BsonDocument> resultSet = MongoDBService.GetService().MapReduceMany(mapper, reducer, options); for (int i = 0, j = numberOfData - 1; i < numberOfData; i++, j--) { avg[i] = resultSet.ElementAt(j).GetElement(1).Value.ToDouble(); } return(avg); }
public override IAsyncCursor <TResult> MapReduce <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TDocument, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); }
public async Task<List<BsonDocument>> MapReduceAsync(string databaseName, string collection, BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions<BsonDocument, BsonDocument> options, CancellationToken token) { var db = client.GetDatabase(databaseName); var mongoCollection = db.GetCollection<BsonDocument>(collection); var result = await mongoCollection.MapReduceAsync<BsonDocument>(map, reduce, options, token); return await result.ToListAsync(); }
public virtual IEnumerable <TResult> MapReduce <TEntity, TResult>(MapReduceOptionsProxy <TEntity, TResult> options) where TEntity : CollectionEntityBase, new() { if (string.IsNullOrEmpty(options.Map)) { throw new Exception("map is must"); } if (string.IsNullOrEmpty(options.Reduce)) { throw new Exception("reduce is must"); } var o = new MapReduceOptions <TEntity, TResult>(); if (options.OutputEnum.Equals(MapReduceOutputOptionsEnum.Inline)) { o.OutputOptions = MapReduceOutputOptions.Inline; } else { if (string.IsNullOrEmpty(options.DatabaseName) || string.IsNullOrEmpty(options.CollectionName)) { throw new Exception("DatabaseName and CollectionName is must"); } if (options.OutputEnum.Equals(MapReduceOutputOptionsEnum.Merge)) { o.OutputOptions = MapReduceOutputOptions.Reduce(options.CollectionName, options.DatabaseName); } else if (options.OutputEnum.Equals(MapReduceOutputOptionsEnum.Reduce)) { o.OutputOptions = MapReduceOutputOptions.Reduce(options.CollectionName, options.DatabaseName); } else if (options.OutputEnum.Equals(MapReduceOutputOptionsEnum.Replace)) { o.OutputOptions = MapReduceOutputOptions.Replace(options.CollectionName, options.DatabaseName); } } o.BypassDocumentValidation = options.BypassDocumentValidation; if (!string.IsNullOrEmpty(options.Filter)) { o.Filter = options.Filter; } if (!string.IsNullOrEmpty(options.Finalize)) { o.Finalize = options.Finalize; } o.JavaScriptMode = options.JavaScriptMode; o.Limit = options.Limit; o.MaxTime = options.MaxTime; if (!string.IsNullOrEmpty(options.Sort)) { o.Sort = options.Sort; } o.Verbose = options.Verbose; var r = this.CurrentCollection <TEntity>().MapReduce <TResult>(new MongoDB.Bson.BsonJavaScript(options.Map), new MongoDB.Bson.BsonJavaScript(options.Reduce), o); if (options.OutputEnum.Equals(MapReduceOutputOptionsEnum.Inline)) { return(r.Current); } else { return(null); } }
public async Task <List <TProjection> > MapReduceAsync <TProjection>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TDocument, TProjection> options, CancellationToken cancellationToken = default(CancellationToken)) { IAsyncCursor <TProjection> mapReduce; Task <IAsyncCursor <TProjection> > task; if (IsInTransaction()) { task = Collection.MapReduceAsync(this.clientSessionHandle, map, reduce, options, cancellationToken); } else { task = Collection.MapReduceAsync(map, reduce, options, cancellationToken); } #if NETFULL mapReduce = await task.ConfigureAwait(false); #else mapReduce = await task; #endif #if NETFULL return(await mapReduce.ToListAsync(cancellationToken).ConfigureAwait(false)); #else return(await mapReduce.ToListAsync(cancellationToken)); #endif }
public Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TDocument, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken)) { return(Task.FromResult(MapReduce(map, reduce, options, cancellationToken))); }