start of new file
[IRC.git] / Robust / src / Benchmarks / MapReduce / Java / WordCounter.java
1
2 //import java.io.FileInputStream;
3 //import java.util.Vector;
4
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;*/
12
13 /**
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>).
17      */
18     public class MapReduceClass extends MapReduceBase {
19         
20         public MapReduceClass() {}
21
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))) {
27                     ++i;
28                 }
29
30                 // Find word end
31                 int start = i;
32                 while ((i < n) && !isspace(value.charAt(i))) {
33                     i++;
34                 }
35
36                 if (start < i) {
37                     output.emit(value.substring(start, i), "1");
38                 }
39             }
40         }
41
42         public void reduce(String key, Vector values, OutputCollector output) {
43             // Iterate over all entries with the
44             // // same key and add the values
45             int value = 0;
46             for(int i = 0; i < values.size(); ++i) {
47                 value += Integer.parseInt((String)values.elementAt(i));
48             }
49
50             // Emit sum for input->key()
51             output.emit(key, String.valueOf(value));
52         }
53
54         boolean isspace(char c) {
55         if((c == ' ') || 
56                 (c == ',') ||
57                 (c == '.') ||
58                 (c == '!') ||
59                 (c == '?') ||
60                 (c == '"') ||
61                 (c == '(') ||
62                 (c == ')') ||
63                 (c == '[') ||
64                 (c == ']') ||
65                 (c == '{') ||
66                 (c == '}') ||
67                 (c == '\n')) {
68             return true;
69         }
70         return false;
71         }
72     }
73
74 public class WordCounter /*implements*/extends Tool {
75
76         public WordCounter() {}
77
78     static int printUsage() {
79         System./*out.println*/printString("<conffile>\n");
80         return -1;
81     }
82
83     /**
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 
87      *                     job tracker.
88      */
89     public int run(String[] args) {
90         //try {
91             MapReduceClass mapreducer = new MapReduceClass();       
92
93             FileInputStream iStream = new FileInputStream(args[0]);
94             byte[] b = new byte[1024];
95             int length = iStream.read(b);
96             if(length < 0 ) {
97                 System./*out.println*/printString("Error! Can not read from configure file: " + args[0] + "\n");
98                 System.exit(-1);
99             }
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));
115             
116             Configuration conf = new Configuration();
117             conf.setMapReduceClass(mapreducer);
118             conf.setInputfile(inputfile);
119             conf.setM(m);
120             conf.setR(r);
121             conf.setSeperator(seperator);
122
123             JobClient.runJob(conf);
124         /*} catch (Exception e) {
125             e.printStackTrace();
126             System.exit(-1);
127         }*/
128         
129         return 0;
130     }
131
132
133     public static void main(String[] args) /*throws Exception*/ {
134         int res = ToolRunner.run(new WordCounter(), args);
135         System.exit(res);
136     }
137
138 }