2020-04-16 15:53:34 +02:00
|
|
|
package eu.dnetlib.dhp.orcidtoresultfromsemrel;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
2020-04-16 16:08:51 +02:00
|
|
|
import org.apache.hadoop.io.compress.GzipCodec;
|
2020-04-16 15:53:34 +02:00
|
|
|
import org.apache.spark.SparkConf;
|
2020-04-16 16:08:51 +02:00
|
|
|
import org.apache.spark.sql.*;
|
2020-04-16 15:53:34 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2020-04-16 16:08:51 +02:00
|
|
|
import scala.Tuple2;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2020-04-16 15:53:34 +02:00
|
|
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
2020-04-16 16:08:51 +02:00
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 16:08:51 +02:00
|
|
|
public class PrepareResultOrcidAssociationStep2 {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(PrepareResultOrcidAssociationStep2.class);
|
2020-04-16 15:53:34 +02:00
|
|
|
|
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
2020-04-18 14:13:16 +02:00
|
|
|
String jsonConfiguration = IOUtils.toString(PrepareResultOrcidAssociationStep2.class
|
2020-04-16 18:40:14 +02:00
|
|
|
.getResourceAsStream("/eu/dnetlib/dhp/orcidtoresultfromsemrel/input_prepareorcidtoresult_parameters2.json"));
|
2020-04-16 15:53:34 +02:00
|
|
|
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
jsonConfiguration);
|
|
|
|
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
Boolean isSparkSessionManaged = isSparkSessionManaged(parser);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
String inputPath = parser.get("sourcePath");
|
|
|
|
log.info("inputPath: {}", inputPath);
|
|
|
|
|
|
|
|
final String outputPath = parser.get("outputPath");
|
|
|
|
log.info("outputPath: {}", outputPath);
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
|
2020-04-16 16:08:51 +02:00
|
|
|
runWithSparkSession(conf, isSparkSessionManaged,
|
2020-04-16 15:53:34 +02:00
|
|
|
spark -> {
|
|
|
|
if (isTest(parser)) {
|
|
|
|
removeOutputDir(spark, outputPath);
|
|
|
|
}
|
2020-04-16 16:08:51 +02:00
|
|
|
mergeInfo(spark, inputPath, outputPath);
|
2020-04-16 15:53:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:08:51 +02:00
|
|
|
private static void mergeInfo(SparkSession spark, String inputPath, String outputPath) {
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 18:40:14 +02:00
|
|
|
Dataset<ResultOrcidList> resultOrcidAssoc = readAssocResultOrcidList(spark, inputPath + "/publication")
|
|
|
|
.union(readAssocResultOrcidList(spark, inputPath + "/dataset"))
|
|
|
|
.union(readAssocResultOrcidList(spark, inputPath + "/otherresearchproduct"))
|
|
|
|
.union(readAssocResultOrcidList(spark, inputPath + "/software"));
|
2020-04-16 15:53:34 +02:00
|
|
|
|
2020-04-16 16:08:51 +02:00
|
|
|
resultOrcidAssoc
|
|
|
|
.toJavaRDD()
|
|
|
|
.mapToPair(r -> new Tuple2<>(r.getResultId(), r))
|
|
|
|
.reduceByKey((a, b) -> {
|
|
|
|
if (a == null) {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
if (b == null) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
Set<String> orcid_set = new HashSet<>();
|
|
|
|
a.getAuthorList().stream().forEach(aa -> orcid_set.add(aa.getOrcid()));
|
|
|
|
|
|
|
|
b.getAuthorList().stream().forEach(aa -> {
|
|
|
|
if (!orcid_set.contains(aa.getOrcid())) {
|
|
|
|
a.getAuthorList().add(aa);
|
|
|
|
orcid_set.add(aa.getOrcid());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return a;
|
|
|
|
})
|
|
|
|
.map(c -> c._2())
|
|
|
|
.map(r -> OBJECT_MAPPER.writeValueAsString(r))
|
|
|
|
.saveAsTextFile(outputPath, GzipCodec.class);
|
2020-04-16 15:53:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 16:08:51 +02:00
|
|
|
private static Dataset<ResultOrcidList> readAssocResultOrcidList(SparkSession spark, String relationPath) {
|
|
|
|
return spark
|
|
|
|
.read()
|
|
|
|
.textFile(relationPath)
|
|
|
|
.map(value -> OBJECT_MAPPER.readValue(value, ResultOrcidList.class), Encoders.bean(ResultOrcidList.class));
|
2020-04-16 15:53:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-16 16:08:51 +02:00
|
|
|
|
|
|
|
|
2020-04-16 15:53:34 +02:00
|
|
|
}
|