2021-02-10 11:51:50 +01:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.oa.dedup;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
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;
|
2021-02-26 10:19:28 +01:00
|
|
|
import org.apache.spark.sql.Dataset;
|
2021-02-10 11:51:50 +01:00
|
|
|
import org.apache.spark.sql.Encoders;
|
2021-02-26 10:19:28 +01:00
|
|
|
import org.apache.spark.sql.SaveMode;
|
2021-02-10 11:51:50 +01:00
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-03-31 17:07:13 +02:00
|
|
|
import com.sun.media.sound.ModelChannelMixer;
|
|
|
|
|
2021-02-10 11:51:50 +01:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2021-03-31 17:07:13 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
2021-02-10 11:51:50 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.DataInfo;
|
2021-02-26 10:19:28 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.KeyValue;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Qualifier;
|
2021-02-10 11:51:50 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
2021-03-29 10:07:12 +02:00
|
|
|
import eu.dnetlib.dhp.utils.DHPUtils;
|
2021-02-10 11:51:50 +01:00
|
|
|
import eu.dnetlib.dhp.utils.ISLookupClientFactory;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
2021-02-26 10:19:28 +01:00
|
|
|
import eu.dnetlib.pace.config.DedupConfig;
|
2021-03-29 10:07:12 +02:00
|
|
|
import net.sf.saxon.ma.trie.Tuple2;
|
2021-02-10 11:51:50 +01:00
|
|
|
|
|
|
|
public class SparkCopyOpenorgsMergeRels extends AbstractSparkAction {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(SparkCopyOpenorgsMergeRels.class);
|
|
|
|
|
|
|
|
public SparkCopyOpenorgsMergeRels(ArgumentApplicationParser parser, SparkSession spark) {
|
|
|
|
super(parser, spark);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
SparkCopyOpenorgsMergeRels.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/oa/dedup/copyOpenorgsMergeRels_parameters.json")));
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
new SparkCopyOpenorgsMergeRels(parser, getSparkSession(conf))
|
|
|
|
.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);
|
|
|
|
|
|
|
|
log.info("Copying OpenOrgs Merge Rels");
|
|
|
|
|
|
|
|
final String outputPath = DedupUtility.createMergeRelPath(workingPath, actionSetId, "organization");
|
|
|
|
|
|
|
|
final String relationPath = DedupUtility.createEntityPath(graphBasePath, "relation");
|
|
|
|
|
|
|
|
DedupConfig dedupConf = getConfigurations(isLookUpService, actionSetId).get(0);
|
|
|
|
|
|
|
|
JavaRDD<Relation> rawRels = spark
|
|
|
|
.read()
|
|
|
|
.textFile(relationPath)
|
|
|
|
.map(patchRelFn(), Encoders.bean(Relation.class))
|
|
|
|
.toJavaRDD()
|
2021-03-19 16:57:40 +01:00
|
|
|
.filter(this::isOpenorgs)
|
2021-03-29 10:07:12 +02:00
|
|
|
.filter(this::filterOpenorgsRels);
|
|
|
|
|
|
|
|
JavaRDD<Relation> selfRawRels = rawRels
|
|
|
|
.map(r -> r.getSource())
|
|
|
|
.distinct()
|
2021-03-31 18:33:57 +02:00
|
|
|
.map(s -> rel(s, s, ModelConstants.IS_SIMILAR_TO, dedupConf));
|
2021-02-10 11:51:50 +01:00
|
|
|
|
2021-03-19 16:57:40 +01:00
|
|
|
log.info("Number of raw Openorgs Relations collected: {}", rawRels.count());
|
|
|
|
|
2021-02-26 10:19:28 +01:00
|
|
|
// turn openorgs isSimilarTo relations into mergerels
|
2021-03-29 10:07:12 +02:00
|
|
|
JavaRDD<Relation> mergeRelsRDD = rawRels
|
|
|
|
.union(selfRawRels)
|
|
|
|
.map(r -> {
|
|
|
|
r.setSource(createDedupID(r.getSource())); // create the dedup_id to align it to the openaire dedup
|
|
|
|
// format
|
|
|
|
return r;
|
|
|
|
})
|
|
|
|
.flatMap(rel -> {
|
2021-02-10 11:51:50 +01:00
|
|
|
|
2021-03-29 10:07:12 +02:00
|
|
|
List<Relation> mergerels = new ArrayList<>();
|
2021-02-10 11:51:50 +01:00
|
|
|
|
2021-03-31 18:33:57 +02:00
|
|
|
mergerels.add(rel(rel.getSource(), rel.getTarget(), ModelConstants.MERGES, dedupConf));
|
|
|
|
mergerels.add(rel(rel.getTarget(), rel.getSource(), ModelConstants.IS_MERGED_IN, dedupConf));
|
2021-02-10 11:51:50 +01:00
|
|
|
|
2021-03-29 10:07:12 +02:00
|
|
|
return mergerels.iterator();
|
|
|
|
});
|
2021-02-10 11:51:50 +01:00
|
|
|
|
2021-03-19 16:57:40 +01:00
|
|
|
log.info("Number of Openorgs Merge Relations created: {}", mergeRelsRDD.count());
|
|
|
|
|
2021-02-26 10:19:28 +01:00
|
|
|
spark
|
|
|
|
.createDataset(
|
|
|
|
mergeRelsRDD.rdd(),
|
|
|
|
Encoders.bean(Relation.class))
|
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Append)
|
|
|
|
.parquet(outputPath);
|
2021-02-10 11:51:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean filterOpenorgsRels(Relation rel) {
|
2021-04-01 10:30:51 +02:00
|
|
|
return rel.getRelClass().equals(ModelConstants.IS_SIMILAR_TO)
|
2021-03-31 17:07:13 +02:00
|
|
|
&& rel.getRelType().equals(ModelConstants.ORG_ORG_RELTYPE)
|
2021-04-01 10:30:51 +02:00
|
|
|
&& rel.getSubRelType().equals(ModelConstants.DEDUP);
|
2021-02-10 11:51:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private Relation rel(String source, String target, String relClass, DedupConfig dedupConf) {
|
|
|
|
|
|
|
|
String entityType = dedupConf.getWf().getEntityType();
|
|
|
|
|
|
|
|
Relation r = new Relation();
|
|
|
|
r.setSource(source);
|
|
|
|
r.setTarget(target);
|
|
|
|
r.setRelClass(relClass);
|
|
|
|
r.setRelType(entityType + entityType.substring(0, 1).toUpperCase() + entityType.substring(1));
|
2021-03-31 17:07:13 +02:00
|
|
|
r.setSubRelType(ModelConstants.DEDUP);
|
2021-02-10 11:51:50 +01:00
|
|
|
|
|
|
|
DataInfo info = new DataInfo();
|
|
|
|
info.setDeletedbyinference(false);
|
|
|
|
info.setInferred(true);
|
|
|
|
info.setInvisible(false);
|
|
|
|
info.setInferenceprovenance(dedupConf.getWf().getConfigurationId());
|
|
|
|
Qualifier provenanceAction = new Qualifier();
|
2021-03-31 18:33:57 +02:00
|
|
|
provenanceAction.setClassid(ModelConstants.PROVENANCE_DEDUP);
|
|
|
|
provenanceAction.setClassname(ModelConstants.PROVENANCE_DEDUP);
|
|
|
|
provenanceAction.setSchemeid(ModelConstants.DNET_PROVENANCE_ACTIONS);
|
|
|
|
provenanceAction.setSchemename(ModelConstants.DNET_PROVENANCE_ACTIONS);
|
2021-02-10 11:51:50 +01:00
|
|
|
info.setProvenanceaction(provenanceAction);
|
|
|
|
|
|
|
|
// TODO calculate the trust value based on the similarity score of the elements in the CC
|
|
|
|
// info.setTrust();
|
|
|
|
|
|
|
|
r.setDataInfo(info);
|
|
|
|
return r;
|
|
|
|
}
|
2021-03-29 10:07:12 +02:00
|
|
|
|
|
|
|
public String createDedupID(String id) {
|
|
|
|
|
|
|
|
String prefix = id.split("\\|")[0];
|
|
|
|
return prefix + "|dedup_wf_001::" + DHPUtils.md5(id);
|
|
|
|
}
|
2021-02-10 11:51:50 +01:00
|
|
|
}
|