首页 > 其他 > 详细

C#版的MapReduce

时间:2014-06-08 10:39:53      阅读:459      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!