2020-04-27 14:52:31 +02:00
|
|
|
|
2020-03-27 13:48:44 +01:00
|
|
|
package eu.dnetlib.dedup;
|
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
import java.util.List;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2020-03-27 13:48:44 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.api.java.JavaPairRDD;
|
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
2020-07-31 09:06:57 +02:00
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
2020-03-27 13:48:44 +01:00
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
2020-07-31 09:06:57 +02:00
|
|
|
import org.codehaus.jackson.map.ObjectMapper;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2020-07-31 09:06:57 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Oaf;
|
2020-04-28 11:23:29 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
|
|
|
import eu.dnetlib.pace.config.DedupConfig;
|
|
|
|
import eu.dnetlib.pace.model.MapDocument;
|
|
|
|
import eu.dnetlib.pace.util.MapDocumentUtil;
|
2020-03-27 13:48:44 +01:00
|
|
|
import scala.Tuple2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Spark class creates similarity relations between entities, saving result
|
2020-04-27 14:52:31 +02:00
|
|
|
* <p>
|
|
|
|
* param request: sourcePath entityType target Path
|
2020-03-27 13:48:44 +01:00
|
|
|
*/
|
|
|
|
public class SparkCreateSimRels {
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
SparkCreateSimRels.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/sx/dedup/dedup_parameters.json")));
|
|
|
|
parser.parseArgument(args);
|
|
|
|
final SparkSession spark = SparkSession
|
|
|
|
.builder()
|
|
|
|
.appName(SparkCreateSimRels.class.getSimpleName())
|
|
|
|
.master(parser.get("master"))
|
|
|
|
.getOrCreate();
|
2020-03-27 13:48:44 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
|
|
|
final String inputPath = parser.get("sourcePath");
|
|
|
|
final String entity = parser.get("entity");
|
|
|
|
final String targetPath = parser.get("targetPath");
|
|
|
|
// final DedupConfig dedupConf =
|
|
|
|
// DedupConfig.load(IOUtils.toString(SparkCreateSimRels.class.getResourceAsStream("/eu/dnetlib/dhp/dedup/conf/org.curr.conf.json")));
|
|
|
|
final DedupConfig dedupConf = DedupConfig.load(parser.get("dedupConf"));
|
2020-03-27 13:48:44 +01:00
|
|
|
|
2020-07-31 09:06:57 +02:00
|
|
|
JavaPairRDD<String, MapDocument> mapDocument = spark
|
|
|
|
.read()
|
|
|
|
.load(inputPath + "/" + entity)
|
|
|
|
.as(Encoders.kryo(Oaf.class))
|
|
|
|
.map((MapFunction<Oaf, String>) p -> new ObjectMapper().writeValueAsString(p), Encoders.STRING())
|
|
|
|
.javaRDD()
|
2020-08-06 10:28:24 +02:00
|
|
|
.repartition(1000)
|
2020-04-27 14:52:31 +02:00
|
|
|
.mapToPair(
|
|
|
|
s -> {
|
|
|
|
MapDocument d = MapDocumentUtil.asMapDocumentWithJPath(dedupConf, s);
|
|
|
|
return new Tuple2<>(d.getIdentifier(), d);
|
|
|
|
});
|
2020-03-27 13:48:44 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
// create blocks for deduplication
|
|
|
|
JavaPairRDD<String, List<MapDocument>> blocks = Deduper.createsortedBlocks(sc, mapDocument, dedupConf);
|
|
|
|
// JavaPairRDD<String, Iterable<MapDocument>> blocks = Deduper.createBlocks(sc,
|
|
|
|
// mapDocument, dedupConf);
|
2020-03-27 13:48:44 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
// create relations by comparing only elements in the same group
|
|
|
|
final JavaPairRDD<String, String> dedupRels = Deduper.computeRelations2(sc, blocks, dedupConf);
|
|
|
|
// final JavaPairRDD<String,String> dedupRels = Deduper.computeRelations(sc, blocks,
|
|
|
|
// dedupConf);
|
2020-03-27 13:48:44 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final JavaRDD<Relation> isSimilarToRDD = dedupRels
|
|
|
|
.map(
|
|
|
|
simRel -> {
|
|
|
|
final Relation r = new Relation();
|
|
|
|
r.setSource(simRel._1());
|
|
|
|
r.setTarget(simRel._2());
|
|
|
|
r.setRelClass("isSimilarTo");
|
|
|
|
return r;
|
|
|
|
});
|
2020-03-27 13:48:44 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
spark
|
|
|
|
.createDataset(isSimilarToRDD.rdd(), Encoders.bean(Relation.class))
|
|
|
|
.write()
|
|
|
|
.mode("overwrite")
|
|
|
|
.save(DedupUtility.createSimRelPath(targetPath, entity));
|
|
|
|
}
|
2020-03-27 13:48:44 +01:00
|
|
|
}
|