2 //import java.io.FileInputStream;
3 //import java.util.Vector;
5 /*import mapreduce.Configuration;
6 import mapreduce.Configured;
7 import mapreduce.JobClient;
8 import mapreduce.MapReduceBase;
9 import mapreduce.OutputCollector;
10 import mapreduce.Tool;
11 import mapreduce.ToolRunner;*/
14 * Counts the words in each line.
15 * For each line of input, break the line into words and emit them as
16 * (<b>word</b>, <b>1</b>).
18 public class MapReduceClass extends MapReduceBase {
20 public MapReduceClass() {}
22 public void map(String key, String value, OutputCollector output) {
23 int n = value.length();
24 for (int i = 0; i < n; ) {
25 // Skip past leading whitespace
26 while ((i < n) && isspace(value.charAt(i))) {
32 while ((i < n) && !isspace(value.charAt(i))) {
37 output.emit(value.substring(start, i), "1");
42 public void reduce(String key, Vector values, OutputCollector output) {
43 // Iterate over all entries with the
44 // // same key and add the values
46 for(int i = 0; i < values.size(); ++i) {
47 value += Integer.parseInt((String)values.elementAt(i));
50 // Emit sum for input->key()
51 output.emit(key, String.valueOf(value));
54 boolean isspace(char c) {
74 public class WordCounter /*implements*/extends Tool {
76 public WordCounter() {}
78 static int printUsage() {
79 System./*out.println*/printString("<conffile>\n");
84 * The main driver for word count map/reduce program.
85 * Invoke this method to submit the map/reduce job.
86 * @throws IOException When there is communication problems with the
89 public int run(String[] args) {
91 MapReduceClass mapreducer = new MapReduceClass();
93 FileInputStream iStream = new FileInputStream(args[0]);
94 byte[] b = new byte[1024];
95 int length = iStream.read(b);
97 System./*out.println*/printString("Error! Can not read from configure file: " + args[0] + "\n");
100 String content = new String(b, 0, length);
101 //System.out.println(content);
102 int index = content.indexOf('\n');
103 String inputfile = content.substring(0, index);
104 content = content.substring(index + 1);
105 index = content.indexOf('\n');
106 int m = Integer.parseInt(content.substring(0, index));
107 content = content.substring(index + 1);
108 index = content.indexOf('\n');
109 int r = Integer.parseInt(content.substring(0, index));
110 content = content.substring(index + 1);
111 index = content.indexOf('\n');
112 String temp = content.substring(0, index);
113 char seperator = temp.charAt(0);
114 //System.out.println(inputfile + "; " + String.valueOf(m) + "; " + String.valueOf(r));
116 Configuration conf = new Configuration();
117 conf.setMapReduceClass(mapreducer);
118 conf.setInputfile(inputfile);
121 conf.setSeperator(seperator);
123 JobClient.runJob(conf);
124 /*} catch (Exception e) {
133 public static void main(String[] args) /*throws Exception*/ {
134 int res = ToolRunner.run(new WordCounter(), args);