1
|
package eu.dnetlib.data.mapreduce.hbase.propagation.communitytoresult;
|
2
|
|
3
|
import eu.dnetlib.data.mapreduce.hbase.propagation.Utils;
|
4
|
import eu.dnetlib.data.mapreduce.hbase.propagation.Value;
|
5
|
|
6
|
import eu.dnetlib.data.proto.*;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.apache.hadoop.hbase.client.Put;
|
10
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
11
|
import org.apache.hadoop.hbase.mapreduce.TableReducer;
|
12
|
import org.apache.hadoop.hbase.util.Bytes;
|
13
|
import org.apache.hadoop.io.Text;
|
14
|
|
15
|
import java.io.IOException;
|
16
|
|
17
|
import java.util.HashSet;
|
18
|
import java.util.Iterator;
|
19
|
import java.util.Set;
|
20
|
|
21
|
import static eu.dnetlib.data.mapreduce.hbase.propagation.PropagationConstants.*;
|
22
|
|
23
|
public class CommunityToResultReducer extends TableReducer<Text, Text, ImmutableBytesWritable> {
|
24
|
private static final Log log = LogFactory.getLog(CommunityToResultReducer.class); // NOPMD by marko on 11/24/08 5:02 PM
|
25
|
private ImmutableBytesWritable keyOut;
|
26
|
|
27
|
@Override
|
28
|
protected void setup(final Context context) throws IOException, InterruptedException {
|
29
|
super.setup(context);
|
30
|
keyOut = new ImmutableBytesWritable();
|
31
|
}
|
32
|
|
33
|
@Override
|
34
|
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
|
35
|
Iterator<Text> it = values.iterator();
|
36
|
final ResultProtos.Result.Metadata.Builder metadata = ResultProtos.Result.Metadata.newBuilder();
|
37
|
Set<String> added_context = new HashSet<>();
|
38
|
while(it.hasNext()){
|
39
|
Value v = Value.fromJson(it.next().toString());
|
40
|
if (!added_context.contains(v.getValue())) {
|
41
|
metadata.addContext(Utils.getContext(v.getValue(), v.getTrust(), CLASS_COMMUNITY_ID, DATA_INFO_TYPE, CLASS_COMMUNITY_NAME));
|
42
|
added_context.add(v.getValue());
|
43
|
}
|
44
|
}
|
45
|
|
46
|
final String rowKey = key.toString();
|
47
|
|
48
|
final Put put = new Put(Bytes.toBytes(rowKey)).add(Bytes.toBytes("result"), Bytes.toBytes("update_" + System.nanoTime()), Utils.getUpdate(metadata, rowKey).toByteArray());
|
49
|
keyOut.set(Bytes.toBytes(rowKey));
|
50
|
context.write(keyOut, put);
|
51
|
context.getCounter(COUNTER_PROPAGATION, "added community to result").increment(1);
|
52
|
|
53
|
}
|
54
|
|
55
|
}
|
56
|
|