1 |
41684
|
eri.katsar
|
package eu.dnetlib.data.mapreduce.hbase.statsExport.eu.dnetlib.data.mapreduce.statsExport.mapreduce;
|
2 |
39854
|
eri.katsar
|
|
3 |
|
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
4 |
|
|
import org.apache.hadoop.io.Text;
|
5 |
|
|
import org.apache.hadoop.mapreduce.Reducer;
|
6 |
|
|
import org.apache.hadoop.mapreduce.TaskInputOutputContext;
|
7 |
|
|
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
|
8 |
|
|
import org.apache.log4j.Logger;
|
9 |
|
|
|
10 |
|
|
import java.io.IOException;
|
11 |
|
|
import java.nio.charset.Charset;
|
12 |
|
|
import java.util.Iterator;
|
13 |
|
|
|
14 |
|
|
public class StatsReducer extends Reducer<Text, ImmutableBytesWritable, Text, Text> {
|
15 |
|
|
|
16 |
41383
|
eri.katsar
|
private Logger log = Logger.getLogger(StatsReducer.class);
|
17 |
|
|
private MultipleOutputs MultipleOutputWriter;
|
18 |
39854
|
eri.katsar
|
|
19 |
41383
|
eri.katsar
|
/**
|
20 |
|
|
* Reducer that splits input according to their Type ( datasource, results
|
21 |
|
|
* etc..) and writes each kind to a seperate output
|
22 |
|
|
*/
|
23 |
|
|
@Override
|
24 |
|
|
protected void setup(Context context) throws IOException, InterruptedException {
|
25 |
39854
|
eri.katsar
|
|
26 |
41383
|
eri.katsar
|
MultipleOutputWriter = new MultipleOutputs((TaskInputOutputContext) context);
|
27 |
|
|
};
|
28 |
39854
|
eri.katsar
|
|
29 |
41383
|
eri.katsar
|
@Override
|
30 |
|
|
protected void reduce(final Text key, final Iterable<ImmutableBytesWritable> values, final Context context) throws IOException, InterruptedException {
|
31 |
41121
|
eri.katsar
|
|
32 |
41383
|
eri.katsar
|
Iterator<ImmutableBytesWritable> it = values.iterator();
|
33 |
|
|
while (it.hasNext()) {
|
34 |
|
|
String[] split = key.toString().split(",");
|
35 |
|
|
String type = split[0];
|
36 |
|
|
String id = split[1];
|
37 |
|
|
String value = new String(it.next().copyBytes(),Charset.forName("UTF-8"));
|
38 |
|
|
value = value.trim();
|
39 |
|
|
MultipleOutputWriter.write(type, new Text(id.getBytes(Charset.forName("UTF-8"))), new Text(value.getBytes(Charset.forName("UTF-8"))), type.toString());
|
40 |
39854
|
eri.katsar
|
|
41 |
41383
|
eri.katsar
|
}
|
42 |
41121
|
eri.katsar
|
|
43 |
41383
|
eri.katsar
|
}
|
44 |
41121
|
eri.katsar
|
|
45 |
41383
|
eri.katsar
|
@Override
|
46 |
|
|
protected void cleanup(Context context) throws IOException, InterruptedException {
|
47 |
|
|
log.info("Cleaning up reducer...");
|
48 |
|
|
MultipleOutputWriter.close();
|
49 |
|
|
}
|
50 |
39854
|
eri.katsar
|
|
51 |
|
|
|
52 |
41383
|
eri.katsar
|
}
|