gpt4 book ai didi

c# - 如何在 C# 中使用 MapReduce 加入 2 个集合?

转载 作者:可可西里 更新时间:2023-11-01 10:05:22 25 4
gpt4 key购买 nike

为了连接两个数据集,我尝试翻译 this C# 的示例如下:

如果你们中的任何人可以建议适当的代码修改以获得与示例相同的结果,我将非常感激。

最佳答案

产生与 this 相同结果的解决方案示例如下:

class Program
{
static void Main(string[] args)
{
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("mr_demo");
var cLifeExpectancy = database.GetCollection("life_expectancy");
var cEconomicAssistance = database.GetCollection("us_economic_assistance");

var options = new MapReduceOptionsBuilder();
options.SetOutput(MapReduceOutput.Inline);


options.SetOutput(MapReduceOutput.Reduce("result"));
var result = cLifeExpectancy.MapReduce(life_expect_map, r, options);
result = cEconomicAssistance.MapReduce(us_econ_map, r, options);

foreach (var record in result.GetResults())
{
Console.WriteLine(record);
}
}

private static string life_expect_map =
@"function() {
// Simply emit the age and 0 for the dollar amount.
// The dollar amount will come from the other collection.
emit(this.country, {life_expectancy: this.age, dollars: 0});
}";

private static string us_econ_map =
@"function() {
// The data set contains grant amounts going back to 1946. I
// am only interested in 2009 grants.
if (this.FY2009 !== undefined && this.FY2009 !== null) {
emit(this.country_name, {
dollars: this.FY2009,
life_expectancy: 0
});
}
}";

private static string r =
@"function(key, values) {
var result = {dollars: 0, life_expectancy: 0};

values.forEach(function(value) {
// Sum up all the money from all the 2009 grants for this
// country (key)
result.dollars += (value.dollars !== null) ? value.dollars : 0;
// Only set life expectancy once
if (result.life_expectancy === 0 &&
value.life_expectancy !== null
) {
result.life_expectancy = value.life_expectancy;
}
});

return result;
}";
}

关于c# - 如何在 C# 中使用 MapReduce 加入 2 个集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14022606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com