|
|
|
@ -1,18 +1,20 @@
|
|
|
|
|
|
|
|
|
|
package eu.dnetlib.dhp.oa.dedup;
|
|
|
|
|
|
|
|
|
|
import static eu.dnetlib.dhp.schema.common.ModelConstants.DNET_PROVENANCE_ACTIONS;
|
|
|
|
|
import static eu.dnetlib.dhp.schema.common.ModelConstants.PROVENANCE_DEDUP;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
|
import org.apache.spark.api.java.JavaPairRDD;
|
|
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
|
import org.apache.spark.api.java.function.FlatMapFunction;
|
|
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
|
|
|
|
import org.apache.spark.api.java.function.PairFunction;
|
|
|
|
|
import org.apache.spark.api.java.function.MapGroupsFunction;
|
|
|
|
|
import org.apache.spark.graphx.Edge;
|
|
|
|
|
import org.apache.spark.rdd.RDD;
|
|
|
|
|
import org.apache.spark.sql.Dataset;
|
|
|
|
@ -23,14 +25,18 @@ import org.dom4j.DocumentException;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import com.google.common.hash.Hashing;
|
|
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
|
import eu.dnetlib.dhp.oa.dedup.graph.ConnectedComponent;
|
|
|
|
|
import eu.dnetlib.dhp.oa.dedup.graph.GraphProcessor;
|
|
|
|
|
import eu.dnetlib.dhp.oa.dedup.model.Identifier;
|
|
|
|
|
import eu.dnetlib.dhp.schema.common.EntityType;
|
|
|
|
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
|
|
|
|
import eu.dnetlib.dhp.schema.common.ModelSupport;
|
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.DataInfo;
|
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.OafEntity;
|
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Qualifier;
|
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
|
|
|
|
import eu.dnetlib.dhp.utils.ISLookupClientFactory;
|
|
|
|
@ -42,9 +48,7 @@ import scala.Tuple2;
|
|
|
|
|
|
|
|
|
|
public class SparkCreateMergeRels extends AbstractSparkAction {
|
|
|
|
|
|
|
|
|
|
public static final String PROVENANCE_ACTION_CLASS = "sysimport:dedup";
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(SparkCreateMergeRels.class);
|
|
|
|
|
public static final String DNET_PROVENANCE_ACTIONS = "dnet:provenanceActions";
|
|
|
|
|
|
|
|
|
|
public SparkCreateMergeRels(ArgumentApplicationParser parser, SparkSession spark) {
|
|
|
|
|
super(parser, spark);
|
|
|
|
@ -92,6 +96,7 @@ public class SparkCreateMergeRels extends AbstractSparkAction {
|
|
|
|
|
|
|
|
|
|
for (DedupConfig dedupConf : getConfigurations(isLookUpService, actionSetId)) {
|
|
|
|
|
final String subEntity = dedupConf.getWf().getSubEntityValue();
|
|
|
|
|
final Class<OafEntity> clazz = ModelSupport.entityTypes.get(EntityType.valueOf(subEntity));
|
|
|
|
|
|
|
|
|
|
log.info("Creating mergerels for: '{}'", subEntity);
|
|
|
|
|
|
|
|
|
@ -100,10 +105,8 @@ public class SparkCreateMergeRels extends AbstractSparkAction {
|
|
|
|
|
|
|
|
|
|
final String mergeRelPath = DedupUtility.createMergeRelPath(workingPath, actionSetId, subEntity);
|
|
|
|
|
|
|
|
|
|
final JavaPairRDD<Object, String> vertexes = sc
|
|
|
|
|
.textFile(graphBasePath + "/" + subEntity)
|
|
|
|
|
.map(s -> MapDocumentUtil.getJPathString(dedupConf.getWf().getIdPath(), s))
|
|
|
|
|
.mapToPair((PairFunction<String, Object, String>) s -> new Tuple2<>(hash(s), s));
|
|
|
|
|
// <hash(id), id>
|
|
|
|
|
JavaPairRDD<Object, String> vertexes = createVertexes(sc, graphBasePath, subEntity, dedupConf);
|
|
|
|
|
|
|
|
|
|
final RDD<Edge<String>> edgeRdd = spark
|
|
|
|
|
.read()
|
|
|
|
@ -113,14 +116,42 @@ public class SparkCreateMergeRels extends AbstractSparkAction {
|
|
|
|
|
.map(it -> new Edge<>(hash(it.getSource()), hash(it.getTarget()), it.getRelClass()))
|
|
|
|
|
.rdd();
|
|
|
|
|
|
|
|
|
|
final Dataset<Relation> mergeRels = spark
|
|
|
|
|
Dataset<Tuple2<String, String>> rawMergeRels = spark
|
|
|
|
|
.createDataset(
|
|
|
|
|
GraphProcessor
|
|
|
|
|
.findCCs(vertexes.rdd(), edgeRdd, maxIterations, cut)
|
|
|
|
|
.toJavaRDD()
|
|
|
|
|
.filter(k -> k.getDocIds().size() > 1)
|
|
|
|
|
.flatMap(cc -> ccToMergeRel(cc, dedupConf))
|
|
|
|
|
.filter(k -> k.getIds().size() > 1)
|
|
|
|
|
.flatMap(this::ccToRels)
|
|
|
|
|
.rdd(),
|
|
|
|
|
Encoders.tuple(Encoders.STRING(), Encoders.STRING()));
|
|
|
|
|
|
|
|
|
|
Dataset<Tuple2<String, OafEntity>> entities = spark
|
|
|
|
|
.read()
|
|
|
|
|
.textFile(DedupUtility.createEntityPath(graphBasePath, subEntity))
|
|
|
|
|
.map(
|
|
|
|
|
(MapFunction<String, Tuple2<String, OafEntity>>) it -> {
|
|
|
|
|
OafEntity entity = OBJECT_MAPPER.readValue(it, clazz);
|
|
|
|
|
return new Tuple2<>(entity.getId(), entity);
|
|
|
|
|
},
|
|
|
|
|
Encoders.tuple(Encoders.STRING(), Encoders.kryo(clazz)));
|
|
|
|
|
|
|
|
|
|
Dataset<Relation> mergeRels = rawMergeRels
|
|
|
|
|
.joinWith(entities, rawMergeRels.col("_2").equalTo(entities.col("_1")), "inner")
|
|
|
|
|
// <tmp_source,target>,<target,entity>
|
|
|
|
|
.map(
|
|
|
|
|
(MapFunction<Tuple2<Tuple2<String, String>, Tuple2<String, OafEntity>>, Tuple2<String, OafEntity>>) value -> new Tuple2<>(
|
|
|
|
|
value._1()._1(), value._2()._2()),
|
|
|
|
|
Encoders.tuple(Encoders.STRING(), Encoders.kryo(clazz)))
|
|
|
|
|
// <tmp_source,entity>
|
|
|
|
|
.groupByKey(
|
|
|
|
|
(MapFunction<Tuple2<String, OafEntity>, String>) Tuple2::_1, Encoders.STRING())
|
|
|
|
|
.mapGroups(
|
|
|
|
|
(MapGroupsFunction<String, Tuple2<String, OafEntity>, ConnectedComponent>) this::generateID,
|
|
|
|
|
Encoders.bean(ConnectedComponent.class))
|
|
|
|
|
// <root_id, list(target)>
|
|
|
|
|
.flatMap(
|
|
|
|
|
(FlatMapFunction<ConnectedComponent, Relation>) cc -> ccToMergeRel(cc, dedupConf),
|
|
|
|
|
Encoders.bean(Relation.class));
|
|
|
|
|
|
|
|
|
|
mergeRels.write().mode(SaveMode.Append).parquet(mergeRelPath);
|
|
|
|
@ -128,9 +159,45 @@ public class SparkCreateMergeRels extends AbstractSparkAction {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Iterator<Relation> ccToMergeRel(ConnectedComponent cc, DedupConfig dedupConf) {
|
|
|
|
|
private <T extends OafEntity> ConnectedComponent generateID(String key, Iterator<Tuple2<String, T>> values) {
|
|
|
|
|
|
|
|
|
|
List<Identifier<T>> identifiers = Lists.newArrayList(values).stream().map(v -> {
|
|
|
|
|
T entity = v._2();
|
|
|
|
|
Identifier<T> identifier = Identifier.newInstance(entity);
|
|
|
|
|
return identifier;
|
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
String rootID = IdGenerator.generate(identifiers, key);
|
|
|
|
|
|
|
|
|
|
if (Objects.equals(rootID, key))
|
|
|
|
|
throw new IllegalStateException("generated default ID: " + rootID);
|
|
|
|
|
|
|
|
|
|
return new ConnectedComponent(rootID,
|
|
|
|
|
identifiers.stream().map(i -> i.getEntity().getId()).collect(Collectors.toSet()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JavaPairRDD<Object, String> createVertexes(JavaSparkContext sc, String graphBasePath, String subEntity,
|
|
|
|
|
DedupConfig dedupConf) {
|
|
|
|
|
|
|
|
|
|
return sc
|
|
|
|
|
.textFile(DedupUtility.createEntityPath(graphBasePath, subEntity))
|
|
|
|
|
.mapToPair(json -> {
|
|
|
|
|
String id = MapDocumentUtil.getJPathString(dedupConf.getWf().getIdPath(), json);
|
|
|
|
|
return new Tuple2<>(hash(id), id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Iterator<Tuple2<String, String>> ccToRels(ConnectedComponent cc) {
|
|
|
|
|
return cc
|
|
|
|
|
.getIds()
|
|
|
|
|
.stream()
|
|
|
|
|
.map(id -> new Tuple2<>(cc.getCcId(), id))
|
|
|
|
|
.iterator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Iterator<Relation> ccToMergeRel(ConnectedComponent cc, DedupConfig dedupConf) {
|
|
|
|
|
return cc
|
|
|
|
|
.getDocIds()
|
|
|
|
|
.getIds()
|
|
|
|
|
.stream()
|
|
|
|
|
.flatMap(
|
|
|
|
|
id -> {
|
|
|
|
@ -161,8 +228,8 @@ public class SparkCreateMergeRels extends AbstractSparkAction {
|
|
|
|
|
info.setInvisible(false);
|
|
|
|
|
info.setInferenceprovenance(dedupConf.getWf().getConfigurationId());
|
|
|
|
|
Qualifier provenanceAction = new Qualifier();
|
|
|
|
|
provenanceAction.setClassid(PROVENANCE_ACTION_CLASS);
|
|
|
|
|
provenanceAction.setClassname(PROVENANCE_ACTION_CLASS);
|
|
|
|
|
provenanceAction.setClassid(PROVENANCE_DEDUP);
|
|
|
|
|
provenanceAction.setClassname(PROVENANCE_DEDUP);
|
|
|
|
|
provenanceAction.setSchemeid(DNET_PROVENANCE_ACTIONS);
|
|
|
|
|
provenanceAction.setSchemename(DNET_PROVENANCE_ACTIONS);
|
|
|
|
|
info.setProvenanceaction(provenanceAction);
|
|
|
|
|