1
|
package eu.dnetlib.data.mapreduce.hbase.broker.enrich;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.Map;
|
6
|
|
7
|
import com.google.common.base.Function;
|
8
|
import com.google.common.base.Predicate;
|
9
|
import com.google.common.collect.Iterables;
|
10
|
import com.google.common.collect.Lists;
|
11
|
import eu.dnetlib.data.mapreduce.util.DedupUtils;
|
12
|
import eu.dnetlib.data.mapreduce.util.OafDecoder;
|
13
|
import eu.dnetlib.data.mapreduce.util.OafRowKeyDecoder;
|
14
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
15
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
16
|
import org.apache.commons.collections.MapUtils;
|
17
|
import org.apache.hadoop.hbase.client.Result;
|
18
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
19
|
import org.apache.hadoop.hbase.util.Bytes;
|
20
|
|
21
|
/**
|
22
|
* Created by claudio on 08/07/16.
|
23
|
*/
|
24
|
public class ProjectEnrichmentMapper extends AbstractEnrichmentMapper {
|
25
|
|
26
|
@Override
|
27
|
protected String counterGroup() {
|
28
|
return "Broker Enrichment projects";
|
29
|
}
|
30
|
|
31
|
@Override
|
32
|
protected void map(final ImmutableBytesWritable key, final Result value, final Context context) throws IOException, InterruptedException {
|
33
|
|
34
|
final Type type = getEntityType(key);
|
35
|
|
36
|
final byte[] body = value.getValue(Bytes.toBytes(type.toString()), Bytes.toBytes("body"));
|
37
|
|
38
|
if (body == null) {
|
39
|
context.getCounter(counterGroup(), "missing metadata").increment(1);
|
40
|
return;
|
41
|
}
|
42
|
|
43
|
switch (type) {
|
44
|
case project:
|
45
|
for (final byte[] resultId : listRelatedIds(value, "resultProject_outcome_produces")) {
|
46
|
emit(context, resultId, body, "project");
|
47
|
}
|
48
|
break;
|
49
|
case result:
|
50
|
|
51
|
final Oaf.Builder oafBuilder = Oaf.newBuilder(OafDecoder.decode(body).getOaf());
|
52
|
for (final String relName : Arrays.asList("resultProject_outcome_isProducedBy")) { // Â TODO add dataset rels
|
53
|
for (final Oaf rel : listRelations(value, relName)) {
|
54
|
oafBuilder.getEntityBuilder().addCachedOafRel(rel);
|
55
|
context.getCounter(counterGroup(), "rel: " + relName).increment(1);
|
56
|
}
|
57
|
}
|
58
|
|
59
|
final Map<byte[], byte[]> mergedIn = value.getFamilyMap(DedupUtils.getDedupCF_mergedInBytes(Type.result));
|
60
|
|
61
|
if (MapUtils.isEmpty(mergedIn) & !DedupUtils.isRoot(key)) {
|
62
|
emit(context, key.copyBytes(), oafBuilder.build().toByteArray(), "result not deduped");
|
63
|
} else if (DedupUtils.isRoot(key)) {
|
64
|
emit(context, key.copyBytes(), oafBuilder.build().toByteArray(), "result merges");
|
65
|
} else {
|
66
|
emit(context, getRootId(mergedIn), oafBuilder.build().toByteArray(), "result mergedIn");
|
67
|
}
|
68
|
|
69
|
break;
|
70
|
default:
|
71
|
throw new IllegalArgumentException("invalid type: " + type);
|
72
|
}
|
73
|
}
|
74
|
|
75
|
private Iterable<Oaf> listRelations(final Result value, final String relType) {
|
76
|
|
77
|
//TODO consider only relationshipt not deletedbyinference
|
78
|
|
79
|
final Map<byte[], byte[]> map = value.getFamilyMap(Bytes.toBytes(relType));
|
80
|
if (MapUtils.isNotEmpty(map)) {
|
81
|
return Iterables.filter(Iterables.transform(map.values(), new Function<byte[], Oaf>() {
|
82
|
|
83
|
@Override
|
84
|
public Oaf apply(final byte[] input) {
|
85
|
return OafDecoder.decode(input).getOaf();
|
86
|
}
|
87
|
}), new Predicate<Oaf>() {
|
88
|
@Override
|
89
|
public boolean apply(final Oaf rel) {
|
90
|
return !rel.getRel().getTarget().contains("unidentified");
|
91
|
}
|
92
|
});
|
93
|
} else {
|
94
|
return Lists.newArrayList();
|
95
|
}
|
96
|
}
|
97
|
|
98
|
private Iterable<byte[]> listRelatedIds(final Result value, final String relType) {
|
99
|
|
100
|
//TODO consider only relationshipt not deletedbyinference
|
101
|
|
102
|
final Map<byte[], byte[]> map = value.getFamilyMap(Bytes.toBytes(relType));
|
103
|
if (MapUtils.isNotEmpty(map)) {
|
104
|
return map.keySet();
|
105
|
} else {
|
106
|
return Lists.newArrayList();
|
107
|
}
|
108
|
}
|
109
|
|
110
|
private Type getEntityType(final ImmutableBytesWritable key) {
|
111
|
return OafRowKeyDecoder.decode(key.copyBytes()).getType();
|
112
|
}
|
113
|
|
114
|
private byte[] getRootId(final Map<byte[], byte[]> mergedIn) {
|
115
|
return Iterables.getOnlyElement(mergedIn.keySet());
|
116
|
}
|
117
|
|
118
|
}
|