modified the writing of the new relations. before: read old rels, add the new ones to them, write all the relations in new location. Now: first step of the wf copies the old relation i new location. If new relations are found, they are saved in the new location in append mode.

This commit is contained in:
Miriam Baglioni 2020-04-15 12:32:01 +02:00
parent 43f0590d4b
commit 964b22d418
1 changed files with 78 additions and 78 deletions

View File

@ -3,10 +3,9 @@ package eu.dnetlib.dhp.projecttoresult;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.dhp.TypedRow; import eu.dnetlib.dhp.TypedRow;
import eu.dnetlib.dhp.application.ArgumentApplicationParser; import eu.dnetlib.dhp.application.ArgumentApplicationParser;
import eu.dnetlib.dhp.schema.oaf.*; import eu.dnetlib.dhp.schema.oaf.Relation;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.Text;
import org.apache.spark.SparkConf; import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD; import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD; import org.apache.spark.api.java.JavaRDD;
@ -17,59 +16,65 @@ import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession; import org.apache.spark.sql.SparkSession;
import scala.Tuple2; import scala.Tuple2;
import java.io.File; import java.util.ArrayList;
import java.util.*; import java.util.Arrays;
import java.util.List;
import java.util.Set;
import static eu.dnetlib.dhp.PropagationConstant.*; import static eu.dnetlib.dhp.PropagationConstant.*;
import static eu.dnetlib.dhp.PropagationConstant.toPair;
public class SparkResultToProjectThroughSemRelJob { public class SparkResultToProjectThroughSemRelJob2 {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
final ArgumentApplicationParser parser = new ArgumentApplicationParser(IOUtils.toString(SparkResultToProjectThroughSemRelJob.class.getResourceAsStream("/eu/dnetlib/dhp/projecttoresult/input_projecttoresult_parameters.json"))); final ArgumentApplicationParser parser = new ArgumentApplicationParser(
IOUtils.toString(SparkResultToProjectThroughSemRelJob2.class
.getResourceAsStream("/eu/dnetlib/dhp/projecttoresult/input_projecttoresult_parameters.json")));
parser.parseArgument(args); parser.parseArgument(args);
parser.getObjectMap().keySet().stream().forEach(k -> System.out.println(k + " = " + parser.getObjectMap().get(k)));
SparkConf conf = new SparkConf(); SparkConf conf = new SparkConf();
conf.set("hive.metastore.uris", parser.get("hive_metastore_uris")); conf.set("hive.metastore.uris", parser.get("hive_metastore_uris"));
final SparkSession spark = SparkSession final SparkSession spark = SparkSession
.builder() .builder()
.appName(SparkResultToProjectThroughSemRelJob.class.getSimpleName()) .appName(SparkResultToProjectThroughSemRelJob2.class.getSimpleName())
.master(parser.get("master")) .master(parser.get("master"))
.config(conf) .config(conf)
.enableHiveSupport() .enableHiveSupport()
.getOrCreate(); .getOrCreate();
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext()); final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
final String inputPath = parser.get("sourcePath"); final String inputPath = parser.get("sourcePath");
final String outputPath = "/tmp/provision/propagation/projecttoresult"; final String outputPath = "/tmp/provision/propagation/projecttoresult";
boolean writeUpdates = "true".equals(parser.get("writeUpdate"));
boolean saveGraph = "true".equals(parser.get("saveGraph"));
final List<String> allowedsemrel = Arrays.asList(parser.get("allowedsemrels").split(";")); final List<String> allowedsemrel = Arrays.asList(parser.get("allowedsemrels").split(";"));
createOutputDirs(outputPath, FileSystem.get(spark.sparkContext().hadoopConfiguration())); createOutputDirs(outputPath, FileSystem.get(spark.sparkContext().hadoopConfiguration()));
JavaRDD<Relation> all_relations = sc.textFile(inputPath + "/relation") Dataset<Relation> relation = spark.createDataset(sc.textFile(inputPath + "/relation")
.map(item -> new ObjectMapper().readValue(item, Relation.class)); .map(item -> new ObjectMapper().readValue(item, Relation.class)).rdd(), Encoders.bean(Relation.class));
relation.createOrReplaceTempView("relation");
JavaRDD<Relation> relations = all_relations.filter(r -> !r.getDataInfo().getDeletedbyinference()).cache(); String query = "Select source, target " +
"from relation " +
"where datainfo.deletedbyinference = false and relClass = '" + RELATION_RESULT_PROJECT_REL_CLASS + "'";
JavaRDD<Relation> result_result = relations Dataset<Row> resproj_relation = spark.sql(query);
.filter(r -> allowedsemrel.contains(r.getRelClass()) && RELATION_RESULTRESULT_REL_TYPE.equals(r.getRelType()));
query = "Select source, target " +
org.apache.spark.sql.Dataset<Relation> resres_relation = spark.createDataset(result_result.rdd(), "from relation " +
Encoders.bean(Relation.class)); "where datainfo.deletedbyinference = false " + getConstraintList(" relClass = '", allowedsemrel );
JavaRDD<Relation> result_project = relations
.filter(r -> RELATION_RESULT_PROJECT_REL_CLASS.equals(r.getRelClass())
&& RELATION_RESULTPROJECT_REL_TYPE.equals(r.getRelType()));
org.apache.spark.sql.Dataset<Relation> resproj_relation = spark.createDataset(result_project.rdd(),
Encoders.bean(Relation.class));
Dataset<Row> resres_relation = spark.sql(query);
resres_relation.createOrReplaceTempView("resres_relation"); resres_relation.createOrReplaceTempView("resres_relation");
resproj_relation.createOrReplaceTempView("resproj_relation"); resproj_relation.createOrReplaceTempView("resproj_relation");
String query ="SELECT proj, collect_set(r1target) result_set " + query ="SELECT proj, collect_set(r1target) result_set " +
"FROM (" + "FROM (" +
" SELECT r1.source as sourcer, r1.relclass as r1rel, r1.target as r1target, r2.target as proj " + " SELECT r1.source as source, r1.target as r1target, r2.target as proj " +
" FROM resres_relation r1 " + " FROM resres_relation r1 " +
" JOIN resproj_relation r2 " + " JOIN resproj_relation r2 " +
" ON r1.source = r2.source " + " ON r1.source = r2.source " +
@ -77,68 +82,63 @@ public class SparkResultToProjectThroughSemRelJob {
"GROUP BY proj "; "GROUP BY proj ";
Dataset<Row> toaddrelations = spark.sql(query); Dataset<Row> toaddrelations = spark.sql(query);
query = "select target, collect_set(source) result_list from " +
"resproj_relation " +
"group by target";
Dataset<Row> project_resultlist = spark.sql(query);
JavaPairRDD<String, TypedRow> project_resultlist = relations //if (writeUpdaes){
.filter(r -> RELATION_PROJECT_RESULT_REL_CLASS.equals(r.getRelClass())) toaddrelations.toJavaRDD().map(r->new ObjectMapper().writeValueAsString(r))
.map(r -> { .saveAsTextFile(outputPath + "/toupdaterelations");
TypedRow tp = new TypedRow(); //}
tp.setSourceId(r.getSource());
tp.add(r.getTarget());
return tp;
}).mapToPair(toPair())
.reduceByKey((a, b) -> {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
a.addAll(b.getAccumulator()); if(saveGraph){
return a; JavaRDD<Relation> new_relations = toaddrelations.toJavaRDD().mapToPair(r -> new Tuple2<>(r.getString(0), r.getList(1)))
}).cache(); .leftOuterJoin(project_resultlist.toJavaRDD().mapToPair(pr -> new Tuple2<>(pr.getString(0), pr.getList(1))))
.flatMap(c -> {
List<Object> toAddRel = new ArrayList<>();
JavaRDD<Relation> new_relations = toaddrelations.toJavaRDD().mapToPair(r -> new Tuple2<>(r.getString(0), r.getList(1))) toAddRel.addAll(c._2()._1());
.leftOuterJoin(project_resultlist) if (c._2()._2().isPresent()) {
.flatMap(c -> { List<Object> originalRels = c._2()._2().get();
List<Object> toAddRel = new ArrayList<>(); for (Object o : originalRels) {
toAddRel.addAll(c._2()._1()); if (toAddRel.contains(o)) {
if (c._2()._2().isPresent()) { toAddRel.remove(o);
Set<String> originalRels = c._2()._2().get().getAccumulator(); }
for (String o : originalRels) {
if (toAddRel.contains(o)) {
toAddRel.remove(o);
} }
} }
} List<Relation> relationList = new ArrayList<>();
List<Relation> relationList = new ArrayList<>(); String projId = c._1();
String projId = c._1(); for (Object r : toAddRel) {
for (Object r : toAddRel) { String rId = (String) r;
String rId = (String) r; relationList.add(getRelation(rId, projId, RELATION_RESULT_PROJECT_REL_CLASS, RELATION_RESULTPROJECT_REL_TYPE,
relationList.add(getRelation(rId, projId, RELATION_RESULT_PROJECT_REL_CLASS, RELATION_RESULTPROJECT_REL_TYPE, RELATION_RESULTPROJECT_SUBREL_TYPE, PROPAGATION_DATA_INFO_TYPE,
RELATION_RESULTPROJECT_SUBREL_TYPE, PROPAGATION_DATA_INFO_TYPE, PROPAGATION_RELATION_RESULT_PROJECT_SEM_REL_CLASS_ID,
PROPAGATION_RELATION_RESULT_PROJECT_SEM_REL_CLASS_ID, PROPAGATION_RELATION_RESULT_PROJECT_SEM_REL_CLASS_NAME));
PROPAGATION_RELATION_RESULT_PROJECT_SEM_REL_CLASS_NAME)); relationList.add(getRelation(projId, rId, RELATION_PROJECT_RESULT_REL_CLASS, RELATION_RESULTPROJECT_REL_TYPE,
relationList.add(getRelation(projId, rId, RELATION_PROJECT_RESULT_REL_CLASS, RELATION_RESULTPROJECT_REL_TYPE, RELATION_RESULTPROJECT_SUBREL_TYPE, PROPAGATION_DATA_INFO_TYPE,
RELATION_RESULTPROJECT_SUBREL_TYPE, PROPAGATION_DATA_INFO_TYPE, PROPAGATION_RELATION_RESULT_PROJECT_SEM_REL_CLASS_ID,
PROPAGATION_RELATION_RESULT_PROJECT_SEM_REL_CLASS_ID, PROPAGATION_RELATION_RESULT_PROJECT_SEM_REL_CLASS_NAME));
PROPAGATION_RELATION_RESULT_PROJECT_SEM_REL_CLASS_NAME));
} }
return relationList.iterator(); if(relationList.size()==0){
}).cache(); return null;
}
return relationList.iterator();
}).filter(r -> !(r==null));
toaddrelations.toJavaRDD().map(r->new ObjectMapper().writeValueAsString(r))
.saveAsTextFile(outputPath + "/toupdaterelations");
new_relations.map(r-> new ObjectMapper().writeValueAsString(r)) new_relations.map(r-> new ObjectMapper().writeValueAsString(r))
.saveAsTextFile(outputPath + "/new_relations" ); .saveAsTextFile(outputPath + "/new_relations" );
all_relations.union(new_relations) sc.textFile(inputPath + "/relation")
.map(r -> new ObjectMapper().writeValueAsString(r)) .map(item -> new ObjectMapper().readValue(item, Relation.class))
.saveAsTextFile(outputPath + "/relation"); .union(new_relations)
.map(r -> new ObjectMapper().writeValueAsString(r))
.saveAsTextFile(outputPath + "/relation");
}
//JavaPairRDD<String, TypedRow> result_result = getResultResultSemRel(allowedsemrel, relations); //JavaPairRDD<String, TypedRow> result_result = getResultResultSemRel(allowedsemrel, relations);