My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.karticks.mapreduce;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;

/**
* A simple test application that runs Map-Reduce on a set of Strings to produce
* a word count.
*
* @author Kartick Suriamoorthy
*/
public class WordCounter
{
public static void main(String[] args)
{
String[] inputs = {
"Guy Montag was a fireman whose job it was to start fires The system was simple Everyone understood it Books were",
"for burning along with the houses in which they were hidden Guy Montag enjoyed his job He had been a fireman for",
"ten years and he had never questioned the pleasure of the midnight runs nor the joy of watching pages consumed by flames",
"never questioned anything until he met a seventeen year old girl who told him of a past when people were not afraid Then",
"he met a professor who told him of a future in which people could think and Guy Montag suddenly realized what he had to do" };

MapReduceWorker worker = new MapReduceWorker();

// create a mapper for every string
for (String s : inputs)
{
byte[] bytes = s.getBytes();
InputStream is = new ByteArrayInputStream(bytes);

Mapper mapper = new WordCountMapper();

worker.addMapper(mapper, is);
}

// get the final result from map-reduce
Map<String, Integer> result = worker.doWork();

// iterate over the keys
Iterator<String> iterator = result.keySet().iterator();

String output = "";
while (iterator.hasNext())
{
String key = iterator.next();

Integer value = result.get(key);

output += key + " (" + value + "), ";
}

// validate the results from the previous run
StringBuffer buffer = new StringBuffer();
for (String s : inputs)
{
buffer.append(s);
buffer.append(" ");
}
String masterString = buffer.substring(0, buffer.length() - 1);

MapReduceWorker newWorker = new MapReduceWorker();
newWorker.addMapper(new WordCountMapper(), new ByteArrayInputStream(masterString.getBytes()));

result = newWorker.doWork();
iterator = result.keySet().iterator();

String newOutput = "";
while (iterator.hasNext())
{
String key = iterator.next();

Integer value = result.get(key);

newOutput += key + " (" + value + "), ";
}

System.out.println("First Output : " + output);
System.out.println("Second Output : " + newOutput);

boolean test = output.equals(newOutput);

if (test)
{
System.out.println("Both the outputs are same !!");
}
else
{
System.out.println("Both the outputs are not the same :-(");
}
}
}
Show details Hide details

Change log

r67 by kartick.suriamoorthy on Jul 29, 2009   Diff
provided a test at the end to validate the
results from the map-reduce phase.
Go to: 
Project members, sign in to write a code review

Older revisions

r59 by kartick.suriamoorthy on Jul 29, 2009   Diff
initial checkin of map-reduce sources
All revisions of this file

File info

Size: 2729 bytes, 93 lines
Hosted by Google Code