2020-04-18 14:09:03 +02:00
|
|
|
package eu.dnetlib.dhp.resulttocommunityfromsemrel;
|
|
|
|
|
2020-04-27 10:34:03 +02:00
|
|
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2020-04-18 14:09:03 +02:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.resulttocommunityfromorganization.ResultCommunityList;
|
2020-04-27 10:34:03 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2020-04-18 14:09:03 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.hadoop.io.compress.GzipCodec;
|
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
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;
|
|
|
|
import scala.Tuple2;
|
|
|
|
|
|
|
|
public class PrepareResultCommunitySetStep2 {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(PrepareResultCommunitySetStep2.class);
|
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
2020-04-27 10:34:03 +02:00
|
|
|
String jsonConfiguration =
|
|
|
|
IOUtils.toString(
|
|
|
|
PrepareResultCommunitySetStep2.class.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/resulttocommunityfromsemrel/input_preparecommunitytoresult2_parameters.json"));
|
2020-04-18 14:09:03 +02:00
|
|
|
|
2020-04-27 10:34:03 +02:00
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
2020-04-18 14:09:03 +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);
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
|
2020-04-27 10:34:03 +02:00
|
|
|
runWithSparkSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
2020-04-18 14:09:03 +02:00
|
|
|
spark -> {
|
|
|
|
if (isTest(parser)) {
|
|
|
|
removeOutputDir(spark, outputPath);
|
|
|
|
}
|
2020-04-27 10:34:03 +02:00
|
|
|
mergeInfo(spark, inputPath, outputPath);
|
2020-04-18 14:09:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void mergeInfo(SparkSession spark, String inputPath, String outputPath) {
|
|
|
|
|
2020-04-27 10:34:03 +02:00
|
|
|
Dataset<ResultCommunityList> resultOrcidAssocCommunityList =
|
|
|
|
readResultCommunityList(spark, inputPath + "/publication")
|
|
|
|
.union(readResultCommunityList(spark, inputPath + "/dataset"))
|
|
|
|
.union(readResultCommunityList(spark, inputPath + "/otherresearchproduct"))
|
|
|
|
.union(readResultCommunityList(spark, inputPath + "/software"));
|
2020-04-18 14:09:03 +02:00
|
|
|
|
|
|
|
resultOrcidAssocCommunityList
|
|
|
|
.toJavaRDD()
|
|
|
|
.mapToPair(r -> new Tuple2<>(r.getResultId(), r))
|
2020-04-27 10:34:03 +02:00
|
|
|
.reduceByKey(
|
|
|
|
(a, b) -> {
|
|
|
|
if (a == null) {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
if (b == null) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
Set<String> community_set = new HashSet<>();
|
|
|
|
|
|
|
|
a.getCommunityList().stream().forEach(aa -> community_set.add(aa));
|
|
|
|
|
|
|
|
b.getCommunityList().stream()
|
|
|
|
.forEach(
|
|
|
|
aa -> {
|
|
|
|
if (!community_set.contains(aa)) {
|
|
|
|
a.getCommunityList().add(aa);
|
|
|
|
community_set.add(aa);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return a;
|
|
|
|
})
|
2020-04-18 14:09:03 +02:00
|
|
|
.map(c -> c._2())
|
|
|
|
.map(r -> OBJECT_MAPPER.writeValueAsString(r))
|
|
|
|
.saveAsTextFile(outputPath, GzipCodec.class);
|
|
|
|
}
|
|
|
|
|
2020-04-27 10:34:03 +02:00
|
|
|
private static Dataset<ResultCommunityList> readResultCommunityList(
|
|
|
|
SparkSession spark, String relationPath) {
|
|
|
|
return spark.read()
|
2020-04-18 14:09:03 +02:00
|
|
|
.textFile(relationPath)
|
2020-04-27 10:34:03 +02:00
|
|
|
.map(
|
|
|
|
value -> OBJECT_MAPPER.readValue(value, ResultCommunityList.class),
|
|
|
|
Encoders.bean(ResultCommunityList.class));
|
2020-04-18 14:09:03 +02:00
|
|
|
}
|
|
|
|
}
|