1
|
package eu.dnetlib.data.mapreduce.hbase.broker.enrich;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
import java.util.stream.Collectors;
|
7
|
|
8
|
import com.google.common.collect.Lists;
|
9
|
import com.google.common.collect.Maps;
|
10
|
import com.google.common.collect.Streams;
|
11
|
import eu.dnetlib.data.mapreduce.hbase.broker.SoftwareEventFactory;
|
12
|
import eu.dnetlib.data.mapreduce.hbase.broker.model.EventWrapper;
|
13
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
14
|
import org.apache.commons.lang.StringUtils;
|
15
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
16
|
import org.dom4j.DocumentException;
|
17
|
|
18
|
import static eu.dnetlib.data.mapreduce.hbase.broker.enrich.SoftwareEnrichmentMapper.PUBLICATION;
|
19
|
import static eu.dnetlib.data.mapreduce.hbase.broker.enrich.SoftwareEnrichmentMapper.SOFTWARE;
|
20
|
import static eu.dnetlib.data.mapreduce.util.OafHbaseUtils.getKey;
|
21
|
|
22
|
/**
|
23
|
* Created by claudio on 08/07/16.
|
24
|
*/
|
25
|
public class SoftwareEnrichmentReducer extends AbstractEnrichmentReducer {
|
26
|
|
27
|
@Override
|
28
|
protected String counterGroup() {
|
29
|
return "Broker Enrichment Software";
|
30
|
}
|
31
|
|
32
|
@Override
|
33
|
protected void reduce(final ImmutableBytesWritable key, final Iterable<ImmutableBytesWritable> values, final Context context)
|
34
|
throws IOException, InterruptedException {
|
35
|
|
36
|
final Map<String, Oaf> software = Maps.newHashMap();
|
37
|
final List<Oaf> pubs = Lists.newArrayList();
|
38
|
|
39
|
Streams.stream(values).limit(LIMIT).map(EnrichmentReducer::toOaf).forEach(oaf -> {
|
40
|
switch (oaf.getEntity().getResult().getMetadata().getResulttype().getClassid()) {
|
41
|
case PUBLICATION:
|
42
|
pubs.add(oaf);
|
43
|
break;
|
44
|
case SOFTWARE:
|
45
|
software.put(oaf.getEntity().getId(), oaf);
|
46
|
break;
|
47
|
default:
|
48
|
throw new IllegalArgumentException("invalid type: " + oaf.getEntity().getType());
|
49
|
}
|
50
|
});
|
51
|
|
52
|
if (software.isEmpty() || pubs.isEmpty()) {
|
53
|
context.getCounter(counterGroup(), "Events Skipped - Nothing to do").increment(1);
|
54
|
return;
|
55
|
}
|
56
|
|
57
|
try {
|
58
|
generateEvents(pubs, software, context);
|
59
|
} catch (final DocumentException e) {
|
60
|
throw new RuntimeException(e);
|
61
|
}
|
62
|
}
|
63
|
|
64
|
private void generateEvents(final List<Oaf> pubs, final Map<String, Oaf> software, final Context context)
|
65
|
throws IOException, InterruptedException, DocumentException {
|
66
|
|
67
|
final List<Oaf> valid = pubs.stream()
|
68
|
.map(p -> addSoftware(p, software))
|
69
|
.filter(r -> !r.getEntity().getCachedOafRelList().isEmpty())
|
70
|
.collect(Collectors.toList());
|
71
|
|
72
|
if (valid.isEmpty()) {
|
73
|
context.getCounter(counterGroup(), "Events Skipped - Missing software").increment(1);
|
74
|
return;
|
75
|
}
|
76
|
|
77
|
for (final Oaf current : valid) {
|
78
|
|
79
|
final String currentId = current.getEntity().getId();
|
80
|
|
81
|
final String currentDsId = StringUtils.substringAfter(getKey(current.getEntity().getCollectedfromList()), "|");
|
82
|
final String currentDsType = dsTypeMap.get(currentDsId);
|
83
|
|
84
|
// System.out.println(String.format("'%s' -> '%s'", currentDsId, currentDsType));
|
85
|
|
86
|
if (StringUtils.isBlank(currentDsType) && !dsWhitelist.contains(currentDsId)) {
|
87
|
context.getCounter(counterGroup(), "Events Skipped - Datasource type excluded").increment(1);
|
88
|
} else if (dsBlacklist.contains(currentDsId)) {
|
89
|
context.getCounter(counterGroup(), "Events Skipped - Datasource blacklisted").increment(1);
|
90
|
} else {
|
91
|
final List<EventWrapper> events = Lists.newArrayList();
|
92
|
|
93
|
if (valid.size() == 1) {
|
94
|
events.addAll(SoftwareEventFactory.process(context, valid.get(0), baseUrlMap));
|
95
|
} else {
|
96
|
|
97
|
for (final Oaf other : valid) {
|
98
|
final String otherId = other.getEntity().getId();
|
99
|
if (!currentId.equals(otherId)) {
|
100
|
|
101
|
final double similarity = similarity(current, other);
|
102
|
|
103
|
if (similarity >= dedupConf.getWf().getThreshold()) {
|
104
|
|
105
|
final float trust = scale(similarity);
|
106
|
events.addAll(SoftwareEventFactory.process(context, current, other, trust, baseUrlMap));
|
107
|
} else {
|
108
|
context.getCounter(counterGroup(), "d < " + dedupConf.getWf().getThreshold()).increment(1);
|
109
|
}
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
emit(events, context);
|
114
|
}
|
115
|
}
|
116
|
|
117
|
}
|
118
|
|
119
|
private Oaf addSoftware(final Oaf current, final Map<String, Oaf> softwareMap) {
|
120
|
|
121
|
final Oaf.Builder oafBuilder = Oaf.newBuilder(current);
|
122
|
final List<Oaf> cachedRels = Lists.newArrayList();
|
123
|
|
124
|
for (final Oaf.Builder cachedOafRel : oafBuilder.getEntityBuilder().getCachedOafRelBuilderList()) {
|
125
|
final String softwareId = cachedOafRel.getRel().getTarget();
|
126
|
|
127
|
if (softwareMap.containsKey(softwareId)) {
|
128
|
final Oaf software = softwareMap.get(softwareId);
|
129
|
|
130
|
cachedOafRel.getRelBuilder().setCachedOafTarget(software);
|
131
|
cachedRels.add(cachedOafRel.build());
|
132
|
}
|
133
|
}
|
134
|
|
135
|
oafBuilder.getEntityBuilder().clearCachedOafRel().addAllCachedOafRel(cachedRels);
|
136
|
|
137
|
return oafBuilder.build();
|
138
|
}
|
139
|
|
140
|
}
|