forked from D-Net/dnet-hadoop
Rewrite SparkPropagateRelation exploiting Dataframe API
This commit is contained in:
parent
9c8b41475a
commit
0d7b2bf83d
|
@ -1,19 +1,6 @@
|
||||||
|
|
||||||
package eu.dnetlib.dhp.oa.dedup;
|
package eu.dnetlib.dhp.oa.dedup;
|
||||||
|
|
||||||
import static org.apache.spark.sql.functions.col;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.apache.spark.SparkConf;
|
|
||||||
import org.apache.spark.api.java.function.FilterFunction;
|
|
||||||
import org.apache.spark.api.java.function.MapFunction;
|
|
||||||
import org.apache.spark.sql.*;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
||||||
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
||||||
import eu.dnetlib.dhp.schema.common.ModelSupport;
|
import eu.dnetlib.dhp.schema.common.ModelSupport;
|
||||||
|
@ -21,198 +8,163 @@ import eu.dnetlib.dhp.schema.oaf.DataInfo;
|
||||||
import eu.dnetlib.dhp.schema.oaf.Relation;
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
||||||
import eu.dnetlib.dhp.utils.ISLookupClientFactory;
|
import eu.dnetlib.dhp.utils.ISLookupClientFactory;
|
||||||
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.spark.SparkConf;
|
||||||
|
import org.apache.spark.api.java.function.FilterFunction;
|
||||||
|
import org.apache.spark.api.java.function.MapFunction;
|
||||||
|
import org.apache.spark.api.java.function.ReduceFunction;
|
||||||
|
import org.apache.spark.sql.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import scala.Tuple2;
|
import scala.Tuple2;
|
||||||
import scala.Tuple3;
|
import scala.Tuple3;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static org.apache.spark.sql.functions.col;
|
||||||
|
|
||||||
public class SparkPropagateRelation extends AbstractSparkAction {
|
public class SparkPropagateRelation extends AbstractSparkAction {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(SparkPropagateRelation.class);
|
private static final Logger log = LoggerFactory.getLogger(SparkPropagateRelation.class);
|
||||||
|
|
||||||
enum FieldType {
|
private static Encoder<Relation> REL_BEAN_ENC = Encoders.bean(Relation.class);
|
||||||
SOURCE, TARGET
|
|
||||||
}
|
|
||||||
|
|
||||||
public SparkPropagateRelation(ArgumentApplicationParser parser, SparkSession spark) {
|
private static Encoder<Relation> REL_KRYO_ENC = Encoders.kryo(Relation.class);
|
||||||
super(parser, spark);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public SparkPropagateRelation(ArgumentApplicationParser parser, SparkSession spark) {
|
||||||
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
super(parser, spark);
|
||||||
IOUtils
|
}
|
||||||
.toString(
|
|
||||||
SparkPropagateRelation.class
|
|
||||||
.getResourceAsStream(
|
|
||||||
"/eu/dnetlib/dhp/oa/dedup/propagateRelation_parameters.json")));
|
|
||||||
|
|
||||||
parser.parseArgument(args);
|
public static void main(String[] args) throws Exception {
|
||||||
|
ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
||||||
|
IOUtils
|
||||||
|
.toString(
|
||||||
|
SparkPropagateRelation.class
|
||||||
|
.getResourceAsStream(
|
||||||
|
"/eu/dnetlib/dhp/oa/dedup/propagateRelation_parameters.json")));
|
||||||
|
|
||||||
SparkConf conf = new SparkConf();
|
parser.parseArgument(args);
|
||||||
conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer");
|
|
||||||
conf.registerKryoClasses(ModelSupport.getOafModelClasses());
|
|
||||||
|
|
||||||
new SparkPropagateRelation(parser, getSparkSession(conf))
|
SparkConf conf = new SparkConf();
|
||||||
.run(ISLookupClientFactory.getLookUpService(parser.get("isLookUpUrl")));
|
conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer");
|
||||||
}
|
conf.registerKryoClasses(ModelSupport.getOafModelClasses());
|
||||||
|
|
||||||
@Override
|
new SparkPropagateRelation(parser, getSparkSession(conf))
|
||||||
public void run(ISLookUpService isLookUpService) {
|
.run(ISLookupClientFactory.getLookUpService(parser.get("isLookUpUrl")));
|
||||||
|
}
|
||||||
|
|
||||||
final String graphBasePath = parser.get("graphBasePath");
|
@Override
|
||||||
final String workingPath = parser.get("workingPath");
|
public void run(ISLookUpService isLookUpService) {
|
||||||
final String graphOutputPath = parser.get("graphOutputPath");
|
|
||||||
|
|
||||||
log.info("graphBasePath: '{}'", graphBasePath);
|
final String graphBasePath = parser.get("graphBasePath");
|
||||||
log.info("workingPath: '{}'", workingPath);
|
final String workingPath = parser.get("workingPath");
|
||||||
log.info("graphOutputPath: '{}'", graphOutputPath);
|
final String graphOutputPath = parser.get("graphOutputPath");
|
||||||
|
|
||||||
final String outputRelationPath = DedupUtility.createEntityPath(graphOutputPath, "relation");
|
log.info("graphBasePath: '{}'", graphBasePath);
|
||||||
removeOutputDir(spark, outputRelationPath);
|
log.info("workingPath: '{}'", workingPath);
|
||||||
|
log.info("graphOutputPath: '{}'", graphOutputPath);
|
||||||
|
|
||||||
Dataset<Relation> mergeRels = spark
|
final String outputRelationPath = DedupUtility.createEntityPath(graphOutputPath, "relation");
|
||||||
.read()
|
removeOutputDir(spark, outputRelationPath);
|
||||||
.load(DedupUtility.createMergeRelPath(workingPath, "*", "*"))
|
|
||||||
.as(Encoders.bean(Relation.class));
|
|
||||||
|
|
||||||
// <mergedObjectID, dedupID>
|
Dataset<Relation> mergeRels = spark
|
||||||
Dataset<Tuple2<String, String>> mergedIds = mergeRels
|
.read()
|
||||||
.where(col("relClass").equalTo(ModelConstants.MERGES))
|
.load(DedupUtility.createMergeRelPath(workingPath, "*", "*"))
|
||||||
.select(col("source"), col("target"))
|
.as(REL_BEAN_ENC);
|
||||||
.distinct()
|
|
||||||
.map(
|
|
||||||
(MapFunction<Row, Tuple2<String, String>>) r -> new Tuple2<>(r.getString(1), r.getString(0)),
|
|
||||||
Encoders.tuple(Encoders.STRING(), Encoders.STRING()))
|
|
||||||
.cache();
|
|
||||||
|
|
||||||
final String relationPath = DedupUtility.createEntityPath(graphBasePath, "relation");
|
// <mergedObjectID, dedupID>
|
||||||
|
Dataset<Row> mergedIds = mergeRels
|
||||||
|
.where(col("relClass").equalTo(ModelConstants.MERGES))
|
||||||
|
.select(col("source").as("dedupID"), col("target").as("mergedObjectID"))
|
||||||
|
.distinct()
|
||||||
|
.cache();
|
||||||
|
|
||||||
Dataset<Relation> rels = spark.read().textFile(relationPath).map(patchRelFn(), Encoders.bean(Relation.class));
|
final String inputRelationPath = DedupUtility.createEntityPath(graphBasePath, "relation");
|
||||||
|
|
||||||
Dataset<Relation> newRels = createNewRels(rels, mergedIds, getFixRelFn());
|
Dataset<Relation> rels = spark.read().schema(REL_BEAN_ENC.schema()).json(inputRelationPath)
|
||||||
|
.as(REL_BEAN_ENC)
|
||||||
|
// .map((MapFunction<Relation, Relation>) rel -> {
|
||||||
|
// if (rel.getDataInfo() == null) {
|
||||||
|
// rel.setDataInfo(new DataInfo());
|
||||||
|
// }
|
||||||
|
// return rel;
|
||||||
|
// }, REL_BEAN_ENC)
|
||||||
|
;
|
||||||
|
|
||||||
Dataset<Relation> updated = processDataset(
|
Dataset<Tuple3<Relation, String, String>> dedupedRels = rels
|
||||||
processDataset(rels, mergedIds, FieldType.SOURCE, getDeletedFn()),
|
.joinWith(mergedIds, rels.col("source").equalTo(mergedIds.col("mergedObjectID")), "left_outer")
|
||||||
mergedIds,
|
.joinWith(mergedIds, col("_1.target").equalTo(mergedIds.col("mergedObjectID")), "left_outer")
|
||||||
FieldType.TARGET,
|
.filter("_1._2 IS NOT NULL OR _2 IS NOT NULL")
|
||||||
getDeletedFn());
|
.select("_1._1", "_1._2.dedupID", "_2.dedupID")
|
||||||
|
.as(Encoders.tuple(REL_BEAN_ENC, Encoders.STRING(), Encoders.STRING()))
|
||||||
|
.cache();
|
||||||
|
|
||||||
save(
|
mergedIds.unpersist();
|
||||||
distinctRelations(
|
|
||||||
newRels
|
|
||||||
.union(updated)
|
|
||||||
.union(mergeRels)
|
|
||||||
.map((MapFunction<Relation, Relation>) r -> r, Encoders.kryo(Relation.class)))
|
|
||||||
.filter((FilterFunction<Relation>) r -> !Objects.equals(r.getSource(), r.getTarget())),
|
|
||||||
outputRelationPath, SaveMode.Overwrite);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Dataset<Relation> distinctRelations(Dataset<Relation> rels) {
|
Dataset<Relation> newRels = dedupedRels
|
||||||
return rels
|
.map((MapFunction<Tuple3<Relation, String, String>, Relation>) t -> {
|
||||||
.filter(getRelationFilterFunction())
|
Relation r = t._1();
|
||||||
.groupByKey(
|
String newSource = t._2();
|
||||||
(MapFunction<Relation, String>) r -> String
|
String newTarget = t._3();
|
||||||
.join(r.getSource(), r.getTarget(), r.getRelType(), r.getSubRelType(), r.getRelClass()),
|
|
||||||
Encoders.STRING())
|
|
||||||
.agg(new RelationAggregator().toColumn())
|
|
||||||
.map((MapFunction<Tuple2<String, Relation>, Relation>) Tuple2::_2, Encoders.bean(Relation.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
// redirect the relations to the dedupID
|
if (r.getDataInfo() == null) {
|
||||||
private static Dataset<Relation> createNewRels(
|
r.setDataInfo(new DataInfo());
|
||||||
Dataset<Relation> rels, // all the relations to be redirected
|
}
|
||||||
Dataset<Tuple2<String, String>> mergedIds, // merge rels: <mergedObjectID, dedupID>
|
r.getDataInfo().setDeletedbyinference(false);
|
||||||
MapFunction<Tuple2<Tuple2<Tuple3<String, Relation, String>, Tuple2<String, String>>, Tuple2<String, String>>, Relation> mapRel) {
|
|
||||||
|
|
||||||
// <sourceID, relation, targetID>
|
if (newSource != null)
|
||||||
Dataset<Tuple3<String, Relation, String>> mapped = rels
|
r.setSource(newSource);
|
||||||
.map(
|
|
||||||
(MapFunction<Relation, Tuple3<String, Relation, String>>) r -> new Tuple3<>(getId(r, FieldType.SOURCE),
|
|
||||||
r, getId(r, FieldType.TARGET)),
|
|
||||||
Encoders.tuple(Encoders.STRING(), Encoders.kryo(Relation.class), Encoders.STRING()));
|
|
||||||
|
|
||||||
// < <sourceID, relation, target>, <sourceID, dedupID> >
|
if (newTarget != null)
|
||||||
Dataset<Tuple2<Tuple3<String, Relation, String>, Tuple2<String, String>>> relSource = mapped
|
r.setTarget(newTarget);
|
||||||
.joinWith(mergedIds, mapped.col("_1").equalTo(mergedIds.col("_1")), "left_outer");
|
|
||||||
|
|
||||||
// < <<sourceID, relation, targetID>, <sourceID, dedupID>>, <targetID, dedupID> >
|
return r;
|
||||||
Dataset<Tuple2<Tuple2<Tuple3<String, Relation, String>, Tuple2<String, String>>, Tuple2<String, String>>> relSourceTarget = relSource
|
}, REL_BEAN_ENC)
|
||||||
.joinWith(mergedIds, relSource.col("_1._3").equalTo(mergedIds.col("_1")), "left_outer");
|
.distinct();
|
||||||
|
|
||||||
return relSourceTarget
|
Dataset<Relation> updated = dedupedRels
|
||||||
.filter(
|
.map((MapFunction<Tuple3<Relation, String, String>, Relation>) t -> {
|
||||||
(FilterFunction<Tuple2<Tuple2<Tuple3<String, Relation, String>, Tuple2<String, String>>, Tuple2<String, String>>>) r -> r
|
Relation r = t._1();
|
||||||
._1()
|
if (r.getDataInfo() == null) {
|
||||||
._1() != null || r._2() != null)
|
r.setDataInfo(new DataInfo());
|
||||||
.map(mapRel, Encoders.bean(Relation.class))
|
}
|
||||||
.distinct();
|
r.getDataInfo().setDeletedbyinference(true);
|
||||||
}
|
return r;
|
||||||
|
}, REL_BEAN_ENC);
|
||||||
|
|
||||||
private static Dataset<Relation> processDataset(
|
save(
|
||||||
Dataset<Relation> rels,
|
distinctRelations(
|
||||||
Dataset<Tuple2<String, String>> mergedIds,
|
newRels
|
||||||
FieldType type,
|
.union(updated)
|
||||||
MapFunction<Tuple2<Tuple2<String, Relation>, Tuple2<String, String>>, Relation> mapFn) {
|
.union(mergeRels)
|
||||||
final Dataset<Tuple2<String, Relation>> mapped = rels
|
.map((MapFunction<Relation, Relation>) r -> r, REL_KRYO_ENC)
|
||||||
.map(
|
)
|
||||||
(MapFunction<Relation, Tuple2<String, Relation>>) r -> new Tuple2<>(getId(r, type), r),
|
.filter((FilterFunction<Relation>) r -> !Objects.equals(r.getSource(), r.getTarget())),
|
||||||
Encoders.tuple(Encoders.STRING(), Encoders.kryo(Relation.class)));
|
outputRelationPath, SaveMode.Overwrite);
|
||||||
return mapped
|
}
|
||||||
.joinWith(mergedIds, mapped.col("_1").equalTo(mergedIds.col("_1")), "left_outer")
|
|
||||||
.map(mapFn, Encoders.bean(Relation.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
private FilterFunction<Relation> getRelationFilterFunction() {
|
private Dataset<Relation> distinctRelations(Dataset<Relation> rels) {
|
||||||
return r -> StringUtils.isNotBlank(r.getSource()) ||
|
return rels
|
||||||
StringUtils.isNotBlank(r.getTarget()) ||
|
.filter(getRelationFilterFunction())
|
||||||
StringUtils.isNotBlank(r.getRelType()) ||
|
.groupByKey(
|
||||||
StringUtils.isNotBlank(r.getSubRelType()) ||
|
(MapFunction<Relation, String>) r -> String
|
||||||
StringUtils.isNotBlank(r.getRelClass());
|
.join(" ", r.getSource(), r.getTarget(), r.getRelType(), r.getSubRelType(), r.getRelClass()),
|
||||||
}
|
Encoders.STRING())
|
||||||
|
.reduceGroups((ReduceFunction<Relation>) (b, a) -> {
|
||||||
|
b.mergeFrom(a);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
private static String getId(Relation r, FieldType type) {
|
.map((MapFunction<Tuple2<String, Relation>, Relation>) Tuple2::_2, REL_BEAN_ENC);
|
||||||
switch (type) {
|
}
|
||||||
case SOURCE:
|
|
||||||
return r.getSource();
|
|
||||||
case TARGET:
|
|
||||||
return r.getTarget();
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static MapFunction<Tuple2<Tuple2<Tuple3<String, Relation, String>, Tuple2<String, String>>, Tuple2<String, String>>, Relation> getFixRelFn() {
|
|
||||||
return value -> {
|
|
||||||
|
|
||||||
Relation r = value._1()._1()._2();
|
|
||||||
String newSource = value._1()._2() != null ? value._1()._2()._2() : null;
|
|
||||||
String newTarget = value._2() != null ? value._2()._2() : null;
|
|
||||||
|
|
||||||
if (r.getDataInfo() == null) {
|
|
||||||
r.setDataInfo(new DataInfo());
|
|
||||||
}
|
|
||||||
r.getDataInfo().setDeletedbyinference(false);
|
|
||||||
|
|
||||||
if (newSource != null)
|
|
||||||
r.setSource(newSource);
|
|
||||||
|
|
||||||
if (newTarget != null)
|
|
||||||
r.setTarget(newTarget);
|
|
||||||
|
|
||||||
return r;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static MapFunction<Tuple2<Tuple2<String, Relation>, Tuple2<String, String>>, Relation> getDeletedFn() {
|
|
||||||
return value -> {
|
|
||||||
if (value._2() != null) {
|
|
||||||
Relation r = value._1()._2();
|
|
||||||
if (r.getDataInfo() == null) {
|
|
||||||
r.setDataInfo(new DataInfo());
|
|
||||||
}
|
|
||||||
r.getDataInfo().setDeletedbyinference(true);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
return value._1()._2();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private FilterFunction<Relation> getRelationFilterFunction() {
|
||||||
|
return r -> StringUtils.isNotBlank(r.getSource()) ||
|
||||||
|
StringUtils.isNotBlank(r.getTarget()) ||
|
||||||
|
StringUtils.isNotBlank(r.getRelType()) ||
|
||||||
|
StringUtils.isNotBlank(r.getSubRelType()) ||
|
||||||
|
StringUtils.isNotBlank(r.getRelClass());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue