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
|
|
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 com.google.common.collect.Maps;
|
12
|
import eu.dnetlib.data.mapreduce.hbase.broker.ProjectEventFactory;
|
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.util.OafHbaseUtils.getKey;
|
19
|
|
20
|
/**
|
21
|
* Created by claudio on 08/07/16.
|
22
|
*/
|
23
|
public class ProjectEnrichmentReducer extends AbstractEnrichmentReducer {
|
24
|
|
25
|
@Override
|
26
|
protected String counterGroup() {
|
27
|
return "Broker Enrichment projects";
|
28
|
}
|
29
|
|
30
|
@Override
|
31
|
protected void reduce(final ImmutableBytesWritable key, final Iterable<ImmutableBytesWritable> values, final Context context)
|
32
|
throws IOException, InterruptedException {
|
33
|
|
34
|
final Map<String, Oaf> projects = Maps.newHashMap();
|
35
|
final List<Oaf> pubs = Lists.newArrayList();
|
36
|
|
37
|
for (final Oaf oaf : Iterables.transform(Iterables.limit(values, LIMIT), oafDeserialiser())) {
|
38
|
|
39
|
switch (oaf.getEntity().getType()) {
|
40
|
case result:
|
41
|
pubs.add(oaf);
|
42
|
break;
|
43
|
case project:
|
44
|
projects.put(oaf.getEntity().getId(), oaf);
|
45
|
break;
|
46
|
default:
|
47
|
throw new IllegalArgumentException("invalid type: " + oaf.getEntity().getType());
|
48
|
}
|
49
|
}
|
50
|
if (projects.isEmpty() || pubs.isEmpty()) {
|
51
|
context.getCounter(counterGroup(), "Events Skipped - Nothing to do").increment(1);
|
52
|
return;
|
53
|
}
|
54
|
|
55
|
try {
|
56
|
generateEvents(pubs, projects, context);
|
57
|
} catch (final DocumentException e) {
|
58
|
throw new RuntimeException(e);
|
59
|
}
|
60
|
}
|
61
|
|
62
|
private void generateEvents(final List<Oaf> pubs, final Map<String, Oaf> projects, final Context context)
|
63
|
throws IOException, InterruptedException, DocumentException {
|
64
|
|
65
|
final List<Oaf> enrichedOaf = Lists.newArrayList(Iterables.transform(pubs, new Function<Oaf, Oaf>() {
|
66
|
@Override
|
67
|
public Oaf apply(final Oaf pub) {
|
68
|
return addProjects(pub, projects);
|
69
|
}
|
70
|
}));
|
71
|
|
72
|
final List<Oaf> valid = Lists.newArrayList(Iterables.filter(enrichedOaf, new Predicate<Oaf>() {
|
73
|
@Override
|
74
|
public boolean apply(final Oaf result) {
|
75
|
return !result.getEntity().getCachedOafRelList().isEmpty();
|
76
|
}
|
77
|
}));
|
78
|
|
79
|
if (valid.isEmpty()) {
|
80
|
context.getCounter(counterGroup(), "Events Skipped - Missing project").increment(1);
|
81
|
return;
|
82
|
}
|
83
|
|
84
|
for (final Oaf current : valid) {
|
85
|
|
86
|
final String currentId = current.getEntity().getId();
|
87
|
|
88
|
final String currentDsId = StringUtils.substringAfter(getKey(current.getEntity().getCollectedfromList()), "|");
|
89
|
final String currentDsType = dsTypeMap.get(currentDsId);
|
90
|
|
91
|
// System.out.println(String.format("'%s' -> '%s'", currentDsId, currentDsType));
|
92
|
|
93
|
if (StringUtils.isBlank(currentDsType) && !dsWhitelist.contains(currentDsId)) {
|
94
|
context.getCounter(counterGroup(), "Events Skipped - Datasource type excluded").increment(1);
|
95
|
} else if (dsBlacklist.contains(currentDsId)) {
|
96
|
context.getCounter(counterGroup(), "Events Skipped - Datasource blacklisted").increment(1);
|
97
|
} else {
|
98
|
if (valid.size() == 1) {
|
99
|
ProjectEventFactory.process(context, valid.get(0));
|
100
|
return;
|
101
|
}
|
102
|
|
103
|
for (final Oaf other : valid) {
|
104
|
final String otherId = other.getEntity().getId();
|
105
|
if (!currentId.equals(otherId)) {
|
106
|
|
107
|
final float trust = similarity(current, other);
|
108
|
ProjectEventFactory.process(context, current, other, trust);
|
109
|
}
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
|
114
|
}
|
115
|
|
116
|
private Oaf addProjects(final Oaf current, final Map<String, Oaf> projects) {
|
117
|
|
118
|
final Oaf.Builder oafBuilder = Oaf.newBuilder(current);
|
119
|
final List<Oaf> cachedRels = Lists.newArrayList();
|
120
|
|
121
|
for (final Oaf.Builder cachedOafRel : oafBuilder.getEntityBuilder().getCachedOafRelBuilderList()) {
|
122
|
final String projectId = cachedOafRel.getRel().getTarget();
|
123
|
|
124
|
if (projects.containsKey(projectId)) {
|
125
|
final Oaf project = projects.get(projectId);
|
126
|
|
127
|
cachedOafRel.getRelBuilder().setCachedOafTarget(project);
|
128
|
cachedRels.add(cachedOafRel.build());
|
129
|
}
|
130
|
}
|
131
|
|
132
|
oafBuilder.getEntityBuilder().clearCachedOafRel().addAllCachedOafRel(cachedRels);
|
133
|
|
134
|
return oafBuilder.build();
|
135
|
}
|
136
|
|
137
|
}
|