2020-04-16 15:53:34 +02:00
|
|
|
package eu.dnetlib.dhp.orcidtoresultfromsemrel;
|
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkHiveSession;
|
|
|
|
|
2020-04-16 15:53:34 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2020-04-18 14:14:08 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Author;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Result;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.StructuredProperty;
|
2020-04-23 12:35:49 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
2020-04-16 15:53:34 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
2020-04-16 15:55:25 +02:00
|
|
|
import org.apache.spark.sql.Dataset;
|
2020-04-18 14:14:08 +02:00
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.SaveMode;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
2020-04-16 15:55:25 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
public class SparkOrcidToResultFromSemRelJob3 {
|
2020-04-23 12:35:49 +02:00
|
|
|
private static final Logger log =
|
|
|
|
LoggerFactory.getLogger(SparkOrcidToResultFromSemRelJob3.class);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
public static void main(String[] args) throws Exception {
|
2020-04-23 12:35:49 +02:00
|
|
|
String jsonConfiguration =
|
|
|
|
IOUtils.toString(
|
|
|
|
SparkOrcidToResultFromSemRelJob3.class.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/orcidtoresultfromsemrel/input_orcidtoresult_parameters.json"));
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
parser.parseArgument(args);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
Boolean isSparkSessionManaged = isSparkSessionManaged(parser);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
final String inputPath = parser.get("sourcePath");
|
|
|
|
log.info("inputPath: {}", inputPath);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
final String outputPath = parser.get("outputPath");
|
|
|
|
log.info("outputPath: {}", outputPath);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
final String possibleUpdates = parser.get("possibleUpdatesPath");
|
|
|
|
log.info("possibleUpdatesPath: {}", possibleUpdates);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
final String resultClassName = parser.get("resultTableName");
|
|
|
|
log.info("resultTableName: {}", resultClassName);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
final Boolean saveGraph =
|
|
|
|
Optional.ofNullable(parser.get("saveGraph"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
2020-04-16 15:55:25 +02:00
|
|
|
log.info("saveGraph: {}", saveGraph);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
Class<? extends Result> resultClazz =
|
|
|
|
(Class<? extends Result>) Class.forName(resultClassName);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.set("hive.metastore.uris", parser.get("hive_metastore_uris"));
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
runWithSparkHiveSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
2020-04-16 15:55:25 +02:00
|
|
|
spark -> {
|
2020-04-23 12:35:49 +02:00
|
|
|
if (isTest(parser)) {
|
2020-04-16 15:55:25 +02:00
|
|
|
removeOutputDir(spark, outputPath);
|
|
|
|
}
|
2020-04-23 12:35:49 +02:00
|
|
|
if (saveGraph)
|
2020-04-16 15:55:25 +02:00
|
|
|
execPropagation(spark, possibleUpdates, inputPath, outputPath, resultClazz);
|
|
|
|
});
|
|
|
|
}
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
private static <R extends Result> void execPropagation(
|
|
|
|
SparkSession spark,
|
|
|
|
String possibleUpdatesPath,
|
|
|
|
String inputPath,
|
|
|
|
String outputPath,
|
|
|
|
Class<R> resultClazz) {
|
|
|
|
|
|
|
|
// read possible updates (resultId and list of possible orcid to add
|
|
|
|
Dataset<ResultOrcidList> possible_updates =
|
|
|
|
readAssocResultOrcidList(spark, possibleUpdatesPath);
|
|
|
|
// read the result we have been considering
|
2020-04-16 15:55:25 +02:00
|
|
|
Dataset<R> result = readPathEntity(spark, inputPath, resultClazz);
|
2020-04-23 12:35:49 +02:00
|
|
|
// make join result left_outer with possible updates
|
|
|
|
|
|
|
|
result.joinWith(
|
|
|
|
possible_updates,
|
|
|
|
result.col("id").equalTo(possible_updates.col("resultId")),
|
|
|
|
"left_outer")
|
|
|
|
.map(
|
|
|
|
value -> {
|
|
|
|
R ret = value._1();
|
|
|
|
Optional<ResultOrcidList> rol = Optional.ofNullable(value._2());
|
|
|
|
if (rol.isPresent()) {
|
|
|
|
List<Author> toenrich_author = ret.getAuthor();
|
|
|
|
List<AutoritativeAuthor> autoritativeAuthors =
|
|
|
|
rol.get().getAuthorList();
|
|
|
|
for (Author author : toenrich_author) {
|
|
|
|
if (!containsAllowedPid(author)) {
|
|
|
|
enrichAuthor(author, autoritativeAuthors);
|
|
|
|
}
|
2020-04-16 15:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
Encoders.bean(resultClazz))
|
|
|
|
.toJSON()
|
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.option("compression", "gzip")
|
|
|
|
.text(outputPath);
|
2020-04-16 15:55:25 +02:00
|
|
|
}
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
private static Dataset<ResultOrcidList> readAssocResultOrcidList(
|
|
|
|
SparkSession spark, String relationPath) {
|
|
|
|
return spark.read()
|
2020-04-16 15:55:25 +02:00
|
|
|
.textFile(relationPath)
|
2020-04-23 12:35:49 +02:00
|
|
|
.map(
|
|
|
|
value -> OBJECT_MAPPER.readValue(value, ResultOrcidList.class),
|
|
|
|
Encoders.bean(ResultOrcidList.class));
|
2020-04-16 15:53:34 +02:00
|
|
|
}
|
2020-04-16 15:55:25 +02:00
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
private static void enrichAuthor(Author a, List<AutoritativeAuthor> au) {
|
|
|
|
for (AutoritativeAuthor aa : au) {
|
|
|
|
if (enrichAuthor(aa, a)) {
|
2020-04-16 15:53:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
private static boolean enrichAuthor(AutoritativeAuthor autoritative_author, Author author) {
|
2020-04-16 15:53:34 +02:00
|
|
|
boolean toaddpid = false;
|
|
|
|
|
|
|
|
if (StringUtils.isNoneEmpty(autoritative_author.getSurname())) {
|
|
|
|
if (StringUtils.isNoneEmpty(author.getSurname())) {
|
2020-04-23 12:35:49 +02:00
|
|
|
if (autoritative_author
|
|
|
|
.getSurname()
|
|
|
|
.trim()
|
|
|
|
.equalsIgnoreCase(author.getSurname().trim())) {
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-23 12:35:49 +02:00
|
|
|
// have the same surname. Check the name
|
2020-04-16 15:53:34 +02:00
|
|
|
if (StringUtils.isNoneEmpty(autoritative_author.getName())) {
|
|
|
|
if (StringUtils.isNoneEmpty(author.getName())) {
|
2020-04-23 12:35:49 +02:00
|
|
|
if (autoritative_author
|
|
|
|
.getName()
|
|
|
|
.trim()
|
|
|
|
.equalsIgnoreCase(author.getName().trim())) {
|
2020-04-16 15:53:34 +02:00
|
|
|
toaddpid = true;
|
|
|
|
}
|
2020-04-23 12:35:49 +02:00
|
|
|
// they could be differently written (i.e. only the initials of the name
|
|
|
|
// in one of the two
|
|
|
|
if (autoritative_author
|
|
|
|
.getName()
|
|
|
|
.trim()
|
|
|
|
.substring(0, 0)
|
|
|
|
.equalsIgnoreCase(author.getName().trim().substring(0, 0))) {
|
2020-04-16 15:53:34 +02:00
|
|
|
toaddpid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 12:35:49 +02:00
|
|
|
if (toaddpid) {
|
2020-04-17 16:52:02 +02:00
|
|
|
StructuredProperty p = new StructuredProperty();
|
|
|
|
p.setValue(autoritative_author.getOrcid());
|
2020-04-23 12:35:49 +02:00
|
|
|
p.setQualifier(getQualifier(PROPAGATION_AUTHOR_PID, PROPAGATION_AUTHOR_PID));
|
|
|
|
p.setDataInfo(
|
|
|
|
getDataInfo(
|
|
|
|
PROPAGATION_DATA_INFO_TYPE,
|
|
|
|
PROPAGATION_ORCID_TO_RESULT_FROM_SEM_REL_CLASS_ID,
|
|
|
|
PROPAGATION_ORCID_TO_RESULT_FROM_SEM_REL_CLASS_NAME));
|
2020-04-17 16:52:02 +02:00
|
|
|
author.addPid(p);
|
2020-04-16 15:53:34 +02:00
|
|
|
}
|
|
|
|
return toaddpid;
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:55:25 +02:00
|
|
|
private static boolean containsAllowedPid(Author a) {
|
2020-04-16 15:53:34 +02:00
|
|
|
for (StructuredProperty pid : a.getPid()) {
|
|
|
|
if (PROPAGATION_AUTHOR_PID.equals(pid.getQualifier().getClassid())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|