531℃

Listing 1. 主程序
public class Main
{
public static void main(String[] args)
{
MyMapReduce my = new MyMapReduce();
my.init();
}
}
Listing 2. MyMapReduce.java
import java.util.*;
public class MyMapReduce
{
List buckets = new ArrayList();
List intermediateresults = new ArrayList();
List values = new ArrayList();
public void init()
{
for(int i = 1; i<=30; i++)
{
}
System.out.println(“**STEP 1 START**-> Running Conversion into Buckets**”);
System.out.println();
List b = step1ConvertIntoBuckets(values,5);
System.out.println(“************STEP 1 COMPLETE*************”);
System.out.println();
System.out.println();
System.out.println(“**STEP 2 START**->Running **Map Function** concurrently for all        Buckets”);
System.out.println();
List res = step2RunMapFunctionForAllBuckets(b);
System.out.println(“************STEP 2 COMPLETE*************”);
System.out.println();
System.out.println();
System.out.println(“**STEP 3 START**->Running **Reduce Function** for collating Intermediate Results and Printing Results”);
System.out.println();
step3RunReduceFunctionForAllBuckets(res);
System.out.println(“************STEP 3 COMPLETE*************”);
System.out.println(“************pconline900 翻译*************”);
System.out.println(“***********博客：http://pconline900.javaeye.com*************”);
}
public List step1ConvertIntoBuckets(List list,int numberofbuckets)
{
int n = list.size();
int m = n / numberofbuckets;
int rem = n% numberofbuckets;
int count = 0;
System.out.println(“BUCKETS”);
for(int j =1; j<= numberofbuckets; j++)
{
List temp = new ArrayList();
for(int i=1; i<= m; i++)
{
count++;
}
temp = new ArrayList();
}
if(rem != 0)
{
List temp = new ArrayList();
for(int i =1; i<=rem;i++)
{
count++;
}
}
System.out.println();
System.out.println(buckets);
System.out.println();
return buckets;
}
public List step2RunMapFunctionForAllBuckets(List list)
{
for(int i=0; i< list.size(); i++)
{
List elementList = (ArrayList)list.get(i);
}
try
{
}catch(Exception e)
{
}
return intermediateresults;
}
public void step3RunReduceFunctionForAllBuckets(List list)
{
int sum =0;
for(int i=0; i< list.size(); i++)
{
//you can do some processing here, like finding max of all results etc
int t = Integer.parseInt((String)list.get(i));
sum += t;
}
System.out.println();
System.out.println(“Total Count is “+ sum);
System.out.println();
}
{
private List tempList = new ArrayList();
{
tempList = list;
}
public void run()
{
for(int i=0; i< tempList.size();i++)
{
String str = (String)tempList.get(i);
synchronized(this)
{
}
}
}
}
}
? init()方法创建了一些测试数据，作为测试数据。实际应用中会是海量数据处理。
? step1ConvertIntoBuckets()方法将测试数据拆分到5个 bucket中，每个bucket是一个ArrayList(包含6个String数据)。bucket可以保存在内存，磁盘，或者集群中的其他节点；