2020-04-27 14:52:31 +02:00
|
|
|
|
2020-04-02 18:37:53 +02:00
|
|
|
package eu.dnetlib.dhp.oa.dedup;
|
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2021-04-12 17:45:11 +02:00
|
|
|
import java.util.Objects;
|
2021-04-01 10:30:51 +02:00
|
|
|
import java.util.Optional;
|
2021-03-24 15:51:27 +01:00
|
|
|
import java.util.stream.Collectors;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2021-03-24 15:51:27 +01:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2020-04-02 18:37:53 +02:00
|
|
|
import org.apache.spark.SparkConf;
|
2021-04-01 10:30:51 +02:00
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
2020-04-18 12:06:23 +02:00
|
|
|
import org.apache.spark.sql.Dataset;
|
|
|
|
import org.apache.spark.sql.SaveMode;
|
2020-04-02 18:37:53 +02:00
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.dom4j.Document;
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
import org.dom4j.Element;
|
|
|
|
import org.dom4j.io.SAXReader;
|
2021-08-11 12:13:22 +02:00
|
|
|
import org.xml.sax.SAXException;
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-28 11:23:29 +02:00
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.common.HdfsSupport;
|
2021-04-01 10:30:51 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
2020-04-28 11:23:29 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
|
|
|
import eu.dnetlib.pace.config.DedupConfig;
|
|
|
|
|
2020-04-02 18:37:53 +02:00
|
|
|
abstract class AbstractSparkAction implements Serializable {
|
|
|
|
|
2020-07-13 20:09:06 +02:00
|
|
|
protected static final int NUM_PARTITIONS = 1000;
|
2021-02-26 10:19:28 +01:00
|
|
|
protected static final int NUM_CONNECTIONS = 20;
|
2020-07-13 20:09:06 +02:00
|
|
|
|
2021-03-24 15:51:27 +01:00
|
|
|
protected static final String TYPE_VALUE_SEPARATOR = "###";
|
|
|
|
protected static final String SP_SEPARATOR = "@@@";
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
|
|
|
|
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
2020-04-17 13:12:44 +02:00
|
|
|
|
2021-08-11 12:13:22 +02:00
|
|
|
public final ArgumentApplicationParser parser; // parameters for the spark action
|
|
|
|
public final SparkSession spark; // the spark session
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2021-08-11 12:13:22 +02:00
|
|
|
protected AbstractSparkAction(ArgumentApplicationParser parser, SparkSession spark) {
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
this.parser = parser;
|
|
|
|
this.spark = spark;
|
|
|
|
}
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public List<DedupConfig> getConfigurations(ISLookUpService isLookUpService, String orchestrator)
|
2021-08-11 12:13:22 +02:00
|
|
|
throws ISLookUpException, DocumentException, IOException, SAXException {
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final String xquery = String.format("/RESOURCE_PROFILE[.//DEDUPLICATION/ACTION_SET/@id = '%s']", orchestrator);
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
String orchestratorProfile = isLookUpService.getResourceProfileByQuery(xquery);
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2021-08-11 12:13:22 +02:00
|
|
|
final SAXReader reader = new SAXReader();
|
|
|
|
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
|
|
|
|
|
|
final Document doc = reader.read(new StringReader(orchestratorProfile));
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final String actionSetId = doc.valueOf("//DEDUPLICATION/ACTION_SET/@id");
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final List<DedupConfig> configurations = new ArrayList<>();
|
2020-04-02 18:37:53 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
for (final Object o : doc.selectNodes("//SCAN_SEQUENCE/SCAN")) {
|
|
|
|
configurations.add(loadConfig(isLookUpService, actionSetId, o));
|
|
|
|
}
|
2020-04-17 13:12:44 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
return configurations;
|
|
|
|
}
|
2020-04-27 14:45:40 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
private DedupConfig loadConfig(
|
|
|
|
final ISLookUpService isLookUpService, final String actionSetId, final Object o)
|
|
|
|
throws ISLookUpException, IOException {
|
|
|
|
final Element s = (Element) o;
|
|
|
|
final String configProfileId = s.attributeValue("id");
|
|
|
|
final String conf = isLookUpService
|
|
|
|
.getResourceProfileByQuery(
|
|
|
|
String
|
|
|
|
.format(
|
|
|
|
"for $x in /RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '%s'] return $x//DEDUPLICATION/text()",
|
|
|
|
configProfileId));
|
2020-04-27 14:45:40 +02:00
|
|
|
|
2023-07-18 11:38:56 +02:00
|
|
|
DedupConfig dedupConfig = DedupConfig.load(conf);
|
2020-04-27 14:52:31 +02:00
|
|
|
dedupConfig.getWf().setConfigurationId(actionSetId);
|
2020-04-27 14:45:40 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
return dedupConfig;
|
|
|
|
}
|
2020-04-27 14:45:40 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
abstract void run(ISLookUpService isLookUpService)
|
2021-08-11 12:13:22 +02:00
|
|
|
throws DocumentException, IOException, ISLookUpException, SAXException;
|
2020-04-27 14:45:40 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
protected static SparkSession getSparkSession(SparkConf conf) {
|
|
|
|
return SparkSession.builder().config(conf).getOrCreate();
|
|
|
|
}
|
2020-04-27 14:45:40 +02:00
|
|
|
|
2023-10-02 09:25:12 +02:00
|
|
|
protected static SparkSession getSparkWithHiveSession(SparkConf conf) {
|
|
|
|
return SparkSession.builder().enableHiveSupport().config(conf).getOrCreate();
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
protected static <T> void save(Dataset<T> dataset, String outPath, SaveMode mode) {
|
|
|
|
dataset.write().option("compression", "gzip").mode(mode).json(outPath);
|
|
|
|
}
|
2020-04-27 14:45:40 +02:00
|
|
|
|
2021-03-29 10:07:12 +02:00
|
|
|
protected static <T> void saveParquet(Dataset<T> dataset, String outPath, SaveMode mode) {
|
|
|
|
dataset.write().option("compression", "gzip").mode(mode).parquet(outPath);
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
protected static void removeOutputDir(SparkSession spark, String path) {
|
|
|
|
HdfsSupport.remove(path, spark.sparkContext().hadoopConfiguration());
|
|
|
|
}
|
2021-03-24 15:51:27 +01:00
|
|
|
|
|
|
|
protected static String structuredPropertyListToString(List<StructuredProperty> list) {
|
|
|
|
|
|
|
|
return list
|
|
|
|
.stream()
|
|
|
|
.filter(p -> p.getQualifier() != null)
|
|
|
|
.filter(p -> StringUtils.isNotBlank(p.getQualifier().getClassid()))
|
|
|
|
.filter(p -> StringUtils.isNotBlank(p.getValue()))
|
|
|
|
.map(p -> p.getValue() + TYPE_VALUE_SEPARATOR + p.getQualifier().getClassid())
|
|
|
|
.collect(Collectors.joining(SP_SEPARATOR));
|
|
|
|
}
|
2021-04-01 10:30:51 +02:00
|
|
|
|
|
|
|
protected static MapFunction<String, Relation> patchRelFn() {
|
|
|
|
return value -> {
|
|
|
|
final Relation rel = OBJECT_MAPPER.readValue(value, Relation.class);
|
|
|
|
if (rel.getDataInfo() == null) {
|
|
|
|
rel.setDataInfo(new DataInfo());
|
|
|
|
}
|
|
|
|
return rel;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean isOpenorgs(Relation rel) {
|
|
|
|
return Optional
|
|
|
|
.ofNullable(rel.getCollectedfrom())
|
2021-11-19 11:30:29 +01:00
|
|
|
.map(c -> isCollectedFromOpenOrgs(c))
|
2021-04-01 10:30:51 +02:00
|
|
|
.orElse(false);
|
|
|
|
}
|
2021-04-07 14:27:43 +02:00
|
|
|
|
2021-11-19 11:30:29 +01:00
|
|
|
protected boolean isOpenorgsDedupRel(Relation rel) {
|
|
|
|
return isOpenorgs(rel) && isOpenOrgsDedupMergeRelation(rel);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isCollectedFromOpenOrgs(List<KeyValue> c) {
|
|
|
|
return c
|
|
|
|
.stream()
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
.anyMatch(kv -> ModelConstants.OPENORGS_NAME.equals(kv.getValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isOpenOrgsDedupMergeRelation(Relation rel) {
|
|
|
|
return ModelConstants.ORG_ORG_RELTYPE.equals(rel.getRelType()) &&
|
|
|
|
ModelConstants.DEDUP.equals(rel.getSubRelType())
|
|
|
|
&& (ModelConstants.IS_MERGED_IN.equals(rel.getRelClass()) ||
|
|
|
|
ModelConstants.MERGES.equals(rel.getRelClass()));
|
|
|
|
}
|
|
|
|
|
2021-04-07 14:27:43 +02:00
|
|
|
protected static Boolean parseECField(Field<String> field) {
|
|
|
|
if (field == null)
|
|
|
|
return null;
|
|
|
|
if (StringUtils.isBlank(field.getValue()) || field.getValue().equalsIgnoreCase("null"))
|
|
|
|
return null;
|
|
|
|
return field.getValue().equalsIgnoreCase("true");
|
|
|
|
}
|
2020-04-02 18:37:53 +02:00
|
|
|
}
|