dnet-hadoop/dhp-workflows/dhp-propagation/src/main/java/eu/dnetlib/dhp/orcidtoresultfromsemrel/PrepareResultOrcidAssociati...

135 lines
5.6 KiB
Java
Raw Normal View History

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 com.google.gson.Gson;
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
import eu.dnetlib.dhp.schema.oaf.Relation;
import eu.dnetlib.dhp.schema.oaf.Result;
2020-04-23 12:35:49 +02:00
import java.util.Arrays;
import java.util.List;
2020-04-16 15:53:34 +02:00
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.io.compress.GzipCodec;
2020-04-16 15:53:34 +02:00
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.SparkSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PrepareResultOrcidAssociationStep1 {
2020-04-23 12:35:49 +02:00
private static final Logger log =
LoggerFactory.getLogger(PrepareResultOrcidAssociationStep1.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-23 12:35:49 +02:00
String jsonConfiguration =
IOUtils.toString(
SparkOrcidToResultFromSemRelJob3.class.getResourceAsStream(
"/eu/dnetlib/dhp/orcidtoresultfromsemrel/input_prepareorcidtoresult_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
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);
final String resultClassName = parser.get("resultTableName");
log.info("resultTableName: {}", resultClassName);
final List<String> allowedsemrel = Arrays.asList(parser.get("allowedsemrels").split(";"));
2020-04-16 15:53:34 +02:00
log.info("allowedSemRel: {}", new Gson().toJson(allowedsemrel));
2020-04-23 12:35:49 +02:00
final String resultType =
resultClassName.substring(resultClassName.lastIndexOf(".") + 1).toLowerCase();
2020-04-16 15:53:34 +02:00
log.info("resultType: {}", resultType);
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
SparkConf conf = new SparkConf();
conf.set("hive.metastore.uris", parser.get("hive_metastore_uris"));
2020-04-23 12:35:49 +02:00
runWithSparkHiveSession(
conf,
isSparkSessionManaged,
2020-04-16 15:53:34 +02:00
spark -> {
if (isTest(parser)) {
removeOutputDir(spark, outputPath);
}
2020-04-23 12:35:49 +02:00
prepareInfo(
spark, inputPath, outputPath, resultClazz, resultType, allowedsemrel);
2020-04-16 15:53:34 +02:00
});
}
2020-04-23 12:35:49 +02:00
private static <R extends Result> void prepareInfo(
SparkSession spark,
String inputPath,
String outputPath,
Class<R> resultClazz,
String resultType,
List<String> allowedsemrel) {
2020-04-16 15:53:34 +02:00
2020-04-23 12:35:49 +02:00
// read the relation table and the table related to the result it is using
2020-04-16 15:53:34 +02:00
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
2020-04-23 12:35:49 +02:00
org.apache.spark.sql.Dataset<Relation> relation =
spark.createDataset(
sc.textFile(inputPath + "/relation")
.map(item -> OBJECT_MAPPER.readValue(item, Relation.class))
.rdd(),
Encoders.bean(Relation.class));
2020-04-16 15:53:34 +02:00
relation.createOrReplaceTempView("relation");
log.info("Reading Graph table from: {}", inputPath + "/" + resultType);
Dataset<R> result = readPathEntity(spark, inputPath + "/" + resultType, resultClazz);
result.createOrReplaceTempView("result");
2020-04-16 15:53:34 +02:00
getPossibleResultOrcidAssociation(spark, allowedsemrel, outputPath + "/" + resultType);
}
2020-04-16 15:53:34 +02:00
2020-04-23 12:35:49 +02:00
private static void getPossibleResultOrcidAssociation(
SparkSession spark, List<String> allowedsemrel, String outputPath) {
String query =
" select target resultId, author authorList"
+ " from (select id, collect_set(named_struct('name', name, 'surname', surname, 'fullname', fullname, 'orcid', orcid)) author "
+ " from ( "
+ " select id, MyT.fullname, MyT.name, MyT.surname, MyP.value orcid "
+ " from result "
+ " lateral view explode (author) a as MyT "
+ " lateral view explode (MyT.pid) p as MyP "
+ " where MyP.qualifier.classid = 'ORCID') tmp "
+ " group by id) r_t "
+ " join ("
+ " select source, target "
+ " from relation "
+ " where datainfo.deletedbyinference = false "
+ getConstraintList(" relclass = '", allowedsemrel)
+ ") rel_rel "
+ " on source = id";
spark.sql(query)
.as(Encoders.bean(ResultOrcidList.class))
.toJavaRDD()
.map(r -> OBJECT_MAPPER.writeValueAsString(r))
.saveAsTextFile(outputPath, GzipCodec.class);
2020-04-23 12:35:49 +02:00
// .toJSON()
// .write()
// .mode(SaveMode.Append)
// .option("compression","gzip")
// .text(outputPath)
// ;
2020-04-16 15:53:34 +02:00
}
}