Project

General

Profile

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.ProjectEventFactory;
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.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
		Streams.stream(values).limit(LIMIT).map(EnrichmentReducer::toOaf).forEach(oaf -> {
38
			switch (oaf.getEntity().getType()) {
39
			case result:
40
				pubs.add(oaf);
41
				break;
42
			case project:
43
				projects.put(oaf.getEntity().getId(), oaf);
44
				break;
45
			default:
46
				throw new IllegalArgumentException("invalid type: " + oaf.getEntity().getType());
47
			}
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> valid = pubs.stream()
66
				.map(p -> addProjects(p, projects))
67
				.filter(r -> !r.getEntity().getCachedOafRelList().isEmpty())
68
				.collect(Collectors.toList());
69

    
70
		if (valid.isEmpty()) {
71
			context.getCounter(counterGroup(), "Events Skipped - Missing project").increment(1);
72
			return;
73
		}
74

    
75
		for (final Oaf current : valid) {
76

    
77
			final String currentId = current.getEntity().getId();
78

    
79
			final String currentDsId = StringUtils.substringAfter(getKey(current.getEntity().getCollectedfromList()), "|");
80
			final String currentDsType = dsTypeMap.get(currentDsId);
81

    
82
			// System.out.println(String.format("'%s' -> '%s'", currentDsId, currentDsType));
83

    
84
			if (StringUtils.isBlank(currentDsType) && !dsWhitelist.contains(currentDsId)) {
85
				context.getCounter(counterGroup(), "Events Skipped - Datasource type excluded").increment(1);
86
			} else if (dsBlacklist.contains(currentDsId)) {
87
				context.getCounter(counterGroup(), "Events Skipped - Datasource blacklisted").increment(1);
88
			} else {
89
				final List<EventWrapper> events = Lists.newArrayList();
90

    
91
				if (valid.size() == 1) {
92
					events.addAll(ProjectEventFactory.process(context, valid.get(0)));
93
				} else {
94

    
95
					for (final Oaf other : valid) {
96
						final String otherId = other.getEntity().getId();
97
						if (!currentId.equals(otherId)) {
98

    
99
							final double similarity = similarity(current, other);
100

    
101
							if (similarity >= dedupConf.getWf().getThreshold()) {
102

    
103
								final float trust = scale(similarity);
104

    
105
								events.addAll(ProjectEventFactory.process(context, current, other, trust));
106
							} else {
107
								context.getCounter(counterGroup(), "d < " + dedupConf.getWf().getThreshold()).increment(1);
108
							}
109

    
110
						}
111
					}
112
				}
113
				emit(events, context);
114
			}
115
		}
116

    
117
	}
118

    
119
	private Oaf addProjects(final Oaf current, final Map<String, Oaf> projects) {
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 projectId = cachedOafRel.getRel().getTarget();
126

    
127
			if (projects.containsKey(projectId)) {
128
				final Oaf project = projects.get(projectId);
129

    
130
				cachedOafRel.getRelBuilder().setCachedOafTarget(project);
131
				cachedRels.add(cachedOafRel.build());
132
			}
133
		}
134

    
135
		oafBuilder.getEntityBuilder().clearCachedOafRel().addAllCachedOafRel(cachedRels);
136

    
137
		return oafBuilder.build();
138
	}
139

    
140
}
(6-6/8)