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.proto.KindProtos.Kind;
|
9
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
10
|
import eu.dnetlib.data.proto.OafProtos.OafEntity;
|
11
|
import eu.dnetlib.data.proto.ResultProtos.Result;
|
12
|
import eu.dnetlib.data.proto.ResultProtos.Result.Context;
|
13
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
14
|
import eu.dnetlib.msro.rmi.MSROException;
|
15
|
import org.apache.commons.codec.binary.Base64;
|
16
|
import org.apache.commons.logging.Log;
|
17
|
import org.apache.commons.logging.LogFactory;
|
18
|
import org.apache.hadoop.util.StringUtils;
|
19
|
|
20
|
/**
|
21
|
* Created by alessia on 23/10/15.
|
22
|
*/
|
23
|
public class ApplyClaimUpdatesJobNode extends AbstractClaimsToHBASE {
|
24
|
|
25
|
private static final Log log = LogFactory.getLog(ApplyClaimUpdatesJobNode.class);
|
26
|
|
27
|
@Override
|
28
|
protected String execute(NodeToken token) throws Exception {
|
29
|
//TODO: use claim.claim_date from the claim db
|
30
|
long timestamp = System.currentTimeMillis();
|
31
|
setTotal(getClaimDatabaseUtils().count(getCountQuery()));
|
32
|
List<Claim> claimUpdates = this.getClaimDatabaseUtils().query(getSql());
|
33
|
int discardedClaims = 0;
|
34
|
|
35
|
HadoopService hadoopService = getServiceLocator().getService(HadoopService.class);
|
36
|
|
37
|
for (Claim claim : claimUpdates) {
|
38
|
try {
|
39
|
log.debug(claim);
|
40
|
String contextId = claim.getSource();
|
41
|
String rowKey = getFullId(getOpenAIREType(claim.getTargetType()), claim.getTarget());
|
42
|
|
43
|
String value = getValue(rowKey, contextId, timestamp);
|
44
|
hadoopService.addHBaseColumn(getClusterName(), getTableName(), rowKey, "result", "update_" + System.nanoTime(), value);
|
45
|
incrementProcessed();
|
46
|
} catch (IllegalArgumentException e) {
|
47
|
log.error("Discarding claim " + claim + ". Cause: " + e.getMessage());
|
48
|
discardedClaims++;
|
49
|
}
|
50
|
}
|
51
|
|
52
|
log.info("Total Claim Updates: " + getTotal());
|
53
|
token.getEnv().setAttribute("claimUpdatesSize", getTotal());
|
54
|
log.info("Claim updates: " + getProcessed());
|
55
|
token.getEnv().setAttribute("claimUpdates", getProcessed());
|
56
|
log.info("Discarded Claim Updates: " + discardedClaims);
|
57
|
token.getEnv().setAttribute("discardedClaimUpdates", discardedClaims);
|
58
|
|
59
|
return Arc.DEFAULT_ARC;
|
60
|
}
|
61
|
|
62
|
protected String getValue(final String rowkey, final String contextid, final long timestamp) throws MSROException {
|
63
|
log.debug(StringUtils.format("%s -- %s", rowkey, contextid));
|
64
|
|
65
|
Result.Builder resultBuilder = Result.newBuilder().setMetadata(Result.Metadata.newBuilder().addContext(getContext(contextid)));
|
66
|
OafEntity.Builder entityBuilder = OafEntity.newBuilder().setId(rowkey).setType(Type.result).setResult(resultBuilder);
|
67
|
Oaf.Builder builder = Oaf.newBuilder().setKind(Kind.entity).setLastupdatetimestamp(timestamp).setEntity(entityBuilder);
|
68
|
|
69
|
return Base64.encodeBase64String(builder.build().toByteArray());
|
70
|
}
|
71
|
|
72
|
private Context getContext(final String sourceId) {
|
73
|
return Context.newBuilder().setDataInfo(getDataInfo()).setId(sourceId).build();
|
74
|
}
|
75
|
}
|