如何在C#中使用MapReduce加入2个集合?为了加入这两个数据集,我尝试通过执行以下操作将此示例转换为C#:如果你们中的任何人可以建议适当的代码修改以获得与示例相同的结果,我将非常感激。与本例产生相同结果的解决方案如下:以上是C#学习教程:Howtojoin2collectionsusingMapReduceinC#?如果分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注——classProgram{staticvoidMain(string[]args){varconnectionString="mongodb://localhost";varclient=newMongoClient(connectionString);varserver=client.GetServer();vardatabase=server.GetDatabase("mr_demo");varcLifeExpectancy=database.GetCollection("life_expectancy");varcEconomicAssistance=database.GetCollection("us_economic_assistance");varoptions=newMapReduceOptionsBuilder();options.SetOutput(MapReduceOutput.Inline);options.SetOutput(MapReduceOutput.Reduce("result"));varresult=cLifeExpectancy.MapReduce(life_expect_map,r,options);结果=cEconomicAssistance.MapReduce(us_econ_map,r,options);foreach(varrecordinresult.GetResults()){Console.WriteLine(record);}}privatestaticstringlife_expect_map=@"function(){//简单地发出年龄和0作为金额。//金额将来自另一个集合。emit(this.country,{life_expectancy:this.age,dollars:0});}";privatestaticstringus_econ_map=@"function(){//数据集包含可追溯到1946年的拨款金额。我//只对2009年的拨款感兴趣。if(this.FY2009!==undefined&&this.FY2009!==null){emit(this.country_name,{dollars:this.FY2009,life_expectancy:0});}}";privatestaticstringr=@"function(key,values){varresult={dollars:0,life_expectancy:0};values.forEach(function(value){//总结2009年所有拨款给这个国家的所有资金(关键)result.dollars+=(value.dollars!==null)?value.dollars:0;//只设置一次预期寿命if(result.life_expectancy===0&&value.life_expectancy!==null){result.life_expectancy=value.life_expectancy;}});返回结果;}";}本文采集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
