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也就是第四步中,需要对不同分区中的数据进行排 ...
随机推荐
- 【转】以 java 为例,总结下 appium 里的一方法的使用心得
转自:http://testerhome.com/topics/1043 1.关于没有name,没有ID的元素的定位---通用篇解题思路:因为没有name,id:其实剩下的选择已不多,要么xpath, ...
- 初学JAVA的 感想 尹鑫磊
开始学习任何一门课(包括java),兴趣最重要.一直觉得自己在学计算机编程语言,学习了很多,但每门语言都停留在知识边缘地带,都没深入到它们的精华部分,对它们的精华部分知之甚少,于是趁学校开设这门课,并 ...
- Reveal UI 分析工具简单使用
官网下载地址(30天免费试用):http://revealapp.com/ 作用: 在 iOS 开发中,我们有时很希望有一款类似 Web 开发中的 UI Debug 工具(例如:Firebug),让我 ...
- T-SQL - 访问远程数据库并对其数据表进行操作
一.概述 很多时候我们需要在.NET应用程序中使用一条SQL语句来访问来自不同服务器.不同数据库中的不同的表,实现方法有很多.我们可以在.NET应用程序中建立数据访问层,使用ADO.NET Entit ...
- Servlet高级
1. 获取初始化参数 在web.xml中配置Servlet时,可以配置一些初始化参数.而在Servlet中可以通过ServletConfig接口提供的方法来取得这些参数. index.jsp < ...
- Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- ffmpeg音频编码
在弄音频采集时,需要设置缓存的大小,如果只是简单的采集和直接播放PCM数据,缓存的大小一般不影响播放和保存. 但是,如果需要使用FFMpeg音频编码,这时,音频缓存的大小必须设置av_samples_ ...
- About SQLite
About SQLite See Also... Features When to use SQLite Frequently Asked Questions Well-known Users Boo ...
- Android proguard 详解
本文转载于:http://blog.csdn.net/banketree/article/details/41928175 简介 Java代码是非常容易反编译的.为了很好的保护Java源代码,我们往往 ...
- ASP.NET MVC图片上传
工具:ssi-uploader $('#id').ssi_uploader({ url: 'path' }); public ActionResult path() { HttpPostedFileB ...