1
|
package eu.dnetlib.data.mapreduce.hbase.dedup;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Collections;
|
5
|
import java.util.HashSet;
|
6
|
import java.util.Map;
|
7
|
import java.util.Set;
|
8
|
import java.util.stream.Collectors;
|
9
|
|
10
|
import com.google.protobuf.InvalidProtocolBufferException;
|
11
|
import eu.dnetlib.data.mapreduce.JobParams;
|
12
|
import eu.dnetlib.data.mapreduce.util.DedupUtils;
|
13
|
import eu.dnetlib.data.mapreduce.util.UpdateMerger;
|
14
|
import eu.dnetlib.data.proto.KindProtos.Kind;
|
15
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
16
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
17
|
import eu.dnetlib.data.transform.OafUtils;
|
18
|
import eu.dnetlib.pace.config.DedupConfig;
|
19
|
import org.apache.commons.logging.Log;
|
20
|
import org.apache.commons.logging.LogFactory;
|
21
|
import org.apache.hadoop.hbase.client.Result;
|
22
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
23
|
import org.apache.hadoop.hbase.mapreduce.TableMapper;
|
24
|
import org.apache.hadoop.hbase.util.Bytes;
|
25
|
import org.apache.hadoop.io.Text;
|
26
|
|
27
|
public class DedupBuildRootsMapper extends TableMapper<Text, ImmutableBytesWritable> {
|
28
|
|
29
|
private static final Log log = LogFactory.getLog(DedupBuildRootsMapper.class);
|
30
|
|
31
|
private DedupConfig dedupConf;
|
32
|
|
33
|
private ImmutableBytesWritable ibw;
|
34
|
|
35
|
private Set<String> entityNames;
|
36
|
|
37
|
@Override
|
38
|
protected void setup(final Context context) {
|
39
|
dedupConf = DedupConfig.load(context.getConfiguration().get(JobParams.DEDUP_CONF));
|
40
|
System.out.println("dedup buildRoots mapper\nwf conf: " + dedupConf.toString());
|
41
|
|
42
|
entityNames = OafUtils.entities();
|
43
|
ibw = new ImmutableBytesWritable();
|
44
|
}
|
45
|
|
46
|
@Override
|
47
|
protected void map(final ImmutableBytesWritable rowkey, final Result value, final Context context) throws IOException, InterruptedException {
|
48
|
// System.out.println("Find root mapping: " + new String(rowkey.copyBytes()));
|
49
|
|
50
|
if (isInvalid(value)) {
|
51
|
context.getCounter(dedupConf.getWf().getEntityType(), "not valid").increment(1);
|
52
|
return;
|
53
|
}
|
54
|
|
55
|
final Map<byte[], byte[]> mergedIn = value.getFamilyMap(DedupUtils.getDedupCF_mergedInBytes(Type.valueOf(dedupConf.getWf().getEntityType())));
|
56
|
|
57
|
if ((mergedIn != null) && !mergedIn.isEmpty()) {
|
58
|
|
59
|
final Set<String> ids = getStrings(mergedIn);
|
60
|
|
61
|
if (ids.size() > 1) {
|
62
|
context.getCounter(dedupConf.getWf().getEntityType(), "mergedIn > 1").increment(1);
|
63
|
}
|
64
|
final Text rootId = new Text(Collections.min(ids));
|
65
|
|
66
|
// byte[] rootIdBytes = rootId.copyBytes();
|
67
|
// byte[] rowkeyBytes = rowkey.copyBytes();
|
68
|
|
69
|
context.getCounter(dedupConf.getWf().getEntityType(), "merged").increment(1);
|
70
|
for (final String family : dedupConf.getWf().getRootBuilder()) {
|
71
|
|
72
|
// if (checkHack(rowkeyBytes, rootIdBytes, family)) {
|
73
|
// context.getCounter("hack", "personResult skipped").increment(1);
|
74
|
// } else {
|
75
|
|
76
|
final Map<byte[], byte[]> map = value.getFamilyMap(Bytes.toBytes(family));
|
77
|
if ((map != null) && !map.isEmpty()) {
|
78
|
|
79
|
// if we're dealing with the entity CF
|
80
|
if (entityNames.contains(family)) {
|
81
|
final Oaf body = UpdateMerger.mergeBodyUpdates(context, map);
|
82
|
|
83
|
emit(context, rootId, body.toByteArray());
|
84
|
} else {
|
85
|
for (final byte[] o : map.values()) {
|
86
|
|
87
|
if (!isRelMarkedDeleted(context, o)) {
|
88
|
emit(context, rootId, o);
|
89
|
} else {
|
90
|
context.getCounter(family, "rel marked deleted").increment(1);
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
} // else {
|
95
|
// System.err.println("empty family: " + family + "\nkey: " + sKey);
|
96
|
// context.getCounter("DedupBuildRootsMapper", "empty family '" + family + "'").increment(1);
|
97
|
// }
|
98
|
// }
|
99
|
}
|
100
|
} else {
|
101
|
//context.getCounter(dedupConf.getWf().getEntityType(), "not in duplicate group").increment(1);
|
102
|
|
103
|
final Map<byte[], byte[]> mergesRels = value.getFamilyMap(DedupUtils.getDedupCF_mergesBytes(Type.valueOf(dedupConf.getWf().getEntityType())));
|
104
|
if (mergesRels != null && !mergesRels.isEmpty()) {
|
105
|
final byte[] body = value.getValue(dedupConf.getWf().getEntityType().getBytes(), DedupUtils.BODY_B);
|
106
|
if (body != null) {
|
107
|
|
108
|
context.getCounter(dedupConf.getWf().getEntityType(), "root").increment(1);
|
109
|
emit(context, new Text(rowkey.copyBytes()), body);
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
|
114
|
}
|
115
|
|
116
|
private HashSet<String> getStrings(final Map<byte[], byte[]> mergedIn) {
|
117
|
return mergedIn.keySet().stream()
|
118
|
.map(b -> new String(b))
|
119
|
.collect(Collectors.toCollection(HashSet::new));
|
120
|
}
|
121
|
|
122
|
private boolean isInvalid(final Result value) {
|
123
|
final Map<byte[], byte[]> bodyMap = value.getFamilyMap(dedupConf.getWf().getEntityType().getBytes());
|
124
|
|
125
|
if ((bodyMap == null) || bodyMap.isEmpty()) return true;
|
126
|
|
127
|
final byte[] bodyB = bodyMap.get(DedupUtils.BODY_B);
|
128
|
|
129
|
if (bodyB == null) return true;
|
130
|
|
131
|
return false;
|
132
|
}
|
133
|
|
134
|
private void emit(final Context context, final Text rootId, final byte[] value) throws IOException, InterruptedException {
|
135
|
ibw.set(value);
|
136
|
context.write(rootId, ibw);
|
137
|
}
|
138
|
|
139
|
private boolean isRelMarkedDeleted(final Context context, final byte[] o) {
|
140
|
try {
|
141
|
final Oaf oaf = Oaf.parseFrom(o);
|
142
|
return oaf.getKind().equals(Kind.relation) && oaf.getDataInfo().getDeletedbyinference();
|
143
|
} catch (final InvalidProtocolBufferException e) {
|
144
|
context.getCounter("error", e.getClass().getName()).increment(1);
|
145
|
return true;
|
146
|
}
|
147
|
}
|
148
|
|
149
|
}
|