1
|
package eu.dnetlib.data.mapreduce.hbase.dedup;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.HashSet;
|
5
|
import java.util.LinkedList;
|
6
|
import java.util.Map;
|
7
|
import java.util.Queue;
|
8
|
import java.util.Set;
|
9
|
|
10
|
import eu.dnetlib.data.mapreduce.JobParams;
|
11
|
import org.apache.hadoop.hbase.client.Durability;
|
12
|
import org.apache.hadoop.hbase.client.Put;
|
13
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
14
|
import org.apache.hadoop.hbase.mapreduce.TableReducer;
|
15
|
import org.apache.hadoop.hbase.util.Bytes;
|
16
|
import org.apache.hadoop.io.Text;
|
17
|
|
18
|
import com.google.common.collect.Maps;
|
19
|
import com.google.common.collect.Sets;
|
20
|
|
21
|
import eu.dnetlib.data.mapreduce.hbase.VolatileColumnFamily;
|
22
|
import eu.dnetlib.pace.model.PersonComparatorUtils;
|
23
|
|
24
|
public class FindDedupCandidatePersonsReducer extends TableReducer<Text, Text, ImmutableBytesWritable> {
|
25
|
|
26
|
private static final int LIMIT = 5000;
|
27
|
|
28
|
@Override
|
29
|
protected void setup(final Context context) throws IOException, InterruptedException {
|
30
|
|
31
|
}
|
32
|
|
33
|
@Override
|
34
|
protected void reduce(final Text key, final Iterable<Text> values, final Context context) throws IOException, InterruptedException {
|
35
|
System.out.println("\nReducing key: " + key);
|
36
|
|
37
|
final Set<String> candidates = Sets.newHashSet();
|
38
|
final Map<String, Set<String>> resultIds = Maps.newHashMap();
|
39
|
|
40
|
final Queue<DedupPersonBean> queue = prepare(context, key, values);
|
41
|
while (!queue.isEmpty()) {
|
42
|
|
43
|
final DedupPersonBean pivot = queue.remove();
|
44
|
|
45
|
for (final DedupPersonBean curr : queue) {
|
46
|
|
47
|
if (PersonComparatorUtils.areSimilar(pivot.getName(), curr.getName())) {
|
48
|
System.out.println("- Similar persons: [" + pivot.getName() + "] - [" + curr.getName() + "]");
|
49
|
candidates.add(pivot.getId());
|
50
|
candidates.add(curr.getId());
|
51
|
|
52
|
collectResultIds(resultIds, pivot);
|
53
|
collectResultIds(resultIds, curr);
|
54
|
}
|
55
|
}
|
56
|
}
|
57
|
|
58
|
emitCandidates(context, candidates);
|
59
|
emitResultCandidates(context, resultIds);
|
60
|
}
|
61
|
|
62
|
private void collectResultIds(final Map<String, Set<String>> resultIds, final DedupPersonBean person) {
|
63
|
if (!resultIds.containsKey(person.getId())) {
|
64
|
resultIds.put(person.getId(), new HashSet<String>());
|
65
|
}
|
66
|
resultIds.get(person.getId()).addAll(person.getResults());
|
67
|
}
|
68
|
|
69
|
private Queue<DedupPersonBean> prepare(final Context context, final Text key, final Iterable<Text> values) {
|
70
|
final Queue<DedupPersonBean> queue = new LinkedList<DedupPersonBean>();
|
71
|
|
72
|
for (final Text i : values) {
|
73
|
queue.add(DedupPersonBean.fromText(i));
|
74
|
|
75
|
if (queue.size() > LIMIT) {
|
76
|
context.getCounter("Comparison list > " + LIMIT, "'" + key.toString() + "', --> " + context.getTaskAttemptID()).increment(1);
|
77
|
System.out.println("breaking out after limit (" + LIMIT + ") for key '" + key);
|
78
|
break;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
return queue;
|
83
|
}
|
84
|
|
85
|
private void emitCandidates(final Context context, final Set<String> candidates) throws IOException, InterruptedException {
|
86
|
final byte[] cf = Bytes.toBytes(VolatileColumnFamily.dedup.toString());
|
87
|
final byte[] col = Bytes.toBytes("isCandidate");
|
88
|
final byte[] val = Bytes.toBytes("");
|
89
|
|
90
|
for (final String s : candidates) {
|
91
|
final byte[] id = Bytes.toBytes(s);
|
92
|
final Put put = new Put(id).add(cf, col, val);
|
93
|
put.setWriteToWAL(JobParams.WRITE_TO_WAL);
|
94
|
context.write(new ImmutableBytesWritable(id), put);
|
95
|
}
|
96
|
context.getCounter(getClass().getSimpleName(), "N. Put. (persons)").increment(candidates.size());
|
97
|
}
|
98
|
|
99
|
private void emitResultCandidates(final Context context, final Map<String, Set<String>> resultIds) throws IOException, InterruptedException {
|
100
|
final byte[] cf = Bytes.toBytes(VolatileColumnFamily.dedupPerson.toString());
|
101
|
final byte[] val = Bytes.toBytes("");
|
102
|
|
103
|
for (final String personId : resultIds.keySet()) {
|
104
|
final byte[] col = Bytes.toBytes(personId);
|
105
|
|
106
|
for (final String s : resultIds.get(personId)) {
|
107
|
final byte[] id = Bytes.toBytes(s);
|
108
|
final Put put = new Put(id).add(cf, col, val);
|
109
|
put.setDurability(Durability.USE_DEFAULT);
|
110
|
context.write(new ImmutableBytesWritable(id), put);
|
111
|
}
|
112
|
context.getCounter(getClass().getSimpleName(), "N. Put. (results)").increment(resultIds.get(personId).size());
|
113
|
}
|
114
|
}
|
115
|
|
116
|
}
|