public MapReduceResult executeMapReduce(string table, IMongoQuery query, string map_js, string reduce_js) { try { var cb = check_table(table); MapReduceArgs args = new MapReduceArgs(); args.MapFunction = new BsonJavaScript(map_js); args.ReduceFunction = new BsonJavaScript(reduce_js); args.Query = query; //args.JsMode = true; //BsonJavaScript map = BsonJavaScript.Create(map_js); //BsonJavaScript reduce = BsonJavaScript.Create(reduce_js); //var ret = cb.MapReduce(query, map, reduce); var ret = cb.MapReduce(args); if (ret.Ok) { return(ret); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(null); }
public MapReduceResult executeMapReduce(string table, IMongoQuery query, string map_js, string reduce_js, string outTableName = "") { try { var cb = check_table(table); MapReduceArgs args = new MapReduceArgs(); args.MapFunction = new BsonJavaScript(map_js); args.ReduceFunction = new BsonJavaScript(reduce_js); args.Query = query; if (!string.IsNullOrEmpty(outTableName)) { args.OutputMode = MapReduceOutputMode.Replace; args.OutputCollectionName = outTableName; } var ret = cb.MapReduce(args); if (ret.Ok) { return(ret); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(null); }
public List <StatInfo> getStat(String testname) { List <StatInfo> list = new List <StatInfo>();; MongoCollection collection; var name = getTestAddr(testname); System.Diagnostics.Debug.WriteLine(name); if ((DbBasic.rwHt[name] as int?) == DbBasic.STAGE_READ)//处于读阶段时,尝试直接读取数据库中的统计结果 { System.Diagnostics.Debug.WriteLine("尝试从数据库获取试卷 " + testname + " 的统计结果"); collection = mcon.GetCollection("StatResult:" + name); if (collection != null) { try { list = collection.FindAllAs <StatInfo>().ToList(); } catch (Exception e) { Basic.trace(e.StackTrace); } if (list.Count() != 0) { return(list); } } } //处于写阶段时、没有统计结果时,使用mapreduce进行统计 System.Diagnostics.Debug.WriteLine("数据库进行试卷 " + testname + " 的分数统计"); collection = mcon.GetCollection("Score:" + name); String map = "function(){emit(this.classs,this.score)}"; String map2 = "function(){emit('总体',this.score)}"; String reduce = "function(key , values){var result = renewStat2(key , values," + name.ToJson() + ");return result;}"; MapReduceArgs args = new MapReduceArgs(); args.MapFunction = BsonJavaScript.Create(map); args.ReduceFunction = BsonJavaScript.Create(reduce); args.OutputCollectionName = ("StatResult:" + name); args.OutputDatabaseName = mcon.Name; // args.OutputMode = MapReduceOutputMode.Replace; // args.OutputMode = MapReduceOutputMode.Inline; args.OutputMode = MapReduceOutputMode.Merge; var md = collection.MapReduce(args); var ms = md.GetResultsAs <StatInfo>(); list = ms.ToList(); args.MapFunction = BsonJavaScript.Create(map2); md = collection.MapReduce(args); ms = md.GetResultsAs <StatInfo>().ToList(); list.Concat(ms); // foreach (var i in mresult.GetResultsAs<StatInfo>()) list.Add(i.convertToJson()); DbBasic.rwHt[name] = DbBasic.STAGE_READ; return(list); }
public IEnumerable <BsonDocument> MapReduce <T>(string map, string reduce) { var arg = new MapReduceArgs(); arg.MapFunction = map; arg.ReduceFunction = reduce; //var result = CurrentCollection<T>().MapReduce(map, reduce).GetResults(); return(CurrentCollection <T>().MapReduce(arg).GetResults());//.GetResultsAs<TResult>().ToList(); }
/// <summary> /// 运行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdRun_Click(object sender, EventArgs e) { var map = new BsonJavaScript(ctlMapFunction.Context); var reduce = new BsonJavaScript(ctlReduceFunction.Context); //TODO:这里可能会超时,失去响应 //需要设置SocketTimeOut var args = new MapReduceArgs() { MapFunction = map, ReduceFunction = reduce }; if (!string.IsNullOrEmpty(ctlFinalizeFunction.Context)) { args.FinalizeFunction = new BsonJavaScript(ctlFinalizeFunction.Context); } args.OutputMode = (MapReduceOutputMode)cmbOutputMode.SelectedIndex; if (!string.IsNullOrEmpty(txtOutputCollectionName.Text)) { args.OutputCollectionName = txtOutputCollectionName.Text; } if (NumLimit.Value != 0) { args.Limit = (long)NumLimit.Value; } args.JsMode = chkjsMode.Checked; args.Verbose = chkverbose.Checked; args.BypassDocumentValidation = chkbypassDocumentValidation.Checked; if (QueryDoc != null) { args.Query = new QueryDocument(QueryDoc); } if (mCollation != null) { args.Collation = mCollation; } try { var mMapReduceResult = RuntimeMongoDbContext.GetCurrentCollection().MapReduce(args); var frm = new frmDataView() { ShowDocument = mMapReduceResult.Response, Title = "MapReduce Result" }; UIAssistant.OpenModalForm(frm, true, true); } catch (Exception ex) { Utility.ExceptionDeal(ex); } }
private MapReduceResult MapReduce(string collectionName, string criteria, string mapFunction, string reduceFunction, string mapFinalize, string sort, int limit) { MongoCollection <BsonDocument> collection = Database.GetCollection <BsonDocument>(collectionName); MapReduceArgs args = new MapReduceArgs(); MapReduceResult result = null; if (!string.IsNullOrEmpty(mapFunction)) { args.MapFunction = mapFunction; } if (!string.IsNullOrEmpty(reduceFunction)) { args.ReduceFunction = reduceFunction; } if (!string.IsNullOrEmpty(mapFinalize)) { args.FinalizeFunction = mapFinalize; } if (!string.IsNullOrEmpty(criteria)) { args.Query = Query.Create(new QueryDocument(ParseQuery(criteria))); } if (!string.IsNullOrEmpty(sort)) { ParseQuery(sort).ToList().ForEach(itemtoSort => { if (itemtoSort.Value > 0) { args.SortBy = SortBy.Ascending(itemtoSort.Name); } else { args.SortBy = SortBy.Descending(itemtoSort.Name); } }); } args.Limit = limit; if (!string.IsNullOrEmpty(mapFunction) && !string.IsNullOrEmpty(reduceFunction)) { result = collection.MapReduce(args); } return(result); }
public override MapReduceResult MapReduce(MapReduceArgs args) { var sw = new Stopwatch(); sw.Start(); var result = base.MapReduce(args); sw.Stop(); string commandString = string.Format("db.{0}.mapReduce(<map function>, <reduce function>, options)", Name); ProfilerUtils.AddMongoTiming(commandString, sw.ElapsedMilliseconds, ExecuteType.Read); return(result); }
/// <summary> /// 运行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdRun_Click(object sender, EventArgs e) { var map = new BsonJavaScript(ctlMapFunction.Context); var reduce = new BsonJavaScript(ctlReduceFunction.Context); //TODO:这里可能会超时,失去响应 //需要设置SocketTimeOut MapReduceArgs args = new MapReduceArgs(); args.MapFunction = map; args.ReduceFunction = reduce; MapReduceResult mMapReduceResult = SystemManager.GetCurrentCollection().MapReduce(args); MongoDbHelper.FillDataToTreeView("MapReduce Result", trvResult, mMapReduceResult.Response); trvResult.DatatreeView.BeginUpdate(); trvResult.DatatreeView.ExpandAll(); trvResult.DatatreeView.EndUpdate(); }
public MapReduceResult executeMapReduce(string table, IMongoQuery query, string map_js, string reduce_js) { try { var cb = mMongodbClient.GetCollection(table); MapReduceArgs args = new MapReduceArgs(); args.MapFunction = new BsonJavaScript(map_js); args.ReduceFunction = new BsonJavaScript(reduce_js); args.Query = query; var ret = cb.MapReduce(args); if (ret.Ok) { return(ret); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(null); }
/// <summary> /// 运行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdRun_Click(object sender, EventArgs e) { var map = new BsonJavaScript(ctlMapFunction.Context); var reduce = new BsonJavaScript(ctlReduceFunction.Context); //TODO:这里可能会超时,失去响应 //需要设置SocketTimeOut var args = new MapReduceArgs(); args.MapFunction = map; args.ReduceFunction = reduce; try { var mMapReduceResult = RuntimeMongoDbContext.GetCurrentCollection().MapReduce(args); UiHelper.FillDataToTreeView("MapReduce Result", trvResult, mMapReduceResult.Response); trvResult.DatatreeView.BeginUpdate(); trvResult.DatatreeView.ExpandAll(); trvResult.DatatreeView.EndUpdate(); } catch (Exception ex) { Utility.ExceptionDeal(ex); } }
public virtual IEnumerable <TResult> MapRedurce <TResult>(MapReduceArgs args) { return(this.context.GetCollection <TEntity>().MapReduce(args).GetResultsAs <TResult>()); }
public virtual MapReduceResult MapRedurce(MapReduceArgs args) { return(this.context.GetCollection <TEntity>().MapReduce(args)); }
protected override void BeginProcessing() { var mc = TargetCollection.Collection as MongoCollection; if (mc == null) { ThrowNotImplementedForFiles("MapReduce"); } var args = new MapReduceArgs(); args.JsMode = JSMode; args.MapFunction = new BsonJavaScript(Function[0]); args.ReduceFunction = new BsonJavaScript(Function[1]); if (Function.Length == 3) { args.FinalizeFunction = new BsonJavaScript(Function[2]); } if (_Query != null) { args.Query = _Query; } if (_SortBy != null) { args.SortBy = _SortBy; } if (First > 0) { args.Limit = First; } if (Scope != null) { args.Scope = new ScopeDocument(Scope); } if (!string.IsNullOrEmpty(OutCollection) && OutMode == MapReduceOutputMode.Inline) { OutMode = MapReduceOutputMode.Replace; } // output args.OutputMode = OutMode; args.OutputDatabaseName = OutDatabase; args.OutputCollectionName = OutCollection; var result = mc.MapReduce(args); if (ResultVariable != null) { SessionState.PSVariable.Set(ResultVariable, result); } if (OutMode != MapReduceOutputMode.Inline) { return; } var documentAs = _ParameterAs ?? new ParameterAs(null); //_131018_160000 foreach (var it in result.GetInlineResultsAs(documentAs.Type)) { WriteObject(it); } }
public Stream GetContentFromMongoDB(GenericStorageExtensionServiceRequest <SearchRequest> request) { string resultJSON = string.Empty; GenericStorageExtensionLogger.WriteLog(ELogLevel.INFO, "Entering into GetContentFromMongoDB"); try { if (request != null && request.ServicePayload != null) { string MongoDBIndexConfigPath = Utility.GetConfigurationValue("IndexServiceConfig"); propConfiguration = ConfigurationManager.GetInstance().GetConfiguration(MongoDBIndexConfigPath) as IPropertyConfiguration; containerLock = new object(); string result = string.Empty; var connectionString = propConfiguration.GetString(GenericStorageExtensionServicesConstants.Search_URL); var client = new MongoClient(connectionString); var server = client.GetServer(); var database = server.GetDatabase(propConfiguration.GetString(GenericStorageExtensionServicesConstants.dbName)); var collection = database.GetCollection <MongoDBModelSearch>(propConfiguration.GetString(GenericStorageExtensionServicesConstants.tableName)); var andList = new List <IMongoQuery>(); foreach (DictionaryEntry entry in request.ServicePayload.Filters) { GenericStorageExtensionLogger.WriteLog(ELogLevel.INFO, "Reading request.ServicePayload.Filters"); switch (request.ServicePayload.QueryType.ToUpper()) { case "AND": andList.Add(Query.EQ(entry.Key.ToString(), entry.Value.ToString())); break; case "OR": andList.Add(Query.Or(Query.EQ(entry.Key.ToString(), entry.Value.ToString()))); break; default: andList.Add(Query.Not(Query.EQ(entry.Key.ToString(), entry.Value.ToString()))); break; } } var query = Query.And(andList); GenericStorageExtensionLogger.WriteLog(ELogLevel.INFO, "Query generated"); //Map/Reduce var map = "function() {" + " for (var key in this) {" + " emit(key, { count : 1 });" + " }" + "}"; var reduce = "function(key, emits) {" + " total = 0;" + " for (var i in emits) {" + " total += emits[i].count;" + " }" + " return { count : total };" + "}"; MapReduceArgs n = new MapReduceArgs(); n.MapFunction = map; n.ReduceFunction = reduce; var mr = collection.MapReduce(n); foreach (var document in mr.GetResults()) { document.ToJson(); } GenericStorageExtensionLogger.WriteLog(ELogLevel.INFO, "Calling collection.FindOne(query)"); // var entity = collection.Find(query).ToListAsync(); var result1 = collection.FindAs <MongoDBModelSearch>(query); resultJSON = result1.ToJson(); GenericStorageExtensionLogger.WriteLog(ELogLevel.INFO, "OUTPUT: " + resultJSON); } } catch (Exception ex) { GenericStorageExtensionLogger.WriteLog(ELogLevel.ERROR, "ERROR: " + ex.Message + ex.StackTrace); } return(new MemoryStream(Encoding.UTF8.GetBytes(resultJSON))); }