c#扩展出MapReduce方法
MapReduce方法主体:
- public static IDictionary<TKey, TResult> MapReduce<TInput, TKey, TValue, TResult>(this IList<TInput> inputList,
- Func<MapReduceData<TInput>, KeyValueClass<TKey, TValue>> map, Func<TKey, IList<TValue>, TResult> reduce)
- {
- object locker = new object();
- ConcurrentDictionary<TKey, TResult> result = new ConcurrentDictionary<TKey, TResult>();
- //保存map出来的结果
- ConcurrentDictionary<TKey, IList<TValue>> mapDic = new ConcurrentDictionary<TKey, IList<TValue>>();
- var parallelOptions = new ParallelOptions();
- parallelOptions.MaxDegreeOfParallelism = Environment.ProcessorCount;
- //并行map
- Parallel.For(, inputList.Count(), parallelOptions, t =>
- {
- MapReduceData<TInput> data = new MapReduceData<TInput>
- {
- Data = inputList[t],
- Index = t,
- List = inputList,
- };
- var pair = map(data);
- if (pair != null && pair.Valid)
- {
- //锁住防止并发操作list造成数据缺失
- lock (locker)
- {
- //将匹配出来的结果加入结果集放入字典
- IList<TValue> list = null;
- if (mapDic.ContainsKey(pair.Key))
- {
- list = mapDic[pair.Key];
- }
- else
- {
- list = new List<TValue>();
- mapDic[pair.Key] = list;
- }
- list.Add(pair.Value);
- }
- }
- });
- //并行reduce
- Parallel.For(, mapDic.Keys.Count, parallelOptions, t =>
- {
- KeyValuePair<TKey, IList<TValue>> pair = mapDic.ElementAt(t);
- result[pair.Key] = reduce(pair.Key, pair.Value);
- });
- return result;
- }
KeyValueClass定义:
- public class KeyValueClass<K, V>
- {
- public KeyValueClass(K key, V value)
- {
- Key = key;
- Value = value;
- }
- public KeyValueClass()
- {
- }
- public K Key { get; set; }
- public V Value { get; set; }
- }
Console测试:
- List<TestClass> listTestClass = new List<TestClass>();
- listTestClass.Add(new TestClass { a = "a", g = });
- listTestClass.Add(new TestClass { a = "b", g = });
- listTestClass.Add(new TestClass { a = "c", g = });
- listTestClass.Add(new TestClass { a = "d", g = });
- listTestClass.Add(new TestClass { a = "e", g = });
- listTestClass.Add(new TestClass { a = "f", g = });
- listTestClass.Add(new TestClass { a = "g", g = });
- listTestClass.Add(new TestClass { a = "h", g = });
- IDictionary<int, string> dic = listTestClass.MapReduce(t =>
- {
- if (t.g < )
- {
- return new KeyValueClass<int, string>(t.g, t.a);
- }
- return null;
- }, (key, values) =>
- {
- return string.Join(",", values);
- });
TestClass定义:
- public class TestClass
- {
- public string a { get; set; }
- public string b { get; set; }
- public string d { get; set; }
- //public DateTime f { get; set; }
- public int g { get; set; }
- public List<TestClass> test { get; set; }
- public Dictionary<string, string> dic { get; set; }
- }
结果:
1:a,e
2:d,f
3:b
4:c
词频性能测试
c#扩展出MapReduce方法的更多相关文章
- EF中扩展出Between操作符 (修订版)
随手记录一下,这是针对原文错误的修改. 原文:EF中扩展出Between操作符 直接使用是错误的,修改后的扩展方法: /// <summary> /// 扩展 Between 操作符 // ...
- 阵列卡,组成的磁盘组就像是一个硬盘,pci-e扩展出sata3.0
你想提升性能,那么组RAID0,主板上的RAID应该是软RAID,肯定没有阵列卡来得稳定.如果你有闲钱,可以考虑用阵列卡. 不会的.即使不能起到RAID的作用,起码也可以当作直接连接了2个硬盘.不会影 ...
- JavaScript简洁继承机制实现(不使用prototype和new)
此方法并非笔者原创,笔者只是在前辈的基础上,加以总结,得出一种简洁实用的JavaScript继承方法. 传统的JavaScript继承基于prototype原型链,并且需要使用大量的new操作,代码不 ...
- javascript中的继承-寄生组合式继承
前文说过,组合继承是javascript最常用的继承模式,不过,它也有自己的不足:组合继承无论在什么情况下,都会调用两次父类构造函数,一次是在创建子类原型的时候,另一次是在子类构造函数内部.子类最终会 ...
- DOM、SAX、JDOM、DOM4J四种XML解析方法PK
基础方法(指不需要导入jar包,java自身提供的解析方式):DOM.SAXDOM:是一种平台无关的官方解析方式 --优点: (1)形成了树结构,直观好理解,代码更易编写 ...
- PHP 魔术方法 __call 与 __callStatic 方法
PHP 魔术方法 __call 与 __callStatic 方法 PHP 5.3 后新增了 __call 与 __callStatic 魔法方法. __call 当要调用的方法不存在或权限不足时,会 ...
- Android中的关于MDM中的几个方法举例
Android中的关于MDM中的几个方法举例 首先介绍一下MDM是什么的缩写,MDM是什么? MDM 是 (Mobile Device Management )的缩写,中文翻译过来就是移动设备管理.随 ...
- 关于mongodb的mapReduce
由于nodejs本身的限制,在程序中使用js进行大批量计算效率不高.而V8引擎自身对内存大小的限制(64位系统下1.4G),同样限制了数据规模. 因此,相对于从mongodb中抽出数据进行计算,在mo ...
- Hadoop学习笔记—11.MapReduce中的排序和分组
一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...
随机推荐
- CSS中的text-overflow:clip|ellipsis的使用
如果想让某个容器(div或者li或者...块级元素)显示一行文字,当文字内容过多时,不换行,而是出现...,可以使用text-overflow:clip|ellipsis 基本语法:text-over ...
- Ftp类
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- Python 2x -> 3.x
Nowadays, Python 3 is becoming more and more popular than Python 2, but there are still a lot of cod ...
- QT QString 很全的使用 (转)
QString, QByteArray, 和 QVariant这三个类和容器有许多相同之处,并且在一些情况下可以被当作特殊的容器. 同样,像容器,这些类使用隐式共享来优化内存和速度. 我们将从QStr ...
- [BZOJ 3191][JLOI 2013]卡牌游戏
觉得这题很有必要讲一下! 现在发现在做概率题,基本是向 dp 和 马尔可夫链 靠齐 但是这一题真是把我坑了,因为状态太多,马式链什么的直接死了 我一开始的想法就是用 f[i][j] 表示剩余 i 个人 ...
- 第一个Struts2程序之HelloWorld
1.Struts2 简介 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与St ...
- css3 animation-fill-mode 对布局的影响
问题描述:在小米手机上,animation-fill-mode设置为 both时,在手机上的web页面会超出屏幕宽度,出现滚动条. 解决方法:animation-fill-mode设为none. .p ...
- 利用C语言获得网页编码
#include <stdio.h> #include <winsock.h> #include <string.h> #pragma comment(lib, & ...
- Java中事务的概念
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 前言: 在学习事务确实有不少弯路,那么今天笔者就用例子讲解一下事务,让初学者少走一些弯路. [回顾事务]问:什么是事务? 答:用专业术语来说事 ...
- python 发邮件-带附件-文本-html
#!/usr/bin/python # encoding=utf-8 # Filename: send_email.py from email.mime.image import MIMEImage ...