2020-11-19 19:15:39 +01:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.oa.graph.dump.funderresults;
|
|
|
|
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.Optional;
|
2022-06-21 17:52:27 +02:00
|
|
|
import java.util.stream.Collectors;
|
2020-11-19 19:15:39 +01:00
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
2022-06-21 17:52:27 +02:00
|
|
|
import org.apache.spark.api.java.function.FilterFunction;
|
|
|
|
import org.apache.spark.api.java.function.FlatMapFunction;
|
2020-11-20 12:21:33 +01:00
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
2020-11-24 14:44:53 +01:00
|
|
|
import org.apache.spark.api.java.function.MapGroupsFunction;
|
2020-11-19 19:15:39 +01:00
|
|
|
import org.apache.spark.sql.Dataset;
|
2020-11-20 12:21:33 +01:00
|
|
|
import org.apache.spark.sql.Encoders;
|
2020-11-19 19:15:39 +01:00
|
|
|
import org.apache.spark.sql.SaveMode;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2022-06-21 17:52:27 +02:00
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.Constants;
|
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.DumpProducts;
|
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.ResultMapper;
|
2020-11-19 19:15:39 +01:00
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.Utils;
|
2022-06-21 17:52:27 +02:00
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.community.CommunityMap;
|
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.community.ResultProject;
|
2020-11-25 18:23:28 +01:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
2022-06-21 17:52:27 +02:00
|
|
|
import eu.dnetlib.dhp.schema.dump.oaf.community.CommunityResult;
|
2021-08-06 18:56:18 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Project;
|
2020-11-19 19:15:39 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Result;
|
2022-06-21 17:52:27 +02:00
|
|
|
import scala.Tuple2;
|
2021-08-06 18:56:18 +02:00
|
|
|
|
2020-11-25 14:08:09 +01:00
|
|
|
/**
|
|
|
|
* Selects the results linked to projects. Only for these results the dump will be performed.
|
2021-08-06 18:56:18 +02:00
|
|
|
* The code to perform the dump and to expend the dumped results with the information related to projects
|
2020-11-25 14:08:09 +01:00
|
|
|
* is the one used for the dump of the community products
|
|
|
|
*/
|
2020-11-19 19:15:39 +01:00
|
|
|
public class SparkResultLinkedToProject implements Serializable {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(SparkResultLinkedToProject.class);
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
String jsonConfiguration = IOUtils
|
2022-07-01 10:44:08 +02:00
|
|
|
.toString(
|
|
|
|
SparkResultLinkedToProject.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/oa/graph/dump/input_parameters_link_prj.json"));
|
2020-11-19 19:15:39 +01:00
|
|
|
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
Boolean isSparkSessionManaged = Optional
|
2022-07-01 10:44:08 +02:00
|
|
|
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
2020-11-19 19:15:39 +01:00
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
final 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);
|
|
|
|
|
2022-06-21 17:52:27 +02:00
|
|
|
final String resultProjectsPath = parser.get("graphPath");
|
|
|
|
log.info("graphPath: {}", resultProjectsPath);
|
|
|
|
|
|
|
|
String communityMapPath = parser.get("communityMapPath");
|
2020-11-19 19:15:39 +01:00
|
|
|
|
2021-08-11 12:13:22 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2020-11-19 19:15:39 +01:00
|
|
|
Class<? extends Result> inputClazz = (Class<? extends Result>) Class.forName(resultClassName);
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
|
|
|
|
runWithSparkSession(
|
2022-07-01 10:44:08 +02:00
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
|
|
|
spark -> {
|
|
|
|
Utils.removeOutputDir(spark, outputPath);
|
|
|
|
writeResultsLinkedToProjects(
|
|
|
|
communityMapPath, spark, inputClazz, inputPath, outputPath, resultProjectsPath);
|
|
|
|
});
|
2020-11-19 19:15:39 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 17:52:27 +02:00
|
|
|
private static <R extends Result> void writeResultsLinkedToProjects(String communityMapPath, SparkSession spark,
|
2022-07-01 10:44:08 +02:00
|
|
|
Class<R> inputClazz,
|
|
|
|
String inputPath, String outputPath, String resultProjectsPath) {
|
2020-11-19 19:15:39 +01:00
|
|
|
|
2020-11-24 14:44:53 +01:00
|
|
|
Dataset<R> results = Utils
|
2022-07-01 10:44:08 +02:00
|
|
|
.readPath(spark, inputPath, inputClazz)
|
|
|
|
.filter(
|
|
|
|
(FilterFunction<R>) r -> !r.getDataInfo().getDeletedbyinference() &&
|
|
|
|
!r.getDataInfo().getInvisible());
|
2022-06-21 17:52:27 +02:00
|
|
|
Dataset<ResultProject> resultProjectDataset = Utils
|
2022-07-01 10:44:08 +02:00
|
|
|
.readPath(spark, resultProjectsPath, ResultProject.class);
|
2022-06-21 17:52:27 +02:00
|
|
|
CommunityMap communityMap = Utils.getCommunityMap(spark, communityMapPath);
|
|
|
|
results
|
2022-07-01 10:44:08 +02:00
|
|
|
.joinWith(resultProjectDataset, results.col("id").equalTo(resultProjectDataset.col("resultId")))
|
|
|
|
.map((MapFunction<Tuple2<R, ResultProject>, CommunityResult>) t2 -> {
|
|
|
|
CommunityResult cr = (CommunityResult) ResultMapper
|
|
|
|
.map(
|
|
|
|
t2._1(),
|
|
|
|
communityMap, Constants.DUMPTYPE.FUNDER.getType());
|
|
|
|
cr.setProjects(t2._2().getProjectsList());
|
|
|
|
return cr;
|
|
|
|
}, Encoders.bean(CommunityResult.class))
|
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.option("compression", "gzip")
|
|
|
|
.json(outputPath);
|
2021-10-01 12:59:08 +02:00
|
|
|
|
2020-11-19 19:15:39 +01:00
|
|
|
}
|
|
|
|
}
|