c#扩展出MapReduce方法

MapReduce方法主体:

 1 public static IDictionary<TKey, TResult> MapReduce<TInput, TKey, TValue, TResult>(this IEnumerable<TInput> inputList,
 2             Func<TInput, KeyValueClass<TKey, TValue>> map, Func<TKey, IEnumerable<TValue>, TResult> reduce)
 3         {
 4             object locker = new object();
 5             ConcurrentDictionary<TKey, TResult> result = new ConcurrentDictionary<TKey, TResult>();
 6             //保存map出来的结果
 7             ConcurrentDictionary<TKey, ConcurrentBag<TValue>> mapDic = new ConcurrentDictionary<TKey, ConcurrentBag<TValue>>();
 8             //并行map
 9             Parallel.ForEach(inputList, (t, p, s) =>
10             {
11                 var pair = map(t);
12                 if (pair != null)
13                 {
14                     //锁住防止并发操作list造成数据缺失
15                     lock (locker)
16                     {
17                         //将匹配出来的结果加入结果集放入字典
18                         ConcurrentBag<TValue> list = null;
19                         if (mapDic.ContainsKey(pair.Key))
20                         {
21                             list = mapDic[pair.Key];
22                         }
23                         else
24                         {
25                             list = new ConcurrentBag<TValue>();
26                             mapDic[pair.Key] = list;
27                         }
28                         list.Add(pair.Value);
29                     }
30                 }
31             });
32
33             //并行reduce
34             Parallel.ForEach(mapDic.Keys, (t, p, s) =>
35             {
36                 result[t] = reduce(t, mapDic[t]);
37             });
38             return result;
39         }

KeyValueClass定义:

 1 public class KeyValueClass<K, V>
 2     {
 3         public KeyValueClass(K key, V value)
 4         {
 5             Key = key;
 6             Value = value;
 7         }
 8
 9         public KeyValueClass()
10         {
11
12         }
13
14         public K Key { get; set; }
15
16         public V Value { get; set; }
17     }

Console测试:

 1 List<TestClass> listTestClass = new List<TestClass>();
 2             listTestClass.Add(new TestClass { a = "a", g = 1 });
 3             listTestClass.Add(new TestClass { a = "b", g = 3 });
 4             listTestClass.Add(new TestClass { a = "c", g = 4 });
 5             listTestClass.Add(new TestClass { a = "d", g = 2 });
 6             listTestClass.Add(new TestClass { a = "e", g = 1 });
 7             listTestClass.Add(new TestClass { a = "f", g = 2 });
 8             listTestClass.Add(new TestClass { a = "g", g = 5 });
 9             listTestClass.Add(new TestClass { a = "h", g = 6 });
10             IDictionary<int, string> dic = listTestClass.MapReduce(t =>
11             {
12                 if (t.g < 5)
13                 {
14                     return new KeyValueClass<int, string>(t.g, t.a);
15                 }
16                 return null;
17             }, (key, values) =>
18            {
19                return string.Join(",", values);
20            });

TestClass定义:

 1 public class TestClass
 2     {
 3         public string a { get; set; }
 4         public string b { get; set; }
 5
 6         public string d { get; set; }
 7
 8         //public DateTime f { get; set; }
 9
10         public int g { get; set; }
11
12         public List<TestClass> test { get; set; }
13
14         public Dictionary<string, string> dic { get; set; }
15     }

结果:

1:a,e

2:d,f

3:b

4:c

时间: 2024-08-25 20:28:03

c#扩展出MapReduce方法的相关文章

EF中扩展出Between操作符 (修订版)

随手记录一下,这是针对原文错误的修改. 原文:EF中扩展出Between操作符 直接使用是错误的,修改后的扩展方法: /// <summary> /// 扩展 Between 操作符 /// 使用 var query = db.People.Between(person => person.Age, 18, 21); /// </summary> /// <typeparam name="TSource"></typeparam>

MapReduce方法的理解和遇到的问题总结

昨天联系了一个用map和reduce来编写wordcount当时写出来感觉自己有点懂了,但是今天做到天气站求天气平均数的时候遇到了问题.自己摸索出来了一点歪门邪道.所谓map只管数据中一行,确定要传的key和value.而reduce则是对所有的value值进行计算,而不是只有一行的.在问题中出现类型的错误.通过将Doule换成Int类型解决. 在对数据进行可视化的时候,tomcat突然就炸了,就连普通的网页就不能跑,后来就开始打不开tomcat.后来发现是maven中导入的tomcat包,和本

