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也就是第四步中,需要对不同分区中的数据进行排 ...
随机推荐
- Poco C++ MySQl demo
#include "Poco/Exception.h"#include "Poco/Data/Session.h"#include "Poco/Dat ...
- quartz(1)
关于定时任务的操作方法,java语言本身具有 Timer 来解决,但Timer 作用起来不是特别的舒服,由于项目的需要,使用了Quartz 这个调度框架,现把学习过程记录下来,方便以后查阅. 本教程是 ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 《C与指针》第十三章练习
本章例程 13.1类型无关的链表查找 #include <stdio.h> #include "node.h" Node *search_list(Node *node ...
- 零基础如何自学MySQL数据库?
作者:姜健链接:https://www.zhihu.com/question/34840297/answer/67536521来源:知乎著作权归作者所有,转载请联系作者获得授权. 本人是个活生生的例子 ...
- Eplan简单教程
鉴于AUTOCAD画电路图比较繁琐而且手动添加关联参考错误率较高,而EPLAN画电路图确实效率要高许多,也更规范.过年正好有点时间,把我这段时间学EPLAN的一些经验总结了一下,有兴趣可以看看,也省得 ...
- ListView的属性及方法详解
本文转载于:http://blog.csdn.net/vector_yi/article/details/23195411 近期在重新学习Android控件知识,目前进行到ListView,感觉这是一 ...
- cps变换
网上看了很多内容,很少有给出一个准确的概念,它的英文全称是continuous passing style, 直译为连续传递样式,那么cps transform就是将一些原本不是continuous ...
- JSBInding+Bridge.NET:把C#编译为Js
编译步骤: 1. 用 VS 2015 打开 BridgeProj/BridgeProj.sln ,编译 2. 将生成 BridgeProj/Bridge/output/bridge.js,对应于 Br ...
- flask在windows上用mod_wsgi部署
flask在windows上用mod_wsgi部署也是折腾了不少时间,下面就总结下. 首先下载Apache httpd,我认为Apache Hans比较好: 一般这种情况下,你的python环境已经安 ...