hadoop-common-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adarsh Sharma <adarsh.sha...@orkash.com>
Subject Factorial in Map-Reduce
Date Wed, 13 Oct 2010 06:57:20 GMT
Hi
I am practising some programs in Map-Reduce such as WordCount, Word 
Search  , Grep etc
Now I want to know is it possible to write Map-Reduce  program on hadoop 
for finding *Factorial of Number*.
In that case how we give InputFormat, what are key-values etc.
I made this program in Java but not able to  convert  in Hadoop  
Map-Reduce. Below is the  code :
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class FactMapReduce
{
  List buckets = new ArrayList();
  List intermediateresults = new ArrayList();
  List values = new ArrayList();

  public void init(int n)
  {
     
    for(int i = 1; i<=n; i++)
    {
      values.add( new Integer(i).toString());
      System.out.println(values);
    }
    System.out.println("**Running Conversion into Buckets**\n");
    //convert the input data in smaller chunks. Here dividing 30 strings 
into chunks of 6 chunks of 5.
    List b = step1ConvertIntoBuckets(values,3);
    System.out.println("*************DONE*******************\n\n");
    System.out.println("************Running Map Function concurrently 
for all Buckets**********\n\n");
    List res = step2RunMapFunctionForAllBuckets(b);
    System.out.println("*MAP Done*\n");
    System.out.println(res);
    System.out.println("**Running #Reduce Function# for collating 
Intermediate Results and Printing Results\n");
    step3RunReduceFunctionForAllBuckets(res);
    System.out.println("*REDUCE Done*\n");
  }
  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<String> temp = new ArrayList<String>();
      for(int i=1; i<= m; i++)
      {
        temp.add((String)values.get(count));
        count++;
      }
      buckets.add(temp);
      temp = new ArrayList<String>();
    }
    if(rem != 0)
    {
      List<String> temp = new ArrayList<String>();
      for(int i =1; i<=rem;i++)
      {
        temp.add((String)values.get(count));
        count++;
      }
      buckets.add(temp);
    }
    System.out.println(buckets);
    return buckets;
  }

  public List step2RunMapFunctionForAllBuckets(List list)
  {
    for(int i=0; i< list.size(); i++)
    {
      List<String> elementList = (ArrayList)list.get(i);
      new StartThread(elementList).start();
    }
    try
    {
      Thread.currentThread().sleep(1000);
    }catch(Exception e)
    {    }
    return intermediateresults;
  }

  public void step3RunReduceFunctionForAllBuckets(List list)
  {
    int sum =1;
    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("\nFactorial of num is  "+ sum+"\n");
  }

  class StartThread extends Thread
  {
   
    private List<String> tempList = new ArrayList<String>();
    public StartThread(List<String> list)
    {
      tempList = list;
    }
    public void run()
    {
        int mul=1;
      System.out.println("In Map...");
             
        System.out.println(tempList);
      for(int i=0;i < tempList.size();i++)
      {  mul *= Integer.parseInt((String)tempList.get(i));
         System.out.println(mul);
      }
        String s = String.valueOf(mul);
        intermediateresults.add(s);
        System.out.println(intermediateresults);
    }
 }

  public static void main(String[] args)
  {
    FactMapReduce my = new FactMapReduce();
    try
    {
    BufferedReader buff = new BufferedReader(new 
InputStreamReader(System.in));
    System.out.println("Enter the number:");
    int num = Integer.parseInt(buff.readLine());
    my.init(num);
    }
  catch (Exception e){}
  }
}

Thanks in Advance

Mime
View raw message