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方法的更多相关文章

  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. QT5学习过程的小问题集锦

    *** only available with -std=c++11 or -std=gnu++11 添加以下代码到*.pro文件. CONFIG += c++11 在 Qt creator 中设置 ...

  2. python unicode 和 str 类型的关系

    python (2.X)在进行 运行时候字符串运算的时候, 分为两种类型 str, unicode 前者是 二进制的形式进行对字符串的保存, 后者是 以unicode的方式进行保存, 一般的工作方式为 ...

  3. 细说php一些常见的知识点

    一.认识脚本语言 1.常见的脚本语言有:html,css,js,asp,Python等 2.脚本语言的特性: a.语法和机构通常比较简单 b.学习和使用通常比较简单 c.通常以容易修改程序的“解释”作 ...

  4. 关于word excel 等的信息隐藏技术

    简单的word 信息隐藏技术分为两种 一  利用word自带的功能对信息进行隐藏,即选中要隐藏的文字 单击右键 选择字体  给隐藏选项打勾即可    这种信息隐藏比较简单  找到的方式为单机文件——找 ...

  5. DOCTYPE声明的作用是什么?严格模式与混杂模式如何区分?

    HTML语言已经存在太久了,目前必然会有一些不同版本的文档存在,为了能够让浏览器清楚你的文档的版本类型和风格,需要在文档的起始用DOCTYPE声明制定当前文档的版本和风格.如果在网页中提供了版本信息, ...

  6. iOS开发之cell多按钮

    iOS开发经常出现cell需要多个按钮,一般以为要导入第三方框架.但其实iOS 8以后,系统提供了UITableViewRowAction以及新的delegate方法,使得自定义一些操作变得非常容易. ...

  7. toUnsignedString详解

    /** * All possible chars for representing a number as a String */ final static char[] digits = { '0' ...

  8. 自己瞎捣腾的Win7下Linux安装之路-----理论篇

    接着上回说道,我把双系统做好啦,开心.... 之后我就在想几个问题: 1.在Ubuntu装好后,重启电脑却还是win7,等我用EasyBCD之后,才可选择使用装好的Ubuntu呢? 2.在用EasyB ...

  9. 解决嵌入式linux系统下iconv库的移植问题

    一.解决问题    在arm开发板上使用framebuff,在汉字显示时,因为只有gb2312的16*16的汉字字库,而ubuntu16.04默认    的编码方式时utf-8,因此需要进行转码(ut ...

  10. LeetCode-Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...