2020-11-25 14:55:32 +01:00
|
|
|
|
2022-01-19 12:24:52 +01:00
|
|
|
package eu.dnetlib.dhp.oa.merge;
|
2020-11-25 14:55:32 +01:00
|
|
|
|
2022-01-19 12:30:52 +01:00
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
2020-11-25 14:55:32 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
import org.apache.spark.api.java.function.FilterFunction;
|
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
2023-08-08 15:52:20 +02:00
|
|
|
import org.apache.spark.sql.*;
|
2020-11-25 14:55:32 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2022-01-19 12:30:52 +01:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.common.HdfsSupport;
|
2023-08-08 15:52:20 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelSupport;
|
2020-11-25 14:55:32 +01:00
|
|
|
|
|
|
|
public class DispatchEntitiesSparkJob {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(DispatchEntitiesSparkJob.class);
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
|
|
|
String jsonConfiguration = IOUtils
|
|
|
|
.toString(
|
2021-08-11 12:13:22 +02:00
|
|
|
Objects
|
|
|
|
.requireNonNull(
|
|
|
|
DispatchEntitiesSparkJob.class
|
|
|
|
.getResourceAsStream(
|
2022-01-19 17:13:21 +01:00
|
|
|
"/eu/dnetlib/dhp/oa/merge/dispatch_entities_parameters.json")));
|
2020-11-25 14:55:32 +01:00
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
Boolean isSparkSessionManaged = Optional
|
|
|
|
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
String inputPath = parser.get("inputPath");
|
|
|
|
log.info("inputPath: {}", inputPath);
|
|
|
|
|
|
|
|
String outputPath = parser.get("outputPath");
|
|
|
|
log.info("outputPath: {}", outputPath);
|
|
|
|
|
2023-09-12 14:34:28 +02:00
|
|
|
boolean filterInvisible = Boolean.parseBoolean(parser.get("filterInvisible"));
|
2023-08-08 15:52:20 +02:00
|
|
|
log.info("filterInvisible: {}", filterInvisible);
|
2020-11-25 14:55:32 +01:00
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
runWithSparkSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
2023-09-12 14:34:28 +02:00
|
|
|
spark -> dispatchEntities(spark, inputPath, outputPath, filterInvisible));
|
2020-11-25 14:55:32 +01:00
|
|
|
}
|
|
|
|
|
2023-08-08 15:52:20 +02:00
|
|
|
private static void dispatchEntities(
|
2020-11-25 14:55:32 +01:00
|
|
|
SparkSession spark,
|
|
|
|
String inputPath,
|
2023-08-08 15:52:20 +02:00
|
|
|
String outputPath,
|
|
|
|
boolean filterInvisible) {
|
|
|
|
|
|
|
|
Dataset<String> df = spark.read().textFile(inputPath);
|
|
|
|
|
|
|
|
ModelSupport.oafTypes.entrySet().parallelStream().forEach(entry -> {
|
|
|
|
String entityType = entry.getKey();
|
|
|
|
Class<?> clazz = entry.getValue();
|
|
|
|
|
2023-09-12 14:34:28 +02:00
|
|
|
final String entityPath = outputPath + "/" + entityType;
|
2023-08-08 15:52:20 +02:00
|
|
|
if (!entityType.equalsIgnoreCase("relation")) {
|
2023-09-12 14:34:28 +02:00
|
|
|
HdfsSupport.remove(entityPath, spark.sparkContext().hadoopConfiguration());
|
2023-08-08 15:52:20 +02:00
|
|
|
Dataset<Row> entityDF = spark
|
|
|
|
.read()
|
|
|
|
.schema(Encoders.bean(clazz).schema())
|
|
|
|
.json(
|
|
|
|
df
|
|
|
|
.filter((FilterFunction<String>) s -> s.startsWith(clazz.getName()))
|
|
|
|
.map(
|
|
|
|
(MapFunction<String, String>) s -> StringUtils.substringAfter(s, "|"),
|
|
|
|
Encoders.STRING()));
|
|
|
|
|
|
|
|
if (filterInvisible) {
|
|
|
|
entityDF = entityDF.filter("dataInfo.invisible != true");
|
|
|
|
}
|
|
|
|
|
|
|
|
entityDF
|
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.option("compression", "gzip")
|
2023-09-12 14:34:28 +02:00
|
|
|
.json(entityPath);
|
2023-08-08 15:52:20 +02:00
|
|
|
}
|
|
|
|
});
|
2020-11-25 14:55:32 +01:00
|
|
|
}
|
|
|
|
}
|