MapReduce方法主体:

  1. public static IDictionary<TKey, TResult> MapReduce<TInput, TKey, TValue, TResult>(this IList<TInput> inputList,
  2. Func<MapReduceData<TInput>, KeyValueClass<TKey, TValue>> map, Func<TKey, IList<TValue>, TResult> reduce)
  3. {
  4. object locker = new object();
  5. ConcurrentDictionary<TKey, TResult> result = new ConcurrentDictionary<TKey, TResult>();
  6. //保存map出来的结果
  7. ConcurrentDictionary<TKey, IList<TValue>> mapDic = new ConcurrentDictionary<TKey, IList<TValue>>();
  8. var parallelOptions = new ParallelOptions();
  9. parallelOptions.MaxDegreeOfParallelism = Environment.ProcessorCount;
  10. //并行map
  11. Parallel.For(, inputList.Count(), parallelOptions, t =>
  12. {
  13. MapReduceData<TInput> data = new MapReduceData<TInput>
  14. {
  15. Data = inputList[t],
  16. Index = t,
  17. List = inputList,
  18. };
  19. var pair = map(data);
  20. if (pair != null && pair.Valid)
  21. {
  22. //锁住防止并发操作list造成数据缺失
  23. lock (locker)
  24. {
  25. //将匹配出来的结果加入结果集放入字典
  26. IList<TValue> list = null;
  27. if (mapDic.ContainsKey(pair.Key))
  28. {
  29. list = mapDic[pair.Key];
  30. }
  31. else
  32. {
  33. list = new List<TValue>();
  34. mapDic[pair.Key] = list;
  35. }
  36. list.Add(pair.Value);
  37. }
  38. }
  39. });
  40.  
  41. //并行reduce
  42. Parallel.For(, mapDic.Keys.Count, parallelOptions, t =>
  43. {
  44. KeyValuePair<TKey, IList<TValue>> pair = mapDic.ElementAt(t);
  45. result[pair.Key] = reduce(pair.Key, pair.Value);
  46. });
  47. return result;
  48. }

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 = });
  3. listTestClass.Add(new TestClass { a = "b", g = });
  4. listTestClass.Add(new TestClass { a = "c", g = });
  5. listTestClass.Add(new TestClass { a = "d", g = });
  6. listTestClass.Add(new TestClass { a = "e", g = });
  7. listTestClass.Add(new TestClass { a = "f", g = });
  8. listTestClass.Add(new TestClass { a = "g", g = });
  9. listTestClass.Add(new TestClass { a = "h", g = });
  10. IDictionary<int, string> dic = listTestClass.MapReduce(t =>
  11. {
  12. if (t.g < )
  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

词频性能测试

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

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

    随手记录一下,这是针对原文错误的修改. 原文:EF中扩展出Between操作符 直接使用是错误的,修改后的扩展方法: /// <summary> /// 扩展 Between 操作符 // ...

  2. 阵列卡,组成的磁盘组就像是一个硬盘,pci-e扩展出sata3.0

    你想提升性能,那么组RAID0,主板上的RAID应该是软RAID,肯定没有阵列卡来得稳定.如果你有闲钱,可以考虑用阵列卡. 不会的.即使不能起到RAID的作用,起码也可以当作直接连接了2个硬盘.不会影 ...

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

    此方法并非笔者原创,笔者只是在前辈的基础上,加以总结,得出一种简洁实用的JavaScript继承方法. 传统的JavaScript继承基于prototype原型链,并且需要使用大量的new操作,代码不 ...

  4. javascript中的继承-寄生组合式继承

    前文说过,组合继承是javascript最常用的继承模式,不过,它也有自己的不足:组合继承无论在什么情况下,都会调用两次父类构造函数,一次是在创建子类原型的时候,另一次是在子类构造函数内部.子类最终会 ...

  5. DOM、SAX、JDOM、DOM4J四种XML解析方法PK

    基础方法(指不需要导入jar包,java自身提供的解析方式):DOM.SAXDOM:是一种平台无关的官方解析方式   --优点:          (1)形成了树结构,直观好理解,代码更易编写     ...

  6. PHP 魔术方法 __call 与 __callStatic 方法

    PHP 魔术方法 __call 与 __callStatic 方法 PHP 5.3 后新增了 __call 与 __callStatic 魔法方法. __call 当要调用的方法不存在或权限不足时,会 ...

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

    Android中的关于MDM中的几个方法举例 首先介绍一下MDM是什么的缩写,MDM是什么? MDM 是 (Mobile Device Management )的缩写,中文翻译过来就是移动设备管理.随 ...

  8. 关于mongodb的mapReduce

    由于nodejs本身的限制,在程序中使用js进行大批量计算效率不高.而V8引擎自身对内存大小的限制(64位系统下1.4G),同样限制了数据规模. 因此,相对于从mongodb中抽出数据进行计算,在mo ...

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

    一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...

随机推荐

  1. CSS中的text-overflow:clip|ellipsis的使用

    如果想让某个容器(div或者li或者...块级元素)显示一行文字,当文字内容过多时,不换行,而是出现...,可以使用text-overflow:clip|ellipsis 基本语法:text-over ...

  2. Ftp类

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...

  3. Python 2x -> 3.x

    Nowadays, Python 3 is becoming more and more popular than Python 2, but there are still a lot of cod ...

  4. QT QString 很全的使用 (转)

    QString, QByteArray, 和 QVariant这三个类和容器有许多相同之处,并且在一些情况下可以被当作特殊的容器. 同样,像容器,这些类使用隐式共享来优化内存和速度. 我们将从QStr ...

  5. [BZOJ 3191][JLOI 2013]卡牌游戏

    觉得这题很有必要讲一下! 现在发现在做概率题,基本是向 dp 和 马尔可夫链 靠齐 但是这一题真是把我坑了,因为状态太多,马式链什么的直接死了 我一开始的想法就是用 f[i][j] 表示剩余 i 个人 ...

  6. 第一个Struts2程序之HelloWorld

    1.Struts2 简介 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与St ...

  7. css3 animation-fill-mode 对布局的影响

    问题描述:在小米手机上,animation-fill-mode设置为 both时,在手机上的web页面会超出屏幕宽度,出现滚动条. 解决方法:animation-fill-mode设为none. .p ...

  8. 利用C语言获得网页编码

    #include <stdio.h> #include <winsock.h> #include <string.h> #pragma comment(lib, & ...

  9. Java中事务的概念

    | 版权声明:本文为博主原创文章,未经博主允许不得转载. 前言: 在学习事务确实有不少弯路,那么今天笔者就用例子讲解一下事务,让初学者少走一些弯路. [回顾事务]问:什么是事务? 答:用专业术语来说事 ...

  10. python 发邮件-带附件-文本-html

    #!/usr/bin/python # encoding=utf-8 # Filename: send_email.py from email.mime.image import MIMEImage ...