MapReduce method Body:
1 Public StaticIdictionary<tkey, tresult> mapreduce<tinput, TKey, TValue, tresult> ( ThisIenumerable<tinput>Inputlist,2Func<tinput, Keyvalueclass<tkey, tvalue>> map, Func<tkey, Ienumerable<tvalue>, TResult>reduce)3 {4 ObjectLocker =New Object();5Concurrentdictionary<tkey, tresult> result =NewConcurrentdictionary<tkey, tresult>();6 //Save the map results7Concurrentdictionary<tkey, concurrentbag<tvalue>> mapdic =NewConcurrentdictionary<tkey, concurrentbag<tvalue>>();8 //Parallel Map9Parallel.ForEach (Inputlist, (T, p, s) = =Ten { One varPair =map (t); A if(Pair! =NULL) - { - //lock to prevent concurrent operations list causes data loss the Lock(Locker) - { - //put the matching results into the dictionary in the result set -concurrentbag<tvalue> list =NULL; + if(Mapdic.containskey (pair). Key)) - { +List =Mapdic[pair. Key]; A } at Else - { -List =NewConcurrentbag<tvalue>(); -Mapdic[pair. Key] =list; - } - list. ADD (pair. Value); in } - } to }); + - //Parallel Reduce theParallel.ForEach (Mapdic.keys, (T, p, s) = = * { $RESULT[T] =reduce (t, mapdic[t]);Panax Notoginseng }); - returnresult; the}
View Code
Keyvalueclass definition:
1 Public classKeyvalueclass<k, v>2 {3 PublicKeyvalueclass (K key, V value)4 {5Key =key;6Value =value;7 }8 9 PublicKeyvalueclass ()Ten { One A } - - PublicK Key {Get;Set; } the - PublicV Value {Get;Set; } -}
View Code
Console test:
1List<testclass> Listtestclass =NewList<testclass>();2Listtestclass.add (NewTestClass {a ="a", G =1 });3Listtestclass.add (NewTestClass {a ="b", G =3 });4Listtestclass.add (NewTestClass {a ="C", G =4 });5Listtestclass.add (NewTestClass {a ="D", G =2 });6Listtestclass.add (NewTestClass {a ="e", G =1 });7Listtestclass.add (NewTestClass {a ="F", G =2 });8Listtestclass.add (NewTestClass {a ="g", G =5 });9Listtestclass.add (NewTestClass {a ="h", G =6 });Tenidictionary<int,string> dic = listtestclass.mapreduce (t = One { A if(T.g <5) - { - return Newkeyvalueclass<int,string>(T.G, t.a); the } - return NULL; -}, (key, values) = - { + return string. Join (",", values); -});
View Code
TestClass definition:
1 Public classTestClass2 {3 Public stringAGet;Set; }4 Public stringD =Get;Set; }5 6 Public stringb =Get;Set; }7 8 //Public DateTime F {get; set;}9 Ten Public intG {Get;Set; } One A PublicList<testclass> Test {Get;Set; } - - Publicdictionary<string,string> DIC {Get;Set; } the}
View Code
Results:
1:a,e
2:d,f
3:b
4:c
C # Extending the MapReduce method