2021-02-10 11:51:50 +01:00
|
|
|
|
2020-12-21 11:58:21 +01:00
|
|
|
package eu.dnetlib.dhp.oa.dedup;
|
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.KeyValue;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Qualifier;
|
|
|
|
import eu.dnetlib.pace.config.DedupConfig;
|
2020-12-21 11:58:21 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
2021-02-10 11:51:50 +01:00
|
|
|
import org.apache.spark.sql.SaveMode;
|
2020-12-21 11:58:21 +01:00
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.DataInfo;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
|
|
|
import eu.dnetlib.dhp.utils.ISLookupClientFactory;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
2020-12-21 11:58:21 +01:00
|
|
|
|
|
|
|
//copy simrels (verified) from relation to the workdir in order to make them available for the deduplication
|
2021-02-10 11:51:50 +01:00
|
|
|
public class SparkCopyOpenorgsSimRels extends AbstractSparkAction {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(SparkCopyOpenorgsMergeRels.class);
|
2020-12-21 11:58:21 +01:00
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
public SparkCopyOpenorgsSimRels(ArgumentApplicationParser parser, SparkSession spark) {
|
2020-12-21 11:58:21 +01:00
|
|
|
super(parser, spark);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
2021-02-10 11:51:50 +01:00
|
|
|
SparkCopyOpenorgsSimRels.class
|
2020-12-21 11:58:21 +01:00
|
|
|
.getResourceAsStream(
|
2021-02-10 11:51:50 +01:00
|
|
|
"/eu/dnetlib/dhp/oa/dedup/copyOpenorgsMergeRels_parameters.json")));
|
2020-12-21 11:58:21 +01:00
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
2021-02-10 11:51:50 +01:00
|
|
|
new SparkCopyOpenorgsSimRels(parser, getSparkSession(conf))
|
2020-12-21 11:58:21 +01:00
|
|
|
.run(ISLookupClientFactory.getLookUpService(parser.get("isLookUpUrl")));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run(ISLookUpService isLookUpService)
|
|
|
|
throws DocumentException, IOException, ISLookUpException {
|
|
|
|
|
|
|
|
// read oozie parameters
|
|
|
|
final String graphBasePath = parser.get("graphBasePath");
|
|
|
|
final String actionSetId = parser.get("actionSetId");
|
|
|
|
final String workingPath = parser.get("workingPath");
|
|
|
|
final int numPartitions = Optional
|
|
|
|
.ofNullable(parser.get("numPartitions"))
|
|
|
|
.map(Integer::valueOf)
|
|
|
|
.orElse(NUM_PARTITIONS);
|
|
|
|
|
|
|
|
log.info("numPartitions: '{}'", numPartitions);
|
|
|
|
log.info("graphBasePath: '{}'", graphBasePath);
|
|
|
|
log.info("actionSetId: '{}'", actionSetId);
|
|
|
|
log.info("workingPath: '{}'", workingPath);
|
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
log.info("Copying OpenOrgs SimRels");
|
2020-12-21 11:58:21 +01:00
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
final String outputPath = DedupUtility.createSimRelPath(workingPath, actionSetId, "organization");
|
2020-12-21 11:58:21 +01:00
|
|
|
|
2021-01-18 16:48:08 +01:00
|
|
|
removeOutputDir(spark, outputPath);
|
2020-12-21 11:58:21 +01:00
|
|
|
|
2021-01-18 16:48:08 +01:00
|
|
|
final String relationPath = DedupUtility.createEntityPath(graphBasePath, "relation");
|
2020-12-21 11:58:21 +01:00
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
JavaRDD<Relation> rawRels = spark
|
|
|
|
.read()
|
|
|
|
.textFile(relationPath)
|
|
|
|
.map(patchRelFn(), Encoders.bean(Relation.class))
|
|
|
|
.toJavaRDD()
|
|
|
|
.filter(this::isOpenorgs)
|
|
|
|
.filter(this::filterOpenorgsRels);
|
2020-12-21 11:58:21 +01:00
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
save(spark.createDataset(rawRels.rdd(),Encoders.bean(Relation.class)), outputPath, SaveMode.Append);
|
2020-12-21 11:58:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private 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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
private boolean filterOpenorgsRels(Relation rel) {
|
2020-12-21 11:58:21 +01:00
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
if (rel.getRelClass().equals("isSimilarTo") && rel.getRelType().equals("organizationOrganization") && rel.getSubRelType().equals("dedup"))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isOpenorgs(Relation rel) {
|
|
|
|
|
|
|
|
if (rel.getCollectedfrom() != null) {
|
|
|
|
for (KeyValue k: rel.getCollectedfrom()) {
|
|
|
|
if (k.getValue().equals("OpenOrgs Database")) {
|
2020-12-21 11:58:21 +01:00
|
|
|
return true;
|
2021-02-10 11:51:50 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-21 11:58:21 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 11:51:50 +01:00
|
|
|
|