基本信息
源码名称:C# mongodb map reduce 计算 实例源码下载
源码大小:1.47M
文件格式:.zip
开发语言:C#
更新时间:2014-12-24
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
mongodb map-reduce计算 源码
主要参考:
http://docs.mongodb.org/manual/tutorial/map-reduce-examples/
http://odetocode.com/blogs/scott/archive/2012/03/19/a-simple-mapreduce-with-mongodb-and-c.aspx
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; namespace MongoDBWinDemo { public partial class MapReduceForm2 : Form { public MapReduceForm2() { InitializeComponent(); } public class Movie { public string Title { get; set; } public string Category { get; set; } public int Minutes { get; set; } } public MongoCollection<T> GetCollection<T>() { //var connectionString = "mongodb://localhost"; //var client = new MongoClient(connectionString); //var server = client.GetServer(); //var database = server.GetDatabase("test"); //var collection = database.GetCollection<Entity>("entities"); var connectionString = "mongodb://localhost"; var client = new MongoClient(connectionString); var server = client.GetServer(); var database = server.GetDatabase("test"); var collection = database.GetCollection<T>("movies"); return collection; } private void AddMovies(MongoCollection<Movie> collection) { var movies = new List<Movie> { new Movie { Title = "The Perfect Developer", Category = "SciFi", Minutes = 118 }, new Movie { Title = "Lost In Frankfurt am Main", Category = "Horror", Minutes = 122 }, new Movie { Title = "The Infinite Standup", Category = "Horror", Minutes = 341 } }; collection.InsertBatch(movies); } private void btnCal_Click(object sender, EventArgs e) { string map = @" function() { var movie = this; emit(movie.Category, { count: 1, totalMinutes: movie.Minutes }); }"; string reduce = @" function(key, values) { var result = {count: 0, totalMinutes: 0 }; values.forEach(function(value){ result.count = value.count; result.totalMinutes = value.totalMinutes; }); return result; }"; string finalize = @" function(key, value){ value.average = value.totalMinutes / value.count; return value; }"; //var collection = db.GetCollection("movies"); var collection = this.GetCollection<Movie>(); AddMovies(collection); var options = new MapReduceOptionsBuilder(); options.SetFinalize(finalize); options.SetOutput(MapReduceOutput.Inline); var results = collection.MapReduce(map, reduce, options); foreach (var result in results.GetResults()) { //Console.WriteLine(result.ToJson()); MessageBox.Show(result.ToJson()); } } } }