Clover Coverage Report - contrib
Coverage timestamp: Fri Apr 27 2012 21:25:11 UTC
../../../../../../img/srcFileCovDistChart9.png 31% of files have more coverage
46   210   31   2
12   142   0.67   4.6
23     1.35  
5    
 
  GenericMR       Line # 51 14 8 82.6% 0.82608694
  GenericMR.RecordProcessor       Line # 99 0 0 - -1.0
  GenericMR.KeyRecordIterator       Line # 103 7 5 84.6% 0.84615386
  GenericMR.RecordReader       Line # 132 13 9 86.4% 0.8636364
  GenericMR.OutputStreamOutput       Line # 176 12 9 82.6% 0.82608694
 
  (6)
 
1    /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18    package org.apache.hadoop.hive.contrib.mr;
19   
20    import java.io.BufferedReader;
21    import java.io.InputStream;
22    import java.io.InputStreamReader;
23    import java.io.OutputStream;
24    import java.io.OutputStreamWriter;
25    import java.io.PrintWriter;
26    import java.io.Reader;
27    import java.io.Writer;
28    import java.util.Iterator;
29    import java.util.NoSuchElementException;
30   
31    /**
32    * This class attempts to provide a simple framework for writing Hive map/reduce
33    * tasks in java.
34    *
35    * The main benefit is that it deals with grouping the keys together for reduce
36    * tasks.
37    *
38    * Additionally, it deals with all system io... and provides something closer to
39    * the hadoop m/r.
40    *
41    * As an example, here's the wordcount reduce:
42    *
43    * new GenericMR().reduce(System.in, System.out, new Reducer() { public void
44    * reduce(String key, Iterator<String[]> records, Output output) throws
45    * Exception { int count = 0;
46    *
47    * while (records.hasNext()) { count += Integer.parseInt(records.next()[1]); }
48    *
49    * output.collect(new String[] { key, String.valueOf(count) }); }});
50    */
 
51    public final class GenericMR {
 
52  0 toggle public void map(final InputStream in, final OutputStream out,
53    final Mapper mapper) throws Exception {
54  0 map(new InputStreamReader(in), new OutputStreamWriter(out), mapper);
55    }
56   
 
57  3 toggle public void map(final Reader in, final Writer out, final Mapper mapper) throws Exception {
58  3 handle(in, out, new RecordProcessor() {
 
59  4 toggle @Override
60    public void processNext(RecordReader reader, Output output) throws Exception {
61  4 mapper.map(reader.next(), output);
62    }
63    });
64    }
65   
 
66  0 toggle public void reduce(final InputStream in, final OutputStream out,
67    final Reducer reducer) throws Exception {
68  0 reduce(new InputStreamReader(in), new OutputStreamWriter(out), reducer);
69    }
70   
 
71  3 toggle public void reduce(final Reader in, final Writer out, final Reducer reducer) throws Exception {
72  3 handle(in, out, new RecordProcessor() {
 
73  5 toggle @Override
74    public void processNext(RecordReader reader, Output output) throws Exception {
75  5 reducer.reduce(reader.peek()[0], new KeyRecordIterator(
76    reader.peek()[0], reader), output);
77    }
78    });
79    }
80   
 
81  6 toggle private void handle(final Reader in, final Writer out,
82    final RecordProcessor processor) throws Exception {
83  6 final RecordReader reader = new RecordReader(in);
84  6 final OutputStreamOutput output = new OutputStreamOutput(out);
85   
86  6 try {
87  14 while (reader.hasNext()) {
88  9 processor.processNext(reader, output);
89    }
90    } finally {
91  6 try {
92  6 output.close();
93    } finally {
94  6 reader.close();
95    }
96    }
97    }
98   
 
99    private static interface RecordProcessor {
100    void processNext(final RecordReader reader, final Output output) throws Exception;
101    }
102   
 
103    private static final class KeyRecordIterator implements Iterator<String[]> {
104    private final String key;
105    private final RecordReader reader;
106   
 
107  5 toggle private KeyRecordIterator(final String key, final RecordReader reader) {
108  5 this.key = key;
109  5 this.reader = reader;
110    }
111   
 
112  20 toggle @Override
113    public boolean hasNext() {
114  20 return (reader.hasNext() && key.equals(reader.peek()[0]));
115    }
116   
 
117  9 toggle @Override
118    public String[] next() {
119  9 if (!hasNext()) {
120  1 throw new NoSuchElementException();
121    }
122   
123  8 return reader.next();
124    }
125   
 
126  0 toggle @Override
127    public void remove() {
128  0 throw new UnsupportedOperationException();
129    }
130    }
131   
 
132    private static final class RecordReader {
133    private final BufferedReader reader;
134    private String[] next;
135   
 
136  0 toggle private RecordReader(final InputStream in) {
137  0 this(new InputStreamReader(in));
138    }
139   
 
140  6 toggle private RecordReader(final Reader in) {
141  6 reader = new BufferedReader(in);
142  6 next = readNext();
143    }
144   
 
145  12 toggle private String[] next() {
146  12 final String[] ret = next;
147   
148  12 next = readNext();
149   
150  12 return ret;
151    }
152   
 
153  18 toggle private String[] readNext() {
154  18 try {
155  18 final String line = reader.readLine();
156  18 return (line == null ? null : line.split("\t"));
157    } catch (final Exception e) {
158  0 throw new RuntimeException(e);
159    }
160    }
161   
 
162  34 toggle private boolean hasNext() {
163  34 return next != null;
164    }
165   
 
166  27 toggle private String[] peek() {
167  27 return next;
168    }
169   
 
170  6 toggle private void close() throws Exception {
171  6 reader.close();
172   
173    }
174    }
175   
 
176    private static final class OutputStreamOutput implements Output {
177    private final PrintWriter out;
178   
 
179  0 toggle private OutputStreamOutput(final OutputStream out) {
180  0 this(new OutputStreamWriter(out));
181    }
182   
 
183  6 toggle private OutputStreamOutput(final Writer out) {
184  6 this.out = new PrintWriter(out);
185    }
186   
 
187  6 toggle public void close() throws Exception {
188  6 out.close();
189    }
190   
 
191  10 toggle @Override
192    public void collect(String[] record) throws Exception {
193  10 out.println(_join(record, "\t"));
194    }
195   
 
196  10 toggle private static String _join(final String[] record, final String separator) {
197  10 if (record == null || record.length == 0) {
198  0 return "";
199    }
200  10 final StringBuilder sb = new StringBuilder();
201  30 for (int i = 0; i < record.length; i++) {
202  20 if (i > 0) {
203  10 sb.append(separator);
204    }
205  20 sb.append(record[i]);
206    }
207  10 return sb.toString();
208    }
209    }
210    }