码迷,mamicode.com
首页 > 其他好文 > 详细

C#版的MapReduce

时间:2014-06-08 10:39:41      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:mapreduce

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mapReduce
{
    public static class helper
    {
        public static Dictionary<TKey, TResult> MapReduce<TInput, TKey, TValue, TResult>(
           this IEnumerable<TInput> list,
           //Func<TInput, IEnumerable<KeyValuePair<TKey, TValue>>> map,
                   Func<TInput, KeyValuePair<TKey, TValue>> map,
           Func<TKey, IEnumerable<TValue>, TResult> reduce)
        {
            Dictionary<TKey, List<TValue>> mapResult = new Dictionary<TKey, List<TValue>>();
            foreach (var item in list)
            {
                //foreach (var one in map(item))
                //{
                var one = map(item);
                if (Convert.ToInt32( one.Key) == 0)
                {
                    continue;
                }
                    List<TValue> mapValues;
                    if (!mapResult.TryGetValue(one.Key, out mapValues))
                    {
                        mapValues = new List<TValue>();
                        mapResult.Add(one.Key, mapValues);
                    }
                    mapValues.Add(one.Value);
                //}
            }
            var result = new Dictionary<TKey, TResult>();
            foreach (var m in mapResult)
            {
                result.Add(m.Key, reduce(m.Key, m.Value));
            }
            return result;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mapReduce
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> list = new List<Person>();
            list.Add(new Person { ID = 1, Name = "肖银龙", Age = 23 });
            list.Add(new Person { ID = 2, Name = "郭彦磊", Age = 24 });
            list.Add(new Person { ID = 3, Name = "王老师", Age = 23 });
            list.Add(new Person { ID = 4, Name = "路人甲", Age = 25 });
            list.Add(new Person { ID = 5, Name = "路人乙", Age = 20 });

            var result = list.MapReduce<Person, int, string, string>(Map,
                (key, values) => string.Join(",", values));

            foreach (var d in result)
            {
                Console.WriteLine(d.Key + ":" + d.Value);
            }

            Console.Read();
        }

        //public static IEnumerable<KeyValuePair<int, string>> Map(Person p)
        //{
        //    if (p.Age > 22)
        //        yield return new KeyValuePair<int, string>(p.Age, p.Name);
               
        //}

        public static KeyValuePair<int, string> Map(Person p)
        {
            if (p.Age > 22)
                return new KeyValuePair<int, string>(p.Age, p.Name);
            else
                return new KeyValuePair<int, string>(0,null);

        }

    }

    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

bubuko.com,布布扣

C#版的MapReduce,布布扣,bubuko.com

C#版的MapReduce

标签:mapreduce

原文地址:http://blog.csdn.net/xyl295528322/article/details/28886093

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!