1
|
package eu.dnetlib.data.mapreduce.hbase.propagation.country.institutionalrepositories;
|
2
|
|
3
|
import com.google.common.base.Splitter;
|
4
|
import com.google.common.collect.Lists;
|
5
|
import com.google.common.collect.Sets;
|
6
|
import com.google.protobuf.InvalidProtocolBufferException;
|
7
|
import eu.dnetlib.data.mapreduce.hbase.propagation.Value;
|
8
|
import eu.dnetlib.data.mapreduce.hbase.propagation.compositekeys.InstOrgKey;
|
9
|
import eu.dnetlib.data.mapreduce.util.OafRowKeyDecoder;
|
10
|
import eu.dnetlib.data.proto.*;
|
11
|
import org.apache.commons.collections.MapUtils;
|
12
|
import org.apache.hadoop.hbase.client.Result;
|
13
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
14
|
import org.apache.hadoop.hbase.mapreduce.TableMapper;
|
15
|
import org.apache.hadoop.hbase.util.Bytes;
|
16
|
import org.apache.hadoop.io.Text;
|
17
|
|
18
|
import java.io.IOException;
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.Map;
|
21
|
import java.util.Set;
|
22
|
import java.util.stream.Collectors;
|
23
|
import java.util.stream.Stream;
|
24
|
|
25
|
import static eu.dnetlib.data.mapreduce.hbase.propagation.PropagationConstants.*;
|
26
|
import static eu.dnetlib.data.mapreduce.hbase.propagation.Utils.*;
|
27
|
/**
|
28
|
* Created by miriam on 17/08/2018.
|
29
|
*/
|
30
|
public class PropagationCountryFromDsOrgResultMapper extends TableMapper<InstOrgKey, Text> {
|
31
|
|
32
|
private Text valueOut;
|
33
|
|
34
|
private Set<String> datasourceTypes = Sets.newHashSet("pubsrepository::institutional");
|
35
|
private Set<String> whiteList = Sets.newHashSet("10|opendoar____::300891a62162b960cf02ce3827bb363c");
|
36
|
private Set<String> blackList = Sets.newHashSet("");
|
37
|
|
38
|
@Override
|
39
|
protected void setup(final Context context) throws IOException, InterruptedException {
|
40
|
super.setup(context);
|
41
|
|
42
|
valueOut = new Text();
|
43
|
|
44
|
datasourceTypes.addAll(getParam(context, "datasource.types"));
|
45
|
whiteList.addAll(getParam(context, "datasource.whitelist"));
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
protected void map(final ImmutableBytesWritable keyIn, final Result value, final Context context) throws IOException, InterruptedException {
|
50
|
|
51
|
final TypeProtos.Type type = OafRowKeyDecoder.decode(keyIn.copyBytes()).getType();
|
52
|
final OafProtos.OafEntity entity = getEntity(value,type);
|
53
|
if (entity != null) {
|
54
|
switch (type) {
|
55
|
case datasource:
|
56
|
final DatasourceProtos.Datasource datasource = entity.getDatasource();
|
57
|
final String id = entity.getId();
|
58
|
if (datasource == null) {
|
59
|
throw new RuntimeException("oaf type is datasource, but datasource proto is not found in oafproto");
|
60
|
}
|
61
|
|
62
|
String dsType = datasource.getMetadata().getDatasourcetype().getClassid();
|
63
|
if (datasourceTypes.contains(dsType)) {
|
64
|
// verify datasource is in blacklist
|
65
|
if (blackList.contains(id)){
|
66
|
context.getCounter(COUNTER_PROPAGATION,"blacklisted").increment(1);
|
67
|
emitNotAllowedDatasource(context,entity.getId());
|
68
|
|
69
|
} else {
|
70
|
emitAllowedDatasource(value, context, entity.getId(), dsType);
|
71
|
}
|
72
|
} else {
|
73
|
// verify datasource is in whiteList
|
74
|
|
75
|
if (whiteList.contains(id)) {
|
76
|
context.getCounter(COUNTER_PROPAGATION,"whitelisted " + id).increment(1);
|
77
|
emitAllowedDatasource(value,context,entity.getId(),dsType);
|
78
|
} else {
|
79
|
emitNotAllowedDatasource(context, entity.getId());
|
80
|
}
|
81
|
}
|
82
|
|
83
|
break;
|
84
|
case organization:
|
85
|
OrganizationProtos.Organization organization = entity.getOrganization();
|
86
|
if (organization == null) {
|
87
|
throw new RuntimeException("oaf type is organizationtoresult, but organizationtoresult proto is not found in oafproto");
|
88
|
}
|
89
|
|
90
|
FieldTypeProtos.Qualifier country = organization.getMetadata().getCountry();
|
91
|
if (country == null) {
|
92
|
context.getCounter(COUNTER_PROPAGATION, "country elem does not exists").increment(1);
|
93
|
} else {
|
94
|
valueOut.set(Value.newInstance(country.getClassid()).toJson());
|
95
|
Stream.of(value.raw())
|
96
|
.filter(kv -> "datasourceOrganization_provision_isProvidedBy".equals(Bytes.toString(kv.getFamily())))
|
97
|
.map(kv -> Bytes.toString(kv.getQualifier()))
|
98
|
.forEach(dsId -> {
|
99
|
try {
|
100
|
context.write( InstOrgKey.organization(dsId), valueOut);
|
101
|
context.getCounter(COUNTER_PROPAGATION, "country").increment(1);
|
102
|
} catch (IOException | InterruptedException e) {
|
103
|
throw new RuntimeException(e);
|
104
|
}
|
105
|
});
|
106
|
}
|
107
|
|
108
|
break;
|
109
|
case result:
|
110
|
ResultProtos.Result result = entity.getResult();
|
111
|
|
112
|
for (ResultProtos.Result.Instance instance : result.getInstanceList()) {
|
113
|
//todo add check if key is not empty and field is not null
|
114
|
|
115
|
String hostedBy = instance.getHostedby().getKey();
|
116
|
valueOut.set(Value.newInstance(entity.getId()).toJson());
|
117
|
context.write( InstOrgKey.publication(hostedBy),valueOut);
|
118
|
context.getCounter(COUNTER_PROPAGATION, "emit hostedby | collectedfrom for publication ").increment(1);
|
119
|
String collectedFrom = instance.getCollectedfrom().getKey();
|
120
|
if (!hostedBy.equals(collectedFrom)) {
|
121
|
context.write(eu.dnetlib.data.mapreduce.hbase.propagation.compositekeys.InstOrgKey.publication(collectedFrom), valueOut);
|
122
|
context.getCounter(COUNTER_PROPAGATION, "emit hostedby | collectedfrom for publication ").increment(1);
|
123
|
}
|
124
|
}
|
125
|
break;
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
|
130
|
private void emitNotAllowedDatasource(Context context, String id) throws IOException, InterruptedException {
|
131
|
valueOut.set(Value.newInstance(ZERO).toJson());
|
132
|
context.write( InstOrgKey.datasource(id), valueOut);
|
133
|
context.getCounter(COUNTER_PROPAGATION, "ds Type not in propagation allowed list").increment(1);
|
134
|
}
|
135
|
|
136
|
private void emitAllowedDatasource(Result value, Context context, String id, String dsType) throws IOException, InterruptedException {
|
137
|
valueOut.set(Value.newInstance(ONE, getTrust(value)).toJson());
|
138
|
context.write( InstOrgKey.datasource(id), valueOut);
|
139
|
context.getCounter(COUNTER_PROPAGATION, String.format("%s in propagation allowed list", dsType)).increment(1);
|
140
|
}
|
141
|
|
142
|
|
143
|
|
144
|
private String getTrust(Result value) throws InvalidProtocolBufferException {
|
145
|
final byte[] body = value.getValue(Bytes.toBytes("datasource"), Bytes.toBytes("body"));
|
146
|
if (body != null){
|
147
|
OafProtos.Oaf oaf = OafProtos.Oaf.parseFrom(body);
|
148
|
return oaf.getDataInfo().getTrust();
|
149
|
}
|
150
|
return null;
|
151
|
}
|
152
|
|
153
|
private ArrayList<String> getParam(Context context, String s) {
|
154
|
return Lists.newArrayList(Splitter.on(",").omitEmptyStrings().split(context.getConfiguration().get(s, "")));
|
155
|
}
|
156
|
|
157
|
}
|