JavaScript简洁继承机制实现(不使用prototype和new)

此方法并非笔者原创,笔者只是在前辈的基础上,加以总结,得出一种简洁实用的JavaScript继承方法. 传统的JavaScript继承基于prototype原型链,并且需要使用大量的new操作,代码不够简洁,可读性也不是很强,貌似还容易受到原型链污染. 笔者总结的继承方式,简洁明了,虽然不是最好的方式,但希望能给读者带来启发. 好了,废话不多说,直接看代码,注释详尽,一看就懂~~~ 1 /** 2 * Created by 杨元 on 14-11-11. 3 * 不使用prototype实现继承

关于mongodb的mapReduce

由于nodejs本身的限制,在程序中使用js进行大批量计算效率不高.而V8引擎自身对内存大小的限制(64位系统下1.4G),同样限制了数据规模. 因此,相对于从mongodb中抽出数据进行计算,在mongodb中利用聚合函数或者其他方法完成计算,避开nodejs自身限制的方案在可靠性和扩展性上都相对较为令人满意. mongodb支持类似SQL中的聚合函数,虽然语法不通,不过基本原理类似. mongodb自带的接口中,aggregate被用来实现聚合查询: rec = db.LIBRARY.agg

C# this用法系列(二) 通过this修饰符为原始类型扩展方法

定义一个静态类,类中定义静态方法,方法中参数类型前边加上this修饰符,即可实现对参数类型的方法扩展 示例如namespace Demo{ // 这里的类必须为静态类 public static class Json { // 方法为静态方法 // this修饰符后边是string类型,即为string类型扩展出了ToJson方法 public static object ToJson(this string Json) { return Json == null ? null : JsonCo

Android中的关于MDM中的几个方法举例

Android中的关于MDM中的几个方法举例 首先介绍一下MDM是什么的缩写,MDM是什么? MDM 是 (Mobile Device Management )的缩写,中文翻译过来就是移动设备管理.随着移动设备计算能力地增强,移动设备携带越来越方便,移动化办公已经成为一种潮流,一种趋势,企业对移动设备管理的需求越来越急切.MDM是企业IT 向移动互联网过渡的平台技术,帮助企业将IT管理能力从传统的 PC 延伸到移动设备甚至 移动应用APP . 随着时间的发展, MDM 厂商逐渐扩展出 MAM (

浅谈Java 8中的方法引用(Method References)

本人接触Java 8的时间不长,对Java 8的一些新特性略有所知.Java 8引入了一些新的编程概念,比如经常用到的 lambda表达式.Stream.Optional以及Function等,让人耳目一新.这些功能其实上手并不是很难,根据别人的代码抄过来改一下,并不要知道内部的实现原理,也可以很熟练地用好这些功能.但是当我深究其中一些细节时,会发现有一些知识的盲区.下面我就来谈一下Java 8中的Method References这个概念. 首先我给出官方对于这一概念的详细解释,https:/

Hadoop学习笔记—11.MapReduce中的排序和分组

一.写在之前的 1.1 回顾Map阶段四大步凑 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排序和分组,默认情况下,是按照key进行排序和分组. 1.2 实验场景数据文件 在一些特定的数据文件中,不一定都是类似于WordCount单次统计这种规范的数据,比如下面这类数据,它虽然只有两列,但是却有一定的实践意义. 3 3 3 2 3 1 2 2 2 1 1 1 (1)如果按照第一列升序排列,当

MapReduce实现两表的Join--原理及python和java代码实现

用Hive一句话搞定的,但是有时必须要用mapreduce 方法介绍 1. 概述 在传统数据库(如:MYSQL)中,JOIN操作是非常常见且非常耗时的.而在HADOOP中进行JOIN操作,同样常见且耗时,由于Hadoop的独特设计思想,当进行JOIN操作时,有一些特殊的技巧. 本文首先介绍了Hadoop上通常的JOIN实现方法,然后给出了几种针对不同输入数据集的优化方法. 2. 常见的join方法介绍 假设要进行join的数据分别来自File1和File2. 2.1 reduce side jo