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

85 lines
3.0 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;
2020-06-30 16:17:09 +02:00
import org.apache.spark.util.LongAccumulator;
2020-06-22 08:51:31 +02:00
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-12-10 14:47:22 +01:00
final String workingDir = parser.get("workingDir");
log.info("workingDir: {}", workingDir);
2020-06-22 11:45:14 +02:00
2020-12-10 14:47:22 +01:00
final String relsPath = workingDir + "/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-30 16:17:09 +02:00
final LongAccumulator total = spark.sparkContext().longAccumulator("total_rels");
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
2020-12-15 08:30:26 +01:00
.loadRelations(graphPath, spark)
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
2020-06-30 16:17:09 +02:00
final Dataset<RelatedSoftware> dataset = rels
2020-06-23 08:37:35 +02:00
.joinWith(softwares, softwares.col("openaireId").equalTo(rels.col("target")), "inner")
2020-06-30 16:17:09 +02:00
.map(t -> new RelatedSoftware(t._1.getSource(), t._2), Encoders.bean(RelatedSoftware.class));
ClusterUtils.save(dataset, relsPath, RelatedSoftware.class, total);
2020-06-22 08:51:31 +02:00
});
}
}