2020-04-27 14:52:31 +02:00
|
|
|
|
2020-04-10 17:53:07 +02:00
|
|
|
package eu.dnetlib.dhp.oa.graph.raw;
|
2020-03-02 16:12:14 +01:00
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
|
|
|
2020-11-13 10:05:12 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Optional;
|
2020-04-18 12:42:58 +02:00
|
|
|
import java.util.stream.Collectors;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2020-03-02 16:12:14 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.hadoop.io.Text;
|
2020-03-18 09:34:57 +01:00
|
|
|
import org.apache.hadoop.io.compress.GzipCodec;
|
2020-04-10 17:53:07 +02:00
|
|
|
import org.apache.spark.SparkConf;
|
2020-03-02 16:12:14 +01:00
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
2021-08-12 11:32:26 +02:00
|
|
|
import org.dom4j.DocumentException;
|
2020-04-10 17:53:07 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.common.HdfsSupport;
|
2021-01-25 15:43:04 +01:00
|
|
|
import eu.dnetlib.dhp.common.vocabulary.VocabularyGroup;
|
2020-04-28 11:23:29 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelSupport;
|
2020-11-13 10:05:02 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
2021-06-23 11:49:42 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.utils.OafMapperUtils;
|
2020-06-09 17:20:40 +02:00
|
|
|
import eu.dnetlib.dhp.utils.ISLookupClientFactory;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
2020-04-10 17:53:07 +02:00
|
|
|
import scala.Tuple2;
|
2020-03-02 16:12:14 +01:00
|
|
|
|
|
|
|
public class GenerateEntitiesApplication {
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(GenerateEntitiesApplication.class);
|
|
|
|
|
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
|
2021-06-23 11:49:42 +02:00
|
|
|
/**
|
|
|
|
* Operation mode
|
|
|
|
*/
|
|
|
|
enum Mode {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Groups all the objects by id to merge them
|
|
|
|
*/
|
|
|
|
claim,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default mode
|
|
|
|
*/
|
|
|
|
graph
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public static void main(final String[] args) throws Exception {
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
2020-05-26 13:11:09 +02:00
|
|
|
.toString(
|
|
|
|
GenerateEntitiesApplication.class
|
|
|
|
.getResourceAsStream("/eu/dnetlib/dhp/oa/graph/generate_entities_parameters.json")));
|
2020-04-27 14:52:31 +02:00
|
|
|
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
2020-05-26 13:06:55 +02:00
|
|
|
final Boolean isSparkSessionManaged = Optional
|
2020-04-27 14:52:31 +02:00
|
|
|
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
final String sourcePaths = parser.get("sourcePaths");
|
2020-05-29 18:17:30 +02:00
|
|
|
log.info("sourcePaths: {}", sourcePaths);
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final String targetPath = parser.get("targetPath");
|
2020-05-29 18:17:30 +02:00
|
|
|
log.info("targetPath: {}", targetPath);
|
2020-04-27 14:52:31 +02:00
|
|
|
|
2020-05-29 18:17:30 +02:00
|
|
|
final String isLookupUrl = parser.get("isLookupUrl");
|
|
|
|
log.info("isLookupUrl: {}", isLookupUrl);
|
2020-04-27 14:52:31 +02:00
|
|
|
|
2020-11-30 12:00:38 +01:00
|
|
|
final boolean shouldHashId = Optional
|
|
|
|
.ofNullable(parser.get("shouldHashId"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(true);
|
|
|
|
log.info("shouldHashId: {}", shouldHashId);
|
|
|
|
|
2021-06-23 11:49:42 +02:00
|
|
|
final Mode mode = Optional
|
|
|
|
.ofNullable(parser.get("mode"))
|
|
|
|
.map(Mode::valueOf)
|
|
|
|
.orElse(Mode.graph);
|
|
|
|
log.info("mode: {}", mode);
|
|
|
|
|
2020-06-09 17:20:40 +02:00
|
|
|
final ISLookUpService isLookupService = ISLookupClientFactory.getLookUpService(isLookupUrl);
|
|
|
|
final VocabularyGroup vocs = VocabularyGroup.loadVocsFromIS(isLookupService);
|
2020-05-26 13:06:55 +02:00
|
|
|
|
|
|
|
final SparkConf conf = new SparkConf();
|
|
|
|
runWithSparkSession(conf, isSparkSessionManaged, spark -> {
|
2020-11-13 10:05:12 +01:00
|
|
|
HdfsSupport.remove(targetPath, spark.sparkContext().hadoopConfiguration());
|
2021-06-23 11:49:42 +02:00
|
|
|
generateEntities(spark, vocs, sourcePaths, targetPath, shouldHashId, mode);
|
2020-05-26 13:06:55 +02:00
|
|
|
});
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void generateEntities(
|
|
|
|
final SparkSession spark,
|
2020-05-27 11:34:13 +02:00
|
|
|
final VocabularyGroup vocs,
|
2020-04-27 14:52:31 +02:00
|
|
|
final String sourcePaths,
|
2020-11-30 12:00:38 +01:00
|
|
|
final String targetPath,
|
2021-06-23 11:49:42 +02:00
|
|
|
final boolean shouldHashId,
|
|
|
|
final Mode mode) {
|
2020-04-27 14:52:31 +02:00
|
|
|
|
2020-05-26 13:06:55 +02:00
|
|
|
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
2020-04-27 14:52:31 +02:00
|
|
|
final List<String> existingSourcePaths = Arrays
|
|
|
|
.stream(sourcePaths.split(","))
|
2020-11-13 10:05:12 +01:00
|
|
|
.filter(p -> HdfsSupport.exists(p, sc.hadoopConfiguration()))
|
2020-04-27 14:52:31 +02:00
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
log.info("Generate entities from files:");
|
|
|
|
existingSourcePaths.forEach(log::info);
|
|
|
|
|
|
|
|
JavaRDD<Oaf> inputRdd = sc.emptyRDD();
|
|
|
|
|
|
|
|
for (final String sp : existingSourcePaths) {
|
|
|
|
inputRdd = inputRdd
|
2020-05-26 13:11:09 +02:00
|
|
|
.union(
|
|
|
|
sc
|
|
|
|
.sequenceFile(sp, Text.class, Text.class)
|
|
|
|
.map(k -> new Tuple2<>(k._1().toString(), k._2().toString()))
|
2020-11-30 12:00:38 +01:00
|
|
|
.map(k -> convertToListOaf(k._1(), k._2(), shouldHashId, vocs))
|
2020-05-26 13:11:09 +02:00
|
|
|
.filter(Objects::nonNull)
|
2021-08-11 12:13:22 +02:00
|
|
|
.flatMap(List::iterator));
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 11:49:42 +02:00
|
|
|
switch (mode) {
|
|
|
|
case claim:
|
|
|
|
save(
|
|
|
|
inputRdd
|
|
|
|
.mapToPair(oaf -> new Tuple2<>(ModelSupport.idFn().apply(oaf), oaf))
|
2021-08-11 12:13:22 +02:00
|
|
|
.reduceByKey(OafMapperUtils::merge)
|
2021-06-23 11:49:42 +02:00
|
|
|
.map(Tuple2::_2),
|
|
|
|
targetPath);
|
|
|
|
break;
|
|
|
|
case graph:
|
|
|
|
save(inputRdd, targetPath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void save(final JavaRDD<Oaf> rdd, final String targetPath) {
|
|
|
|
rdd
|
2020-05-26 13:11:09 +02:00
|
|
|
.map(
|
|
|
|
oaf -> oaf.getClass().getSimpleName().toLowerCase()
|
|
|
|
+ "|"
|
|
|
|
+ OBJECT_MAPPER.writeValueAsString(oaf))
|
2020-04-27 14:52:31 +02:00
|
|
|
.saveAsTextFile(targetPath, GzipCodec.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<Oaf> convertToListOaf(
|
2020-05-26 13:06:55 +02:00
|
|
|
final String id,
|
|
|
|
final String s,
|
2020-11-30 12:00:38 +01:00
|
|
|
final boolean shouldHashId,
|
2021-08-12 11:32:26 +02:00
|
|
|
final VocabularyGroup vocs) throws DocumentException {
|
2020-04-27 14:52:31 +02:00
|
|
|
final String type = StringUtils.substringAfter(id, ":");
|
|
|
|
|
|
|
|
switch (type.toLowerCase()) {
|
2020-06-10 10:04:00 +02:00
|
|
|
case "oaf-store-cleaned":
|
2020-11-30 12:00:38 +01:00
|
|
|
return new OafToOafMapper(vocs, false, shouldHashId).processMdRecord(s);
|
2021-07-19 17:43:52 +02:00
|
|
|
case "oaf-store-claim":
|
|
|
|
return new OafToOafMapper(vocs, false, shouldHashId, true).processMdRecord(s);
|
2020-06-10 10:04:00 +02:00
|
|
|
case "odf-store-cleaned":
|
2020-11-30 12:00:38 +01:00
|
|
|
return new OdfToOafMapper(vocs, false, shouldHashId).processMdRecord(s);
|
2021-07-19 17:43:52 +02:00
|
|
|
case "odf-store-claim":
|
|
|
|
return new OdfToOafMapper(vocs, false, shouldHashId, true).processMdRecord(s);
|
2020-06-10 10:04:00 +02:00
|
|
|
case "oaf-store-intersection":
|
2020-11-30 12:00:38 +01:00
|
|
|
return new OafToOafMapper(vocs, true, shouldHashId).processMdRecord(s);
|
2020-06-10 10:04:00 +02:00
|
|
|
case "odf-store-intersection":
|
2020-11-30 12:00:38 +01:00
|
|
|
return new OdfToOafMapper(vocs, true, shouldHashId).processMdRecord(s);
|
2020-05-26 13:11:09 +02:00
|
|
|
case "datasource":
|
|
|
|
return Arrays.asList(convertFromJson(s, Datasource.class));
|
|
|
|
case "organization":
|
|
|
|
return Arrays.asList(convertFromJson(s, Organization.class));
|
|
|
|
case "project":
|
|
|
|
return Arrays.asList(convertFromJson(s, Project.class));
|
|
|
|
case "relation":
|
|
|
|
return Arrays.asList(convertFromJson(s, Relation.class));
|
|
|
|
case "publication":
|
|
|
|
return Arrays.asList(convertFromJson(s, Publication.class));
|
|
|
|
case "dataset":
|
|
|
|
return Arrays.asList(convertFromJson(s, Dataset.class));
|
|
|
|
case "software":
|
|
|
|
return Arrays.asList(convertFromJson(s, Software.class));
|
|
|
|
case "otherresearchproduct":
|
|
|
|
return Arrays.asList(convertFromJson(s, OtherResearchProduct.class));
|
|
|
|
default:
|
2021-08-11 12:13:22 +02:00
|
|
|
throw new IllegalArgumentException("type not managed: " + type.toLowerCase());
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Oaf convertFromJson(final String s, final Class<? extends Oaf> clazz) {
|
|
|
|
try {
|
|
|
|
return OBJECT_MAPPER.readValue(s, clazz);
|
|
|
|
} catch (final Exception e) {
|
2021-08-11 12:13:22 +02:00
|
|
|
log.error("Error parsing object of class: {}", clazz);
|
2020-04-27 14:52:31 +02:00
|
|
|
log.error(s);
|
2021-08-11 12:13:22 +02:00
|
|
|
throw new IllegalArgumentException(e);
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 16:12:14 +01:00
|
|
|
}
|