如何使用PLINQ在C#中实现MapReduce?
假设您有7-8个WebServices来收集数据,并且在每次接收(异步方式)时,您必须将数据放入数据库的某些表中,在我的例子中是SQL Server2008。例如,您从每个Web服务获得的数据是:
<employees>
<employee>
<name>Ramiz</name>
</employee>
<employee>
<name>Aamir</name>
</employee>
<employee>
<name>Zubair</name>
</employee>
</employees>而且,每次收到响应时,此数据都会进入一个表名为Employee的表中:
Employee
===
EmployeeID (PK)
EmployeeName一旦数据进入表中,它就必须以json的形式返回给客户端,即ASP.NET (MVC3)应用程序正在使用客户端JavaScript (ajax)进行此调用。
假设,一个WebServiceEmployee1返回了数据,其他6个在队列中(仍在尝试获取数据)。然后,它应该将结果集注册到表中,而不是等待其他6,并在json中将插入的employee的数据返回给客户端。而且,当其他人也这样做的时候,保持连接并在做。
请看,在我的工具带中有SQLMVC3(剃刀),ASP.NET server2008 R2,jQuery。
谢谢。
发布于 2012-05-04 11:30:47
如果不清楚您想要执行的处理过程,我建议您阅读以下MSDN文档:http://bit.ly/Ir7Nvk它使用PLINQ描述了map/reduce,示例如下:
public IDMultisetItemList PotentialFriendsPLinq(SubscriberID id,
int maxCandidates)
{
var candidates =
subscribers[id].Friends.AsParallel()
.SelectMany(friend => subscribers[friend].Friends)
.Where(foaf => foaf != id &&
!(subscribers[id].Friends.Contains(foaf)))
.GroupBy(foaf => foaf)
.Select(foafGroup => new IDMultisetItem(foafGroup.Key,
foafGroup.Count()));
return Multiset.MostNumerous(candidates, maxCandidates);
}"map“是Friends.AsParallel、SelectMany和Where,而"reduce”阶段是GroupBy和Select。
发布于 2012-06-11 22:26:55
此来自MS的PDF在中端有一个Map Reduce示例
public static IEnumerable<TResult> MapReduce<TSource, TMapped, TKey, TResult>(
this IEnumerable<TSource> source,
Patterns of Parallel Programming Page 76
Func<TSource, IEnumerable<TMapped>> map,
Func<TMapped, TKey> keySelector,
Func<IGrouping<TKey, TMapped>, IEnumerable<TResult>> reduce)
{
return source.SelectMany(map)
.GroupBy(keySelector)
.SelectMany(reduce);
}https://stackoverflow.com/questions/10119736
复制相似问题