dnet-hadoop/dhp-workflows/dhp-broker-events/src/main/java/eu/dnetlib/dhp/broker/oa/PrepareRelatedSoftwaresJob....

84 lines
2.9 KiB
Java
Raw Normal View History

2020-06-22 08:51:31 +02:00
package eu.dnetlib.dhp.broker.oa;
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
import java.util.Optional;
import org.apache.commons.io.IOUtils;
import org.apache.spark.SparkConf;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.SaveMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2020-06-23 08:37:35 +02:00
import eu.dnetlib.broker.objects.OaBrokerRelatedSoftware;
2020-06-22 08:51:31 +02:00
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
2020-06-23 09:32:32 +02:00
import eu.dnetlib.dhp.broker.oa.util.BrokerConstants;
2020-06-22 08:51:31 +02:00
import eu.dnetlib.dhp.broker.oa.util.ClusterUtils;
import eu.dnetlib.dhp.broker.oa.util.ConversionUtils;
import eu.dnetlib.dhp.broker.oa.util.aggregators.withRels.RelatedSoftware;
2020-06-23 09:32:32 +02:00
import eu.dnetlib.dhp.schema.common.ModelConstants;
2020-06-22 08:51:31 +02:00
import eu.dnetlib.dhp.schema.oaf.Relation;
import eu.dnetlib.dhp.schema.oaf.Software;
2020-06-22 11:45:14 +02:00
public class PrepareRelatedSoftwaresJob {
2020-06-22 08:51:31 +02:00
2020-06-22 11:45:14 +02:00
private static final Logger log = LoggerFactory.getLogger(PrepareRelatedSoftwaresJob.class);
2020-06-22 08:51:31 +02:00
public static void main(final String[] args) throws Exception {
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
IOUtils
2020-06-23 09:32:32 +02:00
.toString(
PrepareRelatedSoftwaresJob.class
.getResourceAsStream("/eu/dnetlib/dhp/broker/oa/common_params.json")));
2020-06-22 08:51:31 +02:00
parser.parseArgument(args);
final Boolean isSparkSessionManaged = Optional
.ofNullable(parser.get("isSparkSessionManaged"))
.map(Boolean::valueOf)
.orElse(Boolean.TRUE);
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
final String graphPath = parser.get("graphPath");
log.info("graphPath: {}", graphPath);
2020-06-22 11:45:14 +02:00
final String workingPath = parser.get("workingPath");
log.info("workingPath: {}", workingPath);
final String relsPath = workingPath + "/relatedSoftwares";
2020-06-22 08:51:31 +02:00
log.info("relsPath: {}", relsPath);
final SparkConf conf = new SparkConf();
runWithSparkSession(conf, isSparkSessionManaged, spark -> {
ClusterUtils.removeDir(spark, relsPath);
2020-06-22 11:45:14 +02:00
2020-06-23 08:37:35 +02:00
final Dataset<OaBrokerRelatedSoftware> softwares = ClusterUtils
.readPath(spark, graphPath + "/software", Software.class)
.filter(sw -> !ClusterUtils.isDedupRoot(sw.getId()))
.map(ConversionUtils::oafSoftwareToBrokerSoftware, Encoders.bean(OaBrokerRelatedSoftware.class));
2020-06-22 08:51:31 +02:00
2020-06-23 08:37:35 +02:00
final Dataset<Relation> rels = ClusterUtils
.readPath(spark, graphPath + "/relation", Relation.class)
2020-06-24 15:47:06 +02:00
.filter(r -> r.getDataInfo().getDeletedbyinference())
2020-06-23 09:32:32 +02:00
.filter(r -> r.getRelType().equals(ModelConstants.RESULT_RESULT))
.filter(r -> !r.getRelClass().equals(BrokerConstants.IS_MERGED_IN_CLASS))
2020-06-23 08:37:35 +02:00
.filter(r -> !ClusterUtils.isDedupRoot(r.getSource()))
.filter(r -> !ClusterUtils.isDedupRoot(r.getTarget()));
2020-06-22 08:51:31 +02:00
rels
2020-06-23 08:37:35 +02:00
.joinWith(softwares, softwares.col("openaireId").equalTo(rels.col("target")), "inner")
2020-06-23 09:32:32 +02:00
.map(t -> new RelatedSoftware(t._1.getSource(), t._2), Encoders.bean(RelatedSoftware.class))
2020-06-22 08:51:31 +02:00
.write()
.mode(SaveMode.Overwrite)
.json(relsPath);
});
}
}