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