2020-04-30 11:05:17 +02:00
|
|
|
|
2020-04-11 16:26:23 +02:00
|
|
|
package eu.dnetlib.dhp.projecttoresult;
|
|
|
|
|
2020-04-23 12:40:44 +02:00
|
|
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
|
|
|
import static eu.dnetlib.dhp.PropagationConstant.getConstraintList;
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkHiveSession;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2020-04-30 11:05:17 +02:00
|
|
|
|
2020-04-11 16:26:23 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2020-04-27 10:26:55 +02:00
|
|
|
import org.apache.hadoop.io.compress.GzipCodec;
|
2020-04-11 16:26:23 +02:00
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
import org.apache.spark.sql.*;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2020-04-30 11:05:17 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.countrypropagation.PrepareDatasourceCountryAssociation;
|
2020-05-15 10:21:09 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
2020-04-30 11:05:17 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
|
|
|
|
2020-04-14 15:31:26 +02:00
|
|
|
public class PrepareProjectResultsAssociation {
|
2020-04-30 11:05:17 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(PrepareDatasourceCountryAssociation.class);
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
|
|
|
String jsonConfiguration = IOUtils
|
|
|
|
.toString(
|
|
|
|
PrepareProjectResultsAssociation.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/projecttoresult/input_prepareprojecttoresult_parameters.json"));
|
|
|
|
|
|
|
|
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 potentialUpdatePath = parser.get("potentialUpdatePath");
|
|
|
|
log.info("potentialUpdatePath {}: ", potentialUpdatePath);
|
|
|
|
|
|
|
|
String alreadyLinkedPath = parser.get("alreadyLinkedPath");
|
|
|
|
log.info("alreadyLinkedPath: {} ", alreadyLinkedPath);
|
|
|
|
|
|
|
|
final List<String> allowedsemrel = Arrays.asList(parser.get("allowedsemrels").split(";"));
|
|
|
|
log.info("allowedSemRel: {}", new Gson().toJson(allowedsemrel));
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.set("hive.metastore.uris", parser.get("hive_metastore_uris"));
|
|
|
|
|
|
|
|
runWithSparkHiveSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
|
|
|
spark -> {
|
2020-05-14 10:25:41 +02:00
|
|
|
removeOutputDir(spark, potentialUpdatePath);
|
|
|
|
removeOutputDir(spark, alreadyLinkedPath);
|
2020-04-30 11:05:17 +02:00
|
|
|
prepareResultProjProjectResults(
|
|
|
|
spark,
|
|
|
|
inputPath,
|
|
|
|
potentialUpdatePath,
|
|
|
|
alreadyLinkedPath,
|
|
|
|
allowedsemrel);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void prepareResultProjProjectResults(
|
|
|
|
SparkSession spark,
|
|
|
|
String inputPath,
|
|
|
|
String potentialUpdatePath,
|
|
|
|
String alreadyLinkedPath,
|
|
|
|
List<String> allowedsemrel) {
|
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
Dataset<Relation> relation = readPath(spark, inputPath, Relation.class);
|
2020-04-30 11:05:17 +02:00
|
|
|
relation.createOrReplaceTempView("relation");
|
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
String resproj_relation_query = "SELECT source, target "
|
2020-04-30 11:05:17 +02:00
|
|
|
+ " FROM relation "
|
|
|
|
+ " WHERE datainfo.deletedbyinference = false "
|
|
|
|
+ " AND relClass = '"
|
2020-05-14 18:29:24 +02:00
|
|
|
+ ModelConstants.IS_PRODUCED_BY
|
2020-04-30 11:05:17 +02:00
|
|
|
+ "'";
|
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
Dataset<Row> resproj_relation = spark.sql(resproj_relation_query);
|
2020-04-30 11:05:17 +02:00
|
|
|
resproj_relation.createOrReplaceTempView("resproj_relation");
|
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
String potential_update_query = "SELECT resultId, collect_set(projectId) projectSet "
|
2020-04-30 11:05:17 +02:00
|
|
|
+ "FROM ( "
|
|
|
|
+ "SELECT r1.target resultId, r2.target projectId "
|
|
|
|
+ " FROM (SELECT source, target "
|
|
|
|
+ " FROM relation "
|
|
|
|
+ " WHERE datainfo.deletedbyinference = false "
|
|
|
|
+ getConstraintList(" relClass = '", allowedsemrel)
|
|
|
|
+ " ) r1"
|
|
|
|
+ " JOIN resproj_relation r2 "
|
|
|
|
+ " ON r1.source = r2.source "
|
|
|
|
+ " ) tmp "
|
|
|
|
+ "GROUP BY resultId ";
|
|
|
|
|
|
|
|
spark
|
2020-05-07 18:22:26 +02:00
|
|
|
.sql(potential_update_query)
|
2020-04-30 11:05:17 +02:00
|
|
|
.as(Encoders.bean(ResultProjectSet.class))
|
2020-05-07 18:22:26 +02:00
|
|
|
.write()
|
|
|
|
.option("compression", "gzip")
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.json(potentialUpdatePath);
|
|
|
|
|
|
|
|
String result_projectset_query = "SELECT source resultId, collect_set(target) projectSet "
|
2020-04-30 11:05:17 +02:00
|
|
|
+ "FROM resproj_relation "
|
|
|
|
+ "GROUP BY source";
|
|
|
|
|
|
|
|
spark
|
2020-05-07 18:22:26 +02:00
|
|
|
.sql(result_projectset_query)
|
2020-04-30 11:05:17 +02:00
|
|
|
.as(Encoders.bean(ResultProjectSet.class))
|
2020-05-07 18:22:26 +02:00
|
|
|
.write()
|
|
|
|
.option("compression", "gzip")
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.json(alreadyLinkedPath);
|
2020-04-30 11:05:17 +02:00
|
|
|
}
|
2020-05-07 18:22:26 +02:00
|
|
|
|
2020-04-11 16:26:23 +02:00
|
|
|
}
|