2021-11-10 17:00:37 +01:00
|
|
|
|
2021-11-11 16:11:25 +01:00
|
|
|
package eu.dnetlib.dhp.actionmanager.createunresolvedentities;
|
2021-11-08 16:44:19 +01:00
|
|
|
|
2021-12-22 16:44:44 +01:00
|
|
|
import static eu.dnetlib.dhp.actionmanager.Constants.*;
|
2021-11-10 17:00:37 +01:00
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
2021-11-11 16:11:25 +01:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
2021-11-10 17:00:37 +01:00
|
|
|
|
2021-11-08 16:44:19 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
2021-11-11 16:11:25 +01:00
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
2021-11-08 16:44:19 +01:00
|
|
|
import org.apache.spark.sql.Dataset;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.SaveMode;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-11-11 16:11:25 +01:00
|
|
|
import eu.dnetlib.dhp.actionmanager.createunresolvedentities.model.FOSDataModel;
|
2021-11-10 17:00:37 +01:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2021-11-11 16:11:25 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Result;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.StructuredProperty;
|
2021-11-15 09:58:48 +01:00
|
|
|
import eu.dnetlib.dhp.utils.DHPUtils;
|
2021-11-08 16:44:19 +01:00
|
|
|
|
2021-11-09 11:25:41 +01:00
|
|
|
public class PrepareFOSSparkJob implements Serializable {
|
2021-11-10 17:00:37 +01:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(PrepareFOSSparkJob.class);
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
|
|
|
String jsonConfiguration = IOUtils
|
|
|
|
.toString(
|
|
|
|
PrepareFOSSparkJob.class
|
|
|
|
.getResourceAsStream(
|
2021-11-12 10:18:01 +01:00
|
|
|
"/eu/dnetlib/dhp/actionmanager/createunresolvedentities/prepare_parameters.json"));
|
2021-11-10 17:00:37 +01:00
|
|
|
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
|
|
|
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
Boolean isSparkSessionManaged = isSparkSessionManaged(parser);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
String sourcePath = parser.get("sourcePath");
|
|
|
|
log.info("sourcePath: {}", sourcePath);
|
|
|
|
|
|
|
|
final String outputPath = parser.get("outputPath");
|
|
|
|
log.info("outputPath: {}", outputPath);
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
runWithSparkSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
|
|
|
spark -> {
|
|
|
|
distributeFOSdois(
|
|
|
|
spark,
|
|
|
|
sourcePath,
|
|
|
|
|
|
|
|
outputPath);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void distributeFOSdois(SparkSession spark, String sourcePath, String outputPath) {
|
|
|
|
Dataset<FOSDataModel> fosDataset = readPath(spark, sourcePath, FOSDataModel.class);
|
|
|
|
|
2021-12-22 16:44:44 +01:00
|
|
|
fosDataset.map((MapFunction<FOSDataModel, Result>) value -> {
|
2021-11-11 16:11:25 +01:00
|
|
|
Result r = new Result();
|
2021-11-12 10:56:58 +01:00
|
|
|
r.setId(DHPUtils.generateUnresolvedIdentifier(value.getDoi(), DOI));
|
2021-11-11 16:11:25 +01:00
|
|
|
r.setSubject(getSubjects(value));
|
|
|
|
return r;
|
|
|
|
}, Encoders.bean(Result.class))
|
2021-11-10 17:00:37 +01:00
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.option("compression", "gzip")
|
2021-11-11 16:11:25 +01:00
|
|
|
.json(outputPath + "/fos");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<StructuredProperty> getSubjects(FOSDataModel fos) {
|
|
|
|
return Arrays
|
2021-12-22 16:44:44 +01:00
|
|
|
.asList(getSubject(fos.getLevel1(), FOS_CLASS_ID, FOS_CLASS_NAME),
|
|
|
|
getSubject(fos.getLevel2(), FOS_CLASS_ID, FOS_CLASS_NAME),
|
|
|
|
getSubject(fos.getLevel3(), FOS_CLASS_ID, FOS_CLASS_NAME))
|
2021-11-11 16:11:25 +01:00
|
|
|
.stream()
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-09 11:25:41 +01:00
|
|
|
|
2021-11-08 16:44:19 +01:00
|
|
|
}
|