2020-05-08 16:49:47 +02:00
|
|
|
|
2020-05-07 12:31:26 +02:00
|
|
|
package eu.dnetlib.dhp.broker.oa;
|
|
|
|
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
2020-05-19 16:17:35 +02:00
|
|
|
import org.apache.hadoop.io.compress.GzipCodec;
|
2020-05-07 12:31:26 +02:00
|
|
|
import org.apache.spark.SparkConf;
|
2020-05-19 16:17:35 +02:00
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
|
|
|
import org.apache.spark.sql.Column;
|
|
|
|
import org.apache.spark.sql.Dataset;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.Row;
|
2020-05-07 12:31:26 +02:00
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2020-05-19 16:17:35 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
2020-05-07 12:31:26 +02:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.broker.model.Event;
|
|
|
|
import eu.dnetlib.dhp.broker.model.EventFactory;
|
2020-05-15 12:25:37 +02:00
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMissingAbstract;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMissingAuthorOrcid;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMissingOpenAccess;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMissingPid;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMissingProject;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMissingPublicationDate;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMissingSubject;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMoreOpenAccess;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMorePid;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.EnrichMoreSubject;
|
|
|
|
import eu.dnetlib.dhp.broker.oa.matchers.UpdateMatcher;
|
2020-05-07 12:31:26 +02:00
|
|
|
import eu.dnetlib.dhp.broker.oa.util.UpdateInfo;
|
|
|
|
import eu.dnetlib.dhp.common.HdfsSupport;
|
2020-05-19 16:17:35 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.OtherResearchProduct;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Publication;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
2020-05-07 12:31:26 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Result;
|
2020-05-19 16:17:35 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Software;
|
2020-05-07 12:31:26 +02:00
|
|
|
|
|
|
|
public class GenerateEventsApplication {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(GenerateEventsApplication.class);
|
|
|
|
|
2020-05-13 12:00:27 +02:00
|
|
|
private static final UpdateMatcher<?> enrichMissingAbstract = new EnrichMissingAbstract();
|
|
|
|
private static final UpdateMatcher<?> enrichMissingAuthorOrcid = new EnrichMissingAuthorOrcid();
|
|
|
|
private static final UpdateMatcher<?> enrichMissingOpenAccess = new EnrichMissingOpenAccess();
|
|
|
|
private static final UpdateMatcher<?> enrichMissingPid = new EnrichMissingPid();
|
|
|
|
private static final UpdateMatcher<?> enrichMissingProject = new EnrichMissingProject();
|
|
|
|
private static final UpdateMatcher<?> enrichMissingPublicationDate = new EnrichMissingPublicationDate();
|
|
|
|
private static final UpdateMatcher<?> enrichMissingSubject = new EnrichMissingSubject();
|
|
|
|
private static final UpdateMatcher<?> enrichMoreOpenAccess = new EnrichMoreOpenAccess();
|
|
|
|
private static final UpdateMatcher<?> enrichMorePid = new EnrichMorePid();
|
|
|
|
private static final UpdateMatcher<?> enrichMoreSubject = new EnrichMoreSubject();
|
2020-05-07 12:31:26 +02:00
|
|
|
|
2020-05-19 16:17:35 +02:00
|
|
|
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
|
2020-05-07 12:31:26 +02:00
|
|
|
public static void main(final String[] args) throws Exception {
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
2020-05-08 16:49:47 +02:00
|
|
|
IOUtils
|
2020-05-19 16:17:35 +02:00
|
|
|
.toString(GenerateEventsApplication.class
|
|
|
|
.getResourceAsStream("/eu/dnetlib/dhp/oa/graph/merge_claims_parameters.json")));
|
2020-05-07 12:31:26 +02:00
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
final Boolean isSparkSessionManaged = Optional
|
|
|
|
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
final String graphPath = parser.get("graphPath");
|
|
|
|
log.info("graphPath: {}", graphPath);
|
|
|
|
|
|
|
|
final String eventsPath = parser.get("eventsPath");
|
|
|
|
log.info("eventsPath: {}", eventsPath);
|
|
|
|
|
2020-05-19 16:17:35 +02:00
|
|
|
final String resultClassName = parser.get("resultTableName");
|
|
|
|
log.info("resultTableName: {}", resultClassName);
|
|
|
|
|
2020-05-07 12:31:26 +02:00
|
|
|
final SparkConf conf = new SparkConf();
|
2020-05-19 16:17:35 +02:00
|
|
|
|
2020-05-07 12:31:26 +02:00
|
|
|
runWithSparkSession(conf, isSparkSessionManaged, spark -> {
|
2020-05-19 16:17:35 +02:00
|
|
|
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
2020-05-07 12:31:26 +02:00
|
|
|
removeOutputDir(spark, eventsPath);
|
2020-05-19 16:17:35 +02:00
|
|
|
|
|
|
|
final JavaRDD<Event> eventsRdd = sc.emptyRDD();
|
|
|
|
|
|
|
|
eventsRdd.union(generateSimpleEvents(spark, graphPath, Publication.class));
|
|
|
|
eventsRdd.union(generateSimpleEvents(spark, graphPath, eu.dnetlib.dhp.schema.oaf.Dataset.class));
|
|
|
|
eventsRdd.union(generateSimpleEvents(spark, graphPath, Software.class));
|
|
|
|
eventsRdd.union(generateSimpleEvents(spark, graphPath, OtherResearchProduct.class));
|
|
|
|
|
|
|
|
eventsRdd.saveAsTextFile(eventsPath, GzipCodec.class);
|
2020-05-07 12:31:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void removeOutputDir(final SparkSession spark, final String path) {
|
|
|
|
HdfsSupport.remove(path, spark.sparkContext().hadoopConfiguration());
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:17:35 +02:00
|
|
|
private static <R extends Result> JavaRDD<Event> generateSimpleEvents(final SparkSession spark,
|
|
|
|
final String graphPath,
|
|
|
|
final Class<R> resultClazz) {
|
|
|
|
|
|
|
|
final Dataset<R> results =
|
|
|
|
readPath(spark, graphPath + "/" + resultClazz.getSimpleName().toLowerCase(), resultClazz)
|
|
|
|
.filter(r -> r.getDataInfo().getDeletedbyinference());
|
|
|
|
|
|
|
|
final Dataset<Relation> rels =
|
|
|
|
readPath(spark, graphPath + "/relation", Relation.class)
|
|
|
|
.filter(r -> r.getRelClass().equals("TODO")); // TODO mergedIN
|
|
|
|
|
|
|
|
final Column c = null; // TODO
|
|
|
|
|
|
|
|
final Dataset<Row> aa = results.joinWith(rels, results.col("id").equalTo(rels.col("source")), "inner")
|
|
|
|
.groupBy(rels.col("target"))
|
|
|
|
.agg(c)
|
|
|
|
.filter(x -> x.size() > 1)
|
|
|
|
// generateSimpleEvents(...)
|
|
|
|
// flatMap()
|
|
|
|
// toRdd()
|
|
|
|
;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
2020-05-07 12:31:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:17:35 +02:00
|
|
|
private List<Event> generateSimpleEvents(final Result... children) {
|
2020-05-13 12:00:27 +02:00
|
|
|
final List<UpdateInfo<?>> list = new ArrayList<>();
|
2020-05-07 12:31:26 +02:00
|
|
|
|
2020-05-13 12:00:27 +02:00
|
|
|
for (final Result target : children) {
|
|
|
|
list.addAll(enrichMissingAbstract.searchUpdatesForRecord(target, children));
|
|
|
|
list.addAll(enrichMissingAuthorOrcid.searchUpdatesForRecord(target, children));
|
|
|
|
list.addAll(enrichMissingOpenAccess.searchUpdatesForRecord(target, children));
|
|
|
|
list.addAll(enrichMissingPid.searchUpdatesForRecord(target, children));
|
|
|
|
list.addAll(enrichMissingProject.searchUpdatesForRecord(target, children));
|
|
|
|
list.addAll(enrichMissingPublicationDate.searchUpdatesForRecord(target, children));
|
|
|
|
list.addAll(enrichMissingSubject.searchUpdatesForRecord(target, children));
|
|
|
|
list.addAll(enrichMoreOpenAccess.searchUpdatesForRecord(target, children));
|
|
|
|
list.addAll(enrichMorePid.searchUpdatesForRecord(target, children));
|
|
|
|
list.addAll(enrichMoreSubject.searchUpdatesForRecord(target, children));
|
|
|
|
}
|
2020-05-07 12:31:26 +02:00
|
|
|
|
2020-05-13 12:00:27 +02:00
|
|
|
return list.stream().map(EventFactory::newBrokerEvent).collect(Collectors.toList());
|
2020-05-07 12:31:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:17:35 +02:00
|
|
|
public static <R> Dataset<R> readPath(
|
|
|
|
final SparkSession spark,
|
|
|
|
final String inputPath,
|
|
|
|
final Class<R> clazz) {
|
|
|
|
return spark
|
|
|
|
.read()
|
|
|
|
.textFile(inputPath)
|
|
|
|
.map((MapFunction<String, R>) value -> OBJECT_MAPPER.readValue(value, clazz), Encoders.bean(clazz));
|
|
|
|
}
|
2020-05-07 12:31:26 +02:00
|
|
|
}
|