added check for possible Optional value in relation dataInfo

This commit is contained in:
Miriam Baglioni 2020-10-30 14:12:28 +01:00
parent 5f4de9a962
commit a9eef9c852
1 changed files with 33 additions and 14 deletions

View File

@ -7,20 +7,25 @@ import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.spark.SparkConf; import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.MapFunction; import org.apache.spark.api.java.function.MapFunction;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders; import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.SaveMode; import org.apache.spark.sql.SaveMode;
import org.apache.spark.sql.SparkSession; import org.apache.spark.sql.SparkSession;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import eu.dnetlib.dhp.application.ArgumentApplicationParser; import eu.dnetlib.dhp.application.ArgumentApplicationParser;
import eu.dnetlib.dhp.oa.graph.dump.Utils; import eu.dnetlib.dhp.oa.graph.dump.Utils;
import eu.dnetlib.dhp.schema.common.ModelSupport; import eu.dnetlib.dhp.schema.common.ModelSupport;
import eu.dnetlib.dhp.schema.dump.oaf.Provenance; import eu.dnetlib.dhp.schema.dump.oaf.Provenance;
import eu.dnetlib.dhp.schema.dump.oaf.graph.Node; import eu.dnetlib.dhp.schema.dump.oaf.graph.Node;
import eu.dnetlib.dhp.schema.dump.oaf.graph.RelType; import eu.dnetlib.dhp.schema.dump.oaf.graph.RelType;
import eu.dnetlib.dhp.schema.oaf.DataInfo;
import eu.dnetlib.dhp.schema.oaf.Relation; import eu.dnetlib.dhp.schema.oaf.Relation;
/** /**
@ -66,40 +71,54 @@ public class SparkDumpRelationJob implements Serializable {
} }
private static void dumpRelation(SparkSession spark, String inputPath, String outputPath) { private static void dumpRelation(SparkSession spark, String inputPath, String outputPath) {
Utils Dataset<Relation> relations = Utils.readPath(spark, inputPath, Relation.class);
.readPath(spark, inputPath, Relation.class) relations
.map((MapFunction<Relation, eu.dnetlib.dhp.schema.dump.oaf.graph.Relation>) relation -> { .map((MapFunction<Relation, eu.dnetlib.dhp.schema.dump.oaf.graph.Relation>) relation -> {
eu.dnetlib.dhp.schema.dump.oaf.graph.Relation rel = new eu.dnetlib.dhp.schema.dump.oaf.graph.Relation(); eu.dnetlib.dhp.schema.dump.oaf.graph.Relation rel_new = new eu.dnetlib.dhp.schema.dump.oaf.graph.Relation();
rel rel_new
.setSource( .setSource(
Node Node
.newInstance( .newInstance(
relation.getSource(), relation.getSource(),
ModelSupport.idPrefixEntity.get(relation.getSource().substring(0, 2)))); ModelSupport.idPrefixEntity.get(relation.getSource().substring(0, 2))));
rel rel_new
.setTarget( .setTarget(
Node Node
.newInstance( .newInstance(
relation.getTarget(), relation.getTarget(),
ModelSupport.idPrefixEntity.get(relation.getTarget().substring(0, 2)))); ModelSupport.idPrefixEntity.get(relation.getTarget().substring(0, 2))));
rel rel_new
.setReltype( .setReltype(
RelType RelType
.newInstance( .newInstance(
relation.getRelClass(), relation.getRelClass(),
relation.getSubRelType())); relation.getSubRelType()));
Optional Optional<DataInfo> odInfo = Optional.ofNullable(relation.getDataInfo());
.ofNullable(relation.getDataInfo()) if (odInfo.isPresent()) {
.ifPresent( DataInfo dInfo = odInfo.get();
datainfo -> rel if (Optional.ofNullable(dInfo.getProvenanceaction()).isPresent()) {
if (Optional.ofNullable(dInfo.getProvenanceaction().getClassname()).isPresent()) {
rel_new
.setProvenance( .setProvenance(
Provenance Provenance
.newInstance(datainfo.getProvenanceaction().getClassname(), datainfo.getTrust()))); .newInstance(
dInfo.getProvenanceaction().getClassname(),
dInfo.getTrust()));
}
}
}
// Optional
// .ofNullable(relation.getDataInfo())
// .ifPresent(
// datainfo -> rel_new
// .setProvenance(
// Provenance
// .newInstance(datainfo.getProvenanceaction().getClassname(), datainfo.getTrust())));
return rel; return rel_new;
}, Encoders.bean(eu.dnetlib.dhp.schema.dump.oaf.graph.Relation.class)) }, Encoders.bean(eu.dnetlib.dhp.schema.dump.oaf.graph.Relation.class))
.write() .write()