1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.claims;
|
2
|
|
3
|
import java.util.List;
|
4
|
|
5
|
import com.googlecode.sarasvati.Arc;
|
6
|
import com.googlecode.sarasvati.NodeToken;
|
7
|
import eu.dnetlib.data.hadoop.rmi.HadoopService;
|
8
|
import eu.dnetlib.data.mapreduce.util.OafRowKeyDecoder;
|
9
|
import eu.dnetlib.data.proto.FieldTypeProtos.DataInfo;
|
10
|
import eu.dnetlib.data.proto.FieldTypeProtos.Qualifier;
|
11
|
import eu.dnetlib.data.proto.KindProtos.Kind;
|
12
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
13
|
import eu.dnetlib.data.proto.OafProtos.OafRel;
|
14
|
import eu.dnetlib.data.proto.RelMetadataProtos.RelMetadata;
|
15
|
import eu.dnetlib.data.proto.RelTypeProtos.RelType;
|
16
|
import eu.dnetlib.data.proto.RelTypeProtos.SubRelType;
|
17
|
import eu.dnetlib.data.proto.ResultProjectProtos.ResultProject;
|
18
|
import eu.dnetlib.data.proto.ResultProjectProtos.ResultProject.Outcome;
|
19
|
import eu.dnetlib.data.proto.ResultResultProtos.ResultResult;
|
20
|
import eu.dnetlib.data.proto.ResultResultProtos.ResultResult.PublicationDataset;
|
21
|
import eu.dnetlib.data.transform.xml.AbstractDNetXsltFunctions;
|
22
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
23
|
import eu.dnetlib.msro.rmi.MSROException;
|
24
|
import eu.dnetlib.msro.workflows.nodes.ProgressJobNode;
|
25
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
26
|
import eu.dnetlib.msro.workflows.util.ProgressProvider;
|
27
|
import eu.dnetlib.utils.ontologies.OntologyLoader;
|
28
|
import org.apache.commons.codec.binary.Base64;
|
29
|
import org.apache.commons.logging.Log;
|
30
|
import org.apache.commons.logging.LogFactory;
|
31
|
import org.apache.hadoop.util.StringUtils;
|
32
|
import org.springframework.beans.factory.annotation.Autowired;
|
33
|
|
34
|
/**
|
35
|
* Created by alessia on 23/10/15.
|
36
|
*/
|
37
|
public class ApplyClaimRelsJobNode extends SimpleJobNode implements ProgressJobNode {
|
38
|
|
39
|
private static final Log log = LogFactory.getLog(ApplyClaimRelsJobNode.class);
|
40
|
|
41
|
private final String SEPARATOR = "_";
|
42
|
|
43
|
@Autowired
|
44
|
private UniqueServiceLocator serviceLocator;
|
45
|
|
46
|
@Autowired
|
47
|
private ClaimDatabaseUtils claimDatabaseUtils;
|
48
|
|
49
|
private String sql;
|
50
|
private String countQuery;
|
51
|
private int total = 0;
|
52
|
private int processed = 0;
|
53
|
|
54
|
private String clusterName;
|
55
|
|
56
|
private String tableName;
|
57
|
|
58
|
// private String fetchSqlAsText(final String path) throws IOException {
|
59
|
// return IOUtils.toString(getClass().getResourceAsStream(path));
|
60
|
// }
|
61
|
|
62
|
@Override
|
63
|
protected String execute(NodeToken token) throws Exception {
|
64
|
//TODO: use claim.claim_date from the claim db
|
65
|
long timestamp = System.currentTimeMillis();
|
66
|
total = this.claimDatabaseUtils.count(countQuery);
|
67
|
List<Claim> claimRels = this.claimDatabaseUtils.query(sql);
|
68
|
int totalWrites = 0;
|
69
|
int valid = 0;
|
70
|
int discardedClaims = 0;
|
71
|
|
72
|
HadoopService hadoopService = serviceLocator.getService(HadoopService.class);
|
73
|
|
74
|
for (Claim claim : claimRels) {
|
75
|
log.debug(claim);
|
76
|
try {
|
77
|
String sourceId = fullId(getOpenAIREType(claim.getSourceType()), claim.getSource());
|
78
|
String targetId = fullId(getOpenAIREType(claim.getTargetType()), claim.getTarget());
|
79
|
String value = getValue(sourceId, claim.getSemantics(), targetId, timestamp);
|
80
|
|
81
|
/*
|
82
|
public void addHBaseColumn(final String clusterName,
|
83
|
final String tableName,
|
84
|
final String rowKey,
|
85
|
final String columnFamily,
|
86
|
final String qualifier,
|
87
|
final String value)
|
88
|
*/
|
89
|
hadoopService.addHBaseColumn(clusterName, tableName, sourceId, claim.getSemantics(), targetId, value);
|
90
|
processed++;
|
91
|
totalWrites++;
|
92
|
|
93
|
String inverseSemantics = OntologyLoader.fetchInverse(claim.getSemantics());
|
94
|
String inverseValue = getValue(targetId, inverseSemantics, sourceId, timestamp);
|
95
|
hadoopService.addHBaseColumn(clusterName, tableName, targetId, inverseSemantics, sourceId, inverseValue);
|
96
|
totalWrites++;
|
97
|
}catch(IllegalArgumentException e){
|
98
|
log.error("Discarding claim "+claim+". Cause: "+e.getMessage());
|
99
|
discardedClaims++;
|
100
|
}
|
101
|
}
|
102
|
|
103
|
log.info("totalClaims: " + total);
|
104
|
token.getEnv().setAttribute("claimSize", total);
|
105
|
log.info("writeOps: " + totalWrites);
|
106
|
token.getEnv().setAttribute("writeOps", totalWrites);
|
107
|
log.info("validClaims: " + valid);
|
108
|
token.getEnv().setAttribute("validClaims", valid);
|
109
|
log.info("discardedClaims: " + discardedClaims);
|
110
|
token.getEnv().setAttribute("discardedClaims", discardedClaims);
|
111
|
|
112
|
return Arc.DEFAULT_ARC;
|
113
|
}
|
114
|
|
115
|
protected String getOpenAIREType(final String type){
|
116
|
switch(type){
|
117
|
case "publication":
|
118
|
case "dataset":
|
119
|
return "result";
|
120
|
default:
|
121
|
return type;
|
122
|
}
|
123
|
}
|
124
|
|
125
|
|
126
|
public String getValue(final String sourceId, final String semantics, final String targetId, final long timestamp) throws MSROException{
|
127
|
log.debug(StringUtils.format("%s -- %s -- %s", sourceId, semantics, targetId));
|
128
|
String[] relInfo = semantics.split(SEPARATOR);
|
129
|
if(relInfo.length < 3){
|
130
|
throw new MSROException("Semantics "+semantics+" not supported: must be splittable in 3 by '_'");
|
131
|
}
|
132
|
Qualifier.Builder semanticsBuilder = Qualifier.newBuilder().setClassid(relInfo[2]).setClassname(relInfo[2]);
|
133
|
|
134
|
Oaf.Builder builder = Oaf.newBuilder().setKind(Kind.relation).setLastupdatetimestamp(timestamp);
|
135
|
builder.setDataInfo(DataInfo.newBuilder().setTrust("0.91").setInferred(false)
|
136
|
.setProvenanceaction(
|
137
|
Qualifier.newBuilder()
|
138
|
.setClassid("user:claim")
|
139
|
.setClassname("user:claim")
|
140
|
.setSchemeid("dnet:provenanceActions")
|
141
|
.setSchemename("dnet:provenanceActions")
|
142
|
));
|
143
|
|
144
|
final SubRelType subRelType = SubRelType.valueOf(relInfo[1]);
|
145
|
final OafRel.Builder relBuilder = OafRel.newBuilder()
|
146
|
.setSubRelType(subRelType)
|
147
|
.setRelClass(relInfo[2])
|
148
|
.setRelType(RelType.valueOf(relInfo[0]))
|
149
|
.setSource(sourceId).setTarget(targetId).setChild(false);
|
150
|
|
151
|
switch (relInfo[0]) {
|
152
|
case "resultProject":
|
153
|
|
154
|
relBuilder.setResultProject(ResultProject.newBuilder()
|
155
|
.setOutcome(Outcome.newBuilder().setRelMetadata(
|
156
|
RelMetadata.newBuilder().setSemantics(
|
157
|
semanticsBuilder
|
158
|
.setSchemeid("dnet:result_project_relations")
|
159
|
.setSchemename("dnet:result_project_relations")
|
160
|
.build()
|
161
|
))));
|
162
|
break;
|
163
|
case "resultResult":
|
164
|
relBuilder.setResultResult(ResultResult.newBuilder()
|
165
|
.setPublicationDataset(PublicationDataset.newBuilder().setRelMetadata(
|
166
|
RelMetadata.newBuilder().setSemantics(
|
167
|
semanticsBuilder
|
168
|
.setSchemeid("dnet:result_result_relations")
|
169
|
.setSchemename("dnet:result_result_relations")
|
170
|
.build()
|
171
|
))));
|
172
|
break;
|
173
|
default:
|
174
|
throw new MSROException("Semantics "+relInfo[0]+" not supported");
|
175
|
}
|
176
|
|
177
|
builder.setRel(relBuilder);
|
178
|
return Base64.encodeBase64String(builder.build().toByteArray());
|
179
|
}
|
180
|
|
181
|
private String fullId(final String type, final String id) {
|
182
|
final String fullId = AbstractDNetXsltFunctions.oafSimpleId(type, id);
|
183
|
return OafRowKeyDecoder.decode(fullId).getKey();
|
184
|
}
|
185
|
|
186
|
public String getClusterName() {
|
187
|
return clusterName;
|
188
|
}
|
189
|
|
190
|
public void setClusterName(final String clusterName) {
|
191
|
this.clusterName = clusterName;
|
192
|
}
|
193
|
|
194
|
public String getTableName() {
|
195
|
return tableName;
|
196
|
}
|
197
|
|
198
|
public void setTableName(final String tableName) {
|
199
|
this.tableName = tableName;
|
200
|
}
|
201
|
|
202
|
public UniqueServiceLocator getServiceLocator() {
|
203
|
return serviceLocator;
|
204
|
}
|
205
|
|
206
|
public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
|
207
|
this.serviceLocator = serviceLocator;
|
208
|
}
|
209
|
|
210
|
public ClaimDatabaseUtils getClaimDatabaseUtils() {
|
211
|
return claimDatabaseUtils;
|
212
|
}
|
213
|
|
214
|
public void setClaimDatabaseUtils(final ClaimDatabaseUtils claimDatabaseUtils) {
|
215
|
this.claimDatabaseUtils = claimDatabaseUtils;
|
216
|
}
|
217
|
|
218
|
public String getSql() {
|
219
|
return sql;
|
220
|
}
|
221
|
|
222
|
public void setSql(final String sql) {
|
223
|
this.sql = sql;
|
224
|
}
|
225
|
|
226
|
public String getCountQuery() {
|
227
|
return countQuery;
|
228
|
}
|
229
|
|
230
|
public void setCountQuery(final String countQuery) {
|
231
|
this.countQuery = countQuery;
|
232
|
}
|
233
|
|
234
|
@Override
|
235
|
public ProgressProvider getProgressProvider() {
|
236
|
return new ProgressProvider() {
|
237
|
|
238
|
@Override
|
239
|
public boolean isInaccurate() {
|
240
|
return false;
|
241
|
}
|
242
|
|
243
|
@Override
|
244
|
public int getTotalValue() {
|
245
|
return total;
|
246
|
}
|
247
|
|
248
|
@Override
|
249
|
public int getCurrentValue() {
|
250
|
return processed;
|
251
|
}
|
252
|
};
|
253
|
}
|
254
|
}
|