Skip to content

Instantly share code, notes, and snippets.

@VincentTatan
Last active September 29, 2015 15:16
Show Gist options
  • Save VincentTatan/388cb06634ef033371fd to your computer and use it in GitHub Desktop.
Save VincentTatan/388cb06634ef033371fd to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package practice;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Vincent Tatan
*/
public class MapReduceGame3 implements Mapper, Reducer{
public HashMap map(List list)
{
HashMap <String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
for (Object o: list)
{
String s = (String)o;
String[] sArray = s.split(",");
String key = sArray[0].trim()+"-"+sArray[1].trim();
int total = Integer.parseInt(sArray[2])*Integer.parseInt(sArray[3]);
int price = Integer.parseInt(sArray[2]);
ArrayList<String> newValue = new ArrayList<>();
//data looks like: cheetah-A, 5,100
//puts "cheetah-A" as key, "5,100" as value.
ArrayList<String> currentValue = map.get(key);
if (currentValue == null) //key not currently in map. Add new one
{
newValue.add(String.valueOf(total)+"#"+String.valueOf(price));
map.put(key, newValue);
}
else //key already in map, add to value
{
currentValue.add(String.valueOf(total)+"#"+String.valueOf(price));
map.put(key, currentValue);
}
}
return map;
}
public HashMap reduce(Object key, List data)
{
HashMap<Object, ArrayList<Integer> >result = new HashMap<Object, ArrayList<Integer> >(1);
/* SUM */
for (Object o : data)
{
int value = 0;
int count = 0;
int priceTotal = 0;
ArrayList<String> totalPriceList = (ArrayList)o;
ArrayList<Integer> results = result.get(key);
for (String s: totalPriceList){
String [] totalPrice = s.split("#");
int total = Integer.parseInt(totalPrice[0]);
int price = Integer.parseInt(totalPrice[1]);
value += total;
priceTotal += price;
count++;
}
if(results == null){
results = new ArrayList<>();
}else{
value += results.get(0);
count += results.get(1);
priceTotal += results.get(2);
results.clear();
}
results.add(value);
results.add(count);
results.add(priceTotal);
result.put(key, results);
}
return result;
}
public static void main(String[] args)
{
//----------- DUMMY DATA SET -------
String fileLocation = "src/resources";
List<String> data = readFile(fileLocation+"/Lions-Tigers-Bears.csv");
MapReduceGame3 mapper = new MapReduceGame3(); //create mapper object
MapReduceGame3 reducer = new MapReduceGame3(); //create reducer object
HashMap<Object, List> results = null;
System.gc(); //good to do garbage collection before timing anything
long s = System.currentTimeMillis(); //ready, set, GO!
try {
results = MapReduce.mapReduce(mapper, reducer, data, 3, true);
} catch (InterruptedException ex) {
Logger.getLogger(MapReduceGame3.class.getName()).log(Level.SEVERE, null, ex);
}
long e = System.currentTimeMillis(); //And the winning time is....
System.out.println("Output from reducers: ");
int total = 0;
int count = 0;
for (Object o : results.keySet())
{
//System.out.println(o + " " + results.get(o));
ArrayList<Integer> totalCount = (ArrayList<Integer>)results.get(o).get(0);
total += totalCount.get(0);
count += totalCount.get(1);
System.out.println(o + " count : " + totalCount.get(1) + " average price : " + ((totalCount.get(2)*1.0)/totalCount.get(1)));
}
System.out.println("Average price per order : " + total/(count*1.0));
System.out.println("Clock time elapsed: " + (e - s) + " ms");
}
public static ArrayList<String> readFile(String filename){
ArrayList<String> list = new ArrayList<>();
try{
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while((line=reader.readLine()) !=null) {
list.add(line);
}
return list;
}catch(FileNotFoundException e){
e.printStackTrace();
}
}catch(IOException e){
e.printStackTrace();
}
return null;
}
}
Sharding data -> 2 shards in the input to send to mappers
34 values sent to mapper -> [Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200, Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100, Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100, Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200, Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100]
34 values sent to mapper -> [Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100, Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200, Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100, Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100, Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200]
32 values sent to mapper -> [Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100, Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100, Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200, Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100, Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100]
3 values sent to reducer -> [[3600#18, 3600#12, 2800#14, 1500#15, 3600#18, 3600#12], [2800#14, 1500#15, 3600#18, 3600#12, 2800#14, 1500#15, 3600#18, 3600#12], [2800#14, 1500#15, 3600#18, 3600#12, 2800#14, 1500#15]]
3 values sent to reducer -> [[800#8, 1800#9, 800#8, 1800#9], [800#8, 1800#9, 800#8], [1800#9, 800#8, 1800#9]]
3 values sent to reducer -> [[1400#14, 1300#13, 1200#12, 1100#11, 1400#14], [1300#13, 1200#12, 1100#11, 1400#14, 1300#13, 1200#12, 1100#11, 1400#14], [1300#13, 1200#12, 1100#11, 1400#14, 1300#13, 1200#12, 1100#11]]
3 values sent to reducer -> [[1200#6, 500#5, 1200#6, 500#5], [1200#6, 500#5, 1200#6], [500#5, 1200#6, 500#5]]
3 values sent to reducer -> [[1000#10, 1100#11, 900#9, 1100#11, 1000#10, 1100#11, 900#9, 1100#11], [1000#10, 1100#11, 900#9, 1100#11, 1000#10, 1100#11, 900#9], [1100#11, 1000#10, 1100#11, 900#9, 1100#11]]
3 values sent to reducer -> [[1600#16, 6000#15, 1400#14, 1400#14, 1600#16, 6000#15, 1400#14], [1400#14, 1600#16, 6000#15, 1400#14, 1400#14], [1600#16, 6000#15, 1400#14, 1400#14, 1600#16, 6000#15, 1400#14, 1400#14]]
Output from reducers:
Tiger-B count : 20 average price : 14.75
Bear-B count : 10 average price : 8.5
Tiger-A count : 20 average price : 12.5
Bear-A count : 10 average price : 5.5
Lion-A count : 20 average price : 10.25
Lion-B count : 20 average price : 14.75
Average price per order : 1765.0
Clock time elapsed: 8 ms
@VincentTatan
Copy link
Author

Sharding data -> 2 shards in the input to send to mappers
34 values sent to mapper -> [Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200, Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100, Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100, Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200, Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100]
34 values sent to mapper -> [Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100, Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200, Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100, Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100, Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200]
32 values sent to mapper -> [Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100, Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100, Lion,A,10,100, Tiger,B,18,200, Lion,A,11,100, Tiger,B,12,300, Lion ,A,9,100, Tiger,A,14,100, Bear ,B,8,100, Bear ,A,6,200, Lion ,B,16,100, Lion,B,15,400, Lion ,B,14,100, Lion,A,11,100, Bear ,B,9,200, Bear ,A,5,100, Tiger,A,13,100, Tiger ,B,14,200, Tiger,A,12,100, Tiger ,A,11,100, Tiger ,B,15,100, Lion,B,14,100]
3 values sent to reducer -> [[3600#18, 3600#12, 2800#14, 1500#15, 3600#18, 3600#12], [2800#14, 1500#15, 3600#18, 3600#12, 2800#14, 1500#15, 3600#18, 3600#12], [2800#14, 1500#15, 3600#18, 3600#12, 2800#14, 1500#15]]
3 values sent to reducer -> [[800#8, 1800#9, 800#8, 1800#9], [800#8, 1800#9, 800#8], [1800#9, 800#8, 1800#9]]
3 values sent to reducer -> [[1400#14, 1300#13, 1200#12, 1100#11, 1400#14], [1300#13, 1200#12, 1100#11, 1400#14, 1300#13, 1200#12, 1100#11, 1400#14], [1300#13, 1200#12, 1100#11, 1400#14, 1300#13, 1200#12, 1100#11]]
3 values sent to reducer -> [[1200#6, 500#5, 1200#6, 500#5], [1200#6, 500#5, 1200#6], [500#5, 1200#6, 500#5]]
3 values sent to reducer -> [[1000#10, 1100#11, 900#9, 1100#11, 1000#10, 1100#11, 900#9, 1100#11], [1000#10, 1100#11, 900#9, 1100#11, 1000#10, 1100#11, 900#9], [1100#11, 1000#10, 1100#11, 900#9, 1100#11]]
3 values sent to reducer -> [[1600#16, 6000#15, 1400#14, 1400#14, 1600#16, 6000#15, 1400#14], [1400#14, 1600#16, 6000#15, 1400#14, 1400#14], [1600#16, 6000#15, 1400#14, 1400#14, 1600#16, 6000#15, 1400#14, 1400#14]]
Output from reducers:
Tiger-B count : 20 average price : 14.75
Bear-B count : 10 average price : 8.5
Tiger-A count : 20 average price : 12.5
Bear-A count : 10 average price : 5.5
Lion-A count : 20 average price : 10.25
Lion-B count : 20 average price : 14.75
Average price per order : 1765.0
Clock time elapsed: 8 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment