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. (转) Spring框架笔记(二十五)——NamedParameterJdbcTemplate与具名参数(转)

    在经典的 JDBC 用法中, SQL 参数是用占位符 ? 表示,并且受到位置的限制. 定位参数的问题在于, 一旦参数的顺序发生变化, 就必须改变参数绑定. 在 Spring JDBC 框架中, 绑定 ...

  2. js判断类型方法

    在JavaScript中,有5种基本数据类型和1种复杂数据类型,基本数据类型有:Undefined, Null,Boolean, Number和String:复杂数据类型是Object,Object中 ...

  3. CentOS上yum安装JDK

    转: http://blog.csdn.net/onepiecehuiyu/article/details/17189571

  4. Debian普通用户获取root权限|sudo的安装与配置

    Debian系统的普通用户需要安装软件时,往往会收到“Permission denied”的提示,这时候需要root权限.那么如何在不登陆超级管理员账户的前提下拥有root权限呢?对于大多数Linux ...

  5. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  6. 【转】MAPI over HTTP协议

    这是一篇非常详细和精彩的介绍MAPI over HTTP协议英文博文.原文地址如下: http://blogs.technet.com/b/exchange/archive/2014/05/09/ou ...

  7. ThinkPHP中的动态缓存(S方法)和快速缓存(F方法)

    系统默认的缓存方式是采用File方式缓存,我们可以在项目配置文件里面定义其他的缓存方式,例如,修改默认的缓存方式为Xcache(当然,你的环境需要支持Xcache)    对于File方式缓存下的缓存 ...

  8. ugui,button的一个坑

    如果ugui button下面挂了一些image或text,而这些image或text的范围超出了button本身的范围,则应将image和text的raycast Target属性的勾选去掉,否则只 ...

  9. Disque

    Disque是一个内存储存的分布式任务队列实现, 它由 Redis 的作者 Salvatore Sanfilippo (@antirez)开发, 目前正处于预览版(alpha)阶段. 本文档将对 Di ...

  10. Maven新建webapp项目index.jsp报错

    最近用eclipse新建了一个maven项目,结果刚新建完成index.jsp页面就报错了,先把错误信息贴出来看看 后来就找资料,结果发现两种解决办法,希望可以帮助用得上的人! 第一种:直接在pom. ...