2020-11-18 16:47:31 +01:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.oa.graph.dump.funderresults;
|
|
|
|
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
2022-03-23 16:08:14 +01:00
|
|
|
import java.util.ArrayList;
|
2021-08-11 12:13:22 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Optional;
|
2022-03-23 16:08:14 +01:00
|
|
|
import java.util.stream.Collectors;
|
2020-11-18 16:47:31 +01:00
|
|
|
|
2022-03-23 16:08:14 +01:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2020-11-18 16:47:31 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
2022-03-23 16:08:14 +01:00
|
|
|
import org.apache.spark.api.java.function.FlatMapFunction;
|
|
|
|
import org.apache.spark.api.java.function.ForeachFunction;
|
2020-11-18 16:47:31 +01:00
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
2022-03-23 16:08:14 +01:00
|
|
|
import org.apache.spark.api.java.function.MapGroupsFunction;
|
2020-11-24 14:44:53 +01:00
|
|
|
import org.apache.spark.sql.*;
|
2022-03-23 16:08:14 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2020-11-18 16:47:31 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.Utils;
|
2020-11-19 19:15:39 +01:00
|
|
|
import eu.dnetlib.dhp.schema.dump.oaf.community.CommunityResult;
|
2020-11-24 14:44:53 +01:00
|
|
|
import eu.dnetlib.dhp.schema.dump.oaf.community.Project;
|
2022-03-23 16:08:14 +01:00
|
|
|
import scala.Tuple2;
|
2020-11-18 16:47:31 +01:00
|
|
|
|
|
|
|
/**
|
2020-11-25 14:08:09 +01:00
|
|
|
* Splits the dumped results by funder and stores them in a folder named as the funder nsp (for all the funders, but the EC
|
|
|
|
* for the EC it specifies also the fundingStream (FP7 or H2020)
|
2020-11-18 16:47:31 +01:00
|
|
|
*/
|
2020-11-18 18:56:48 +01:00
|
|
|
public class SparkDumpFunderResults implements Serializable {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(SparkDumpFunderResults.class);
|
2020-11-18 16:47:31 +01:00
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
String jsonConfiguration = IOUtils
|
|
|
|
.toString(
|
2020-11-18 18:56:48 +01:00
|
|
|
SparkDumpFunderResults.class
|
2020-11-18 16:47:31 +01:00
|
|
|
.getResourceAsStream(
|
2020-11-18 16:58:20 +01:00
|
|
|
"/eu/dnetlib/dhp/oa/graph/dump/funder_result_parameters.json"));
|
2020-11-18 16:47:31 +01:00
|
|
|
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
Boolean isSparkSessionManaged = Optional
|
2021-10-01 12:59:47 +02:00
|
|
|
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
2020-11-18 16:47:31 +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);
|
|
|
|
|
2021-08-06 18:56:18 +02:00
|
|
|
final String graphPath = parser.get("graphPath");
|
|
|
|
log.info("relationPath: {}", graphPath);
|
2020-11-18 16:47:31 +01:00
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
|
|
|
|
runWithSparkSession(
|
2021-10-01 12:59:47 +02:00
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
|
|
|
spark -> {
|
|
|
|
Utils.removeOutputDir(spark, outputPath);
|
|
|
|
writeResultProjectList(spark, inputPath, outputPath, graphPath);
|
|
|
|
});
|
2020-11-18 16:47:31 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 14:44:53 +01:00
|
|
|
private static void writeResultProjectList(SparkSession spark, String inputPath, String outputPath,
|
2021-10-01 12:59:47 +02:00
|
|
|
String graphPath) {
|
2020-11-18 16:47:31 +01:00
|
|
|
|
2022-03-23 16:08:14 +01:00
|
|
|
Dataset<String> funderList = Utils
|
|
|
|
.readPath(spark, inputPath + "/publication", CommunityResult.class)
|
|
|
|
.union(Utils.readPath(spark, inputPath + "/dataset", CommunityResult.class))
|
|
|
|
.union(Utils.readPath(spark, inputPath + "/otherresearchproduct", CommunityResult.class))
|
|
|
|
.union(Utils.readPath(spark, inputPath + "/software", CommunityResult.class))
|
|
|
|
.flatMap((FlatMapFunction<CommunityResult, String>) cr ->
|
|
|
|
cr.getProjects().stream().map(p -> p.getFunder().getShortName()).collect(Collectors.toList()).iterator()
|
|
|
|
, Encoders.STRING())
|
|
|
|
.distinct();
|
2020-11-18 16:47:31 +01:00
|
|
|
|
2022-03-23 17:10:19 +01:00
|
|
|
Dataset<CommunityResult> pubs;
|
|
|
|
Dataset<CommunityResult> result ;
|
|
|
|
pubs = Utils
|
|
|
|
.readPath(spark, inputPath + "/publication", CommunityResult.class);
|
|
|
|
Dataset<CommunityResult> dats = Utils.readPath(spark, inputPath + "/dataset", CommunityResult.class);
|
|
|
|
Dataset<CommunityResult> orp = Utils.readPath(spark, inputPath + "/otherresearchproduct", CommunityResult.class);
|
|
|
|
Dataset<CommunityResult> sw = Utils.readPath(spark, inputPath + "/software", CommunityResult.class);
|
|
|
|
result = pubs.union(dats).union(orp).union(sw);
|
|
|
|
|
2022-03-23 16:08:14 +01:00
|
|
|
funderList.foreach((ForeachFunction<String>) funder ->
|
|
|
|
getFunderResult(funder, inputPath, spark)
|
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.option("compression", "gzip")
|
|
|
|
.json(outputPath + "/" + funder)
|
|
|
|
|
|
|
|
);
|
2020-11-24 14:44:53 +01:00
|
|
|
|
|
|
|
|
2020-11-18 16:47:31 +01:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:31:45 +02:00
|
|
|
|
2022-03-23 16:08:14 +01:00
|
|
|
@Nullable
|
|
|
|
private static Dataset<CommunityResult> getFunderResult(String funderName, String inputPath, SparkSession spark) {
|
2022-03-23 17:10:19 +01:00
|
|
|
Dataset<CommunityResult> pubs;
|
|
|
|
Dataset<CommunityResult> result ;
|
|
|
|
pubs = Utils
|
|
|
|
.readPath(spark, inputPath + "/publication", CommunityResult.class);
|
|
|
|
Dataset<CommunityResult> dats = Utils.readPath(spark, inputPath + "/dataset", CommunityResult.class);
|
|
|
|
Dataset<CommunityResult> orp = Utils.readPath(spark, inputPath + "/otherresearchproduct", CommunityResult.class);
|
|
|
|
Dataset<CommunityResult> sw = Utils.readPath(spark, inputPath + "/software", CommunityResult.class);
|
|
|
|
result = pubs.union(dats).union(orp).union(sw);
|
|
|
|
Dataset<CommunityResult> tmp = result.map((MapFunction<CommunityResult, CommunityResult>) cr -> {
|
2022-03-23 16:08:14 +01:00
|
|
|
if (!Optional.ofNullable(cr.getProjects()).isPresent()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (Project p : cr.getProjects()) {
|
|
|
|
if (p.getFunder().getShortName().equalsIgnoreCase(funderName)) {
|
|
|
|
return cr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}, Encoders.bean(CommunityResult.class))
|
|
|
|
.filter(Objects::nonNull);
|
2022-03-23 17:10:19 +01:00
|
|
|
System.out.println(tmp.count());
|
|
|
|
return tmp;
|
2021-07-13 14:31:45 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:08:14 +01:00
|
|
|
|
2020-11-18 16:47:31 +01:00
|
|
|
}
|