forked from D-Net/dnet-hadoop
new parametrized implementation for country propagation
This commit is contained in:
parent
df2fc4a6d7
commit
03f7cb6402
|
@ -14,14 +14,13 @@ import org.apache.spark.sql.SaveMode;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static eu.dnetlib.dhp.PropagationConstant.createOutputDirs;
|
import static eu.dnetlib.dhp.PropagationConstant.createOutputDirs;
|
||||||
import static eu.dnetlib.dhp.PropagationConstant.getConstraintList;
|
import static eu.dnetlib.dhp.PropagationConstant.getConstraintList;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For the association of the country to the datasource
|
* For the association of the country to the datasource
|
||||||
|
@ -38,53 +37,46 @@ public class PrepareResultCountryAssociation {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
String jsonConfiguration = IOUtils.toString(PrepareResultCountryAssociation.class
|
String jsonConfiguration = IOUtils.toString(PrepareResultCountryAssociation.class
|
||||||
.getResourceAsStream("/eu/dnetlib/dhp/countrypropagation/input_countrypropagation_parameters.json"));
|
.getResourceAsStream("/eu/dnetlib/dhp/countrypropagation/input_prepare_dc_assoc.json"));
|
||||||
|
|
||||||
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
||||||
jsonConfiguration);
|
jsonConfiguration);
|
||||||
|
|
||||||
parser.parseArgument(args);
|
parser.parseArgument(args);
|
||||||
|
|
||||||
Boolean isSparkSessionManaged = Optional
|
|
||||||
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
||||||
.map(Boolean::valueOf)
|
|
||||||
.orElse(Boolean.TRUE);
|
|
||||||
|
|
||||||
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
||||||
|
|
||||||
String inputPath = parser.get("sourcePath");
|
String inputPath = parser.get("sourcePath");
|
||||||
log.info("inputPath: {}", inputPath);
|
log.info("inputPath: {}", inputPath);
|
||||||
|
|
||||||
|
|
||||||
String graphTableClassName = parser.get("graphTableClassName");
|
|
||||||
log.info("graphTableClassName: {}", graphTableClassName);
|
|
||||||
|
|
||||||
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"));
|
||||||
|
|
||||||
runWithSparkSession(conf, isSparkSessionManaged,
|
|
||||||
spark -> {
|
|
||||||
removeOutputDir(spark, outputPath);
|
|
||||||
joinRelationEntity(spark, inputRelationsPath, inputEntityPath, entityClazz, outputPath);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
final SparkSession spark = SparkSession
|
final SparkSession spark = SparkSession
|
||||||
.builder()
|
.builder()
|
||||||
.appName(PrepareResultCountryAssociation.class.getSimpleName())
|
.appName(SparkCountryPropagationJob.class.getSimpleName())
|
||||||
.master(parser.get("master"))
|
.master(parser.get("master"))
|
||||||
.config(conf)
|
.config(conf)
|
||||||
.enableHiveSupport()
|
.enableHiveSupport()
|
||||||
.getOrCreate();
|
.getOrCreate();
|
||||||
|
|
||||||
//todo add link to working dir
|
|
||||||
final String outputPath = "/tmp/provision/propagation/countrytoresultfrominstitutionalrepositories";
|
final String outputPath = "/tmp/provision/propagation/countrytoresultfrominstitutionalrepositories";
|
||||||
|
|
||||||
|
|
||||||
createOutputDirs(outputPath, FileSystem.get(spark.sparkContext().hadoopConfiguration()));
|
createOutputDirs(outputPath, FileSystem.get(spark.sparkContext().hadoopConfiguration()));
|
||||||
|
prepareDatasourceCountryAssociation(spark,
|
||||||
|
Arrays.asList(parser.get("whitelist").split(";")),
|
||||||
|
Arrays.asList(parser.get("allowedtypes").split(";")),
|
||||||
|
inputPath,
|
||||||
|
outputPath);
|
||||||
|
|
||||||
List<String> whitelist = Arrays.asList(parser.get("whitelist").split(";"));
|
|
||||||
List<String> allowedtypes = Arrays.asList(parser.get("allowedtypes").split(";"));
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void prepareDatasourceCountryAssociation(SparkSession spark,
|
||||||
|
List<String> whitelist,
|
||||||
|
List<String> allowedtypes,
|
||||||
|
String inputPath,
|
||||||
|
String outputPath) {
|
||||||
String whitelisted = "";
|
String whitelisted = "";
|
||||||
for (String i : whitelist){
|
for (String i : whitelist){
|
||||||
whitelisted += " OR id = '" + i + "'";
|
whitelisted += " OR id = '" + i + "'";
|
||||||
|
@ -93,13 +85,13 @@ public class PrepareResultCountryAssociation {
|
||||||
|
|
||||||
|
|
||||||
Dataset<Datasource> datasource = spark.createDataset(sc.textFile(inputPath + "/datasource")
|
Dataset<Datasource> datasource = spark.createDataset(sc.textFile(inputPath + "/datasource")
|
||||||
.map(item -> new ObjectMapper().readValue(item, Datasource.class)).rdd(), Encoders.bean(Datasource.class));
|
.map(item -> OBJECT_MAPPER.readValue(item, Datasource.class)).rdd(), Encoders.bean(Datasource.class));
|
||||||
|
|
||||||
Dataset<Relation> relation = spark.createDataset(sc.textFile(inputPath + "/relation")
|
Dataset<Relation> relation = spark.createDataset(sc.textFile(inputPath + "/relation")
|
||||||
.map(item -> new ObjectMapper().readValue(item, Relation.class)).rdd(), Encoders.bean(Relation.class));
|
.map(item -> OBJECT_MAPPER.readValue(item, Relation.class)).rdd(), Encoders.bean(Relation.class));
|
||||||
|
|
||||||
Dataset<Organization> organization = spark.createDataset(sc.textFile(inputPath + "/organization")
|
Dataset<Organization> organization = spark.createDataset(sc.textFile(inputPath + "/organization")
|
||||||
.map(item -> new ObjectMapper().readValue(item, Organization.class)).rdd(), Encoders.bean(Organization.class));
|
.map(item -> OBJECT_MAPPER.readValue(item, Organization.class)).rdd(), Encoders.bean(Organization.class));
|
||||||
|
|
||||||
datasource.createOrReplaceTempView("datasource");
|
datasource.createOrReplaceTempView("datasource");
|
||||||
relation.createOrReplaceTempView("relation");
|
relation.createOrReplaceTempView("relation");
|
||||||
|
@ -107,18 +99,18 @@ public class PrepareResultCountryAssociation {
|
||||||
|
|
||||||
String query = "SELECT source ds, country.classid country " +
|
String query = "SELECT source ds, country.classid country " +
|
||||||
"FROM ( SELECT id " +
|
"FROM ( SELECT id " +
|
||||||
"FROM datasource " +
|
" FROM datasource " +
|
||||||
"WHERE (datainfo.deletedbyinference = false " + whitelisted + ") " +
|
" WHERE (datainfo.deletedbyinference = false " + whitelisted + ") " +
|
||||||
getConstraintList("datasourcetype.classid = '", allowedtypes) + ") d " +
|
getConstraintList("datasourcetype.classid = '", allowedtypes) + ") d " +
|
||||||
"JOIN ( SELECT source, target " +
|
"JOIN ( SELECT source, target " +
|
||||||
"FROM relation " +
|
" FROM relation " +
|
||||||
"WHERE relclass = 'provides' " +
|
" WHERE relclass = 'provides' " +
|
||||||
"AND datainfo.deletedbyinference = false ) rel " +
|
" AND datainfo.deletedbyinference = false ) rel " +
|
||||||
"ON d.id = rel.source " +
|
"ON d.id = rel.source " +
|
||||||
"JOIN (SELECT id, country " +
|
"JOIN (SELECT id, country " +
|
||||||
"FROM organization " +
|
" FROM organization " +
|
||||||
"WHERE datainfo.deletedbyinference = false " +
|
" WHERE datainfo.deletedbyinference = false " +
|
||||||
"AND length(country.classid)>0) o " +
|
" AND length(country.classid)>0) o " +
|
||||||
"ON o.id = rel.target";
|
"ON o.id = rel.target";
|
||||||
|
|
||||||
spark.sql(query)
|
spark.sql(query)
|
||||||
|
@ -129,4 +121,6 @@ public class PrepareResultCountryAssociation {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,21 +4,17 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
||||||
import eu.dnetlib.dhp.schema.oaf.*;
|
import eu.dnetlib.dhp.schema.oaf.*;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.hadoop.fs.FileSystem;
|
|
||||||
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;
|
||||||
import org.apache.spark.api.java.JavaSparkContext;
|
import org.apache.spark.api.java.JavaSparkContext;
|
||||||
|
import org.apache.spark.api.java.function.MapFunction;
|
||||||
|
import org.apache.spark.broadcast.Broadcast;
|
||||||
|
import org.apache.spark.sql.*;
|
||||||
import org.apache.spark.sql.Dataset;
|
import org.apache.spark.sql.Dataset;
|
||||||
import org.apache.spark.sql.Encoders;
|
|
||||||
import org.apache.spark.sql.Row;
|
|
||||||
import org.apache.spark.sql.SparkSession;
|
|
||||||
import scala.Tuple2;
|
import scala.Tuple2;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static eu.dnetlib.dhp.PropagationConstant.*;
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -32,8 +28,25 @@ public class SparkCountryPropagationJob2 {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
final ArgumentApplicationParser parser = new ArgumentApplicationParser(IOUtils.toString(SparkCountryPropagationJob2.class.getResourceAsStream("/eu/dnetlib/dhp/countrypropagation/input_countrypropagation_parameters.json")));
|
String jsonConfiguration = IOUtils.toString(SparkCountryPropagationJob2.class
|
||||||
|
.getResourceAsStream("/eu/dnetlib/dhp/countrypropagation/input_countrypropagation_parameters.json"));
|
||||||
|
|
||||||
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
||||||
|
jsonConfiguration);
|
||||||
|
|
||||||
parser.parseArgument(args);
|
parser.parseArgument(args);
|
||||||
|
|
||||||
|
String inputPath = parser.get("sourcePath");
|
||||||
|
log.info("inputPath: {}", inputPath);
|
||||||
|
|
||||||
|
final String outputPath = "/tmp/provision/propagation/countrytoresultfrominstitutionalrepositories";
|
||||||
|
final String datasourcecountrypath = outputPath + "/prepared_datasource_country";
|
||||||
|
final String resultClassName = parser.get("resultClazz");
|
||||||
|
|
||||||
|
final String resultType = resultClassName.substring(resultClassName.lastIndexOf(".")+1);
|
||||||
|
|
||||||
|
Class<? extends Result> resultClazz = (Class<? extends Result>) Class.forName(resultClassName);
|
||||||
|
|
||||||
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
|
||||||
|
@ -44,242 +57,135 @@ public class SparkCountryPropagationJob2 {
|
||||||
.enableHiveSupport()
|
.enableHiveSupport()
|
||||||
.getOrCreate();
|
.getOrCreate();
|
||||||
|
|
||||||
|
final boolean writeUpdates = TRUE.equals(parser.get("writeUpdate"));
|
||||||
final String inputPath = parser.get("sourcePath");
|
final boolean saveGraph = TRUE.equals(parser.get("saveGraph"));
|
||||||
final String outputPath = "/tmp/provision/propagation/countrytoresultfrominstitutionalrepositories";
|
|
||||||
|
|
||||||
// createOutputDirs(outputPath, FileSystem.get(spark.sparkContext().hadoopConfiguration()));
|
|
||||||
|
|
||||||
boolean writeUpdates = TRUE.equals(parser.get("writeUpdate"));
|
|
||||||
boolean saveGraph = TRUE.equals(parser.get("saveGraph"));
|
|
||||||
|
|
||||||
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
||||||
|
|
||||||
|
//Load parque file with preprocessed association datasource - country
|
||||||
|
Dataset<DatasourceCountry> datasourcecountryassoc = readAssocDatasourceCountry(spark, datasourcecountrypath);
|
||||||
|
//broadcasting the result of the preparation step
|
||||||
|
Broadcast<Dataset<DatasourceCountry>> broadcast_datasourcecountryassoc = sc.broadcast(datasourcecountryassoc);
|
||||||
|
|
||||||
Dataset<eu.dnetlib.dhp.schema.oaf.Dataset> dataset = spark.createDataset(sc.textFile(inputPath + "/dataset")
|
Dataset<Row> potentialUpdates = getPotentialResultToUpdate(spark, inputPath, resultClazz, broadcast_datasourcecountryassoc);
|
||||||
.map(item -> new ObjectMapper().readValue(item, eu.dnetlib.dhp.schema.oaf.Dataset.class)).rdd(),
|
|
||||||
Encoders.bean(eu.dnetlib.dhp.schema.oaf.Dataset.class));
|
|
||||||
|
|
||||||
Dataset<OtherResearchProduct> other = spark.createDataset(sc.textFile(inputPath + "/otherresearchproduct")
|
|
||||||
.map(item -> new ObjectMapper().readValue(item, OtherResearchProduct.class)).rdd(),
|
|
||||||
Encoders.bean(OtherResearchProduct.class));
|
|
||||||
|
|
||||||
Dataset<Software> software = spark.createDataset(sc.textFile(inputPath + "/software")
|
|
||||||
.map(item -> new ObjectMapper().readValue(item, Software.class)).rdd(),
|
|
||||||
Encoders.bean(Software.class));
|
|
||||||
|
|
||||||
Dataset<Publication> publication = spark.createDataset(sc.textFile(inputPath + "/publication")
|
|
||||||
.map(item -> new ObjectMapper().readValue(item, Publication.class)).rdd(),
|
|
||||||
Encoders.bean(Publication.class));
|
|
||||||
|
|
||||||
|
|
||||||
//todo broadcast
|
|
||||||
|
|
||||||
|
|
||||||
software.createOrReplaceTempView("software");
|
|
||||||
final JavaRDD<Row> toupdateresultsoftware = propagateOnResult(spark, "software");
|
|
||||||
|
|
||||||
dataset.createOrReplaceTempView("dataset");
|
|
||||||
final JavaRDD<Row> toupdateresultdataset = propagateOnResult(spark, "dataset");
|
|
||||||
|
|
||||||
other.createOrReplaceTempView("other");
|
|
||||||
final JavaRDD<Row> toupdateresultother = propagateOnResult(spark, "other");
|
|
||||||
|
|
||||||
publication.createOrReplaceTempView("publication");
|
|
||||||
final JavaRDD<Row> toupdateresultpublication = propagateOnResult(spark, "publication");
|
|
||||||
|
|
||||||
if(writeUpdates){
|
if(writeUpdates){
|
||||||
writeUpdates(toupdateresultsoftware, toupdateresultdataset, toupdateresultother, toupdateresultpublication, outputPath);
|
writeUpdates(potentialUpdates.toJavaRDD(), outputPath + "/update_" + resultType);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(saveGraph){
|
if(saveGraph){
|
||||||
createUpdateForSoftwareDataset(toupdateresultsoftware, inputPath, spark)
|
updateResultTable(spark, potentialUpdates, inputPath, resultClazz, outputPath + "/" + resultType);
|
||||||
.map(s -> new ObjectMapper().writeValueAsString(s))
|
|
||||||
.saveAsTextFile(outputPath + "/software");
|
|
||||||
|
|
||||||
createUpdateForDatasetDataset(toupdateresultdataset,inputPath,spark)
|
|
||||||
.map(d -> new ObjectMapper().writeValueAsString(d))
|
|
||||||
.saveAsTextFile(outputPath + "/dataset");
|
|
||||||
|
|
||||||
createUpdateForOtherDataset(toupdateresultother, inputPath, spark)
|
|
||||||
.map(o -> new ObjectMapper().writeValueAsString(o))
|
|
||||||
.saveAsTextFile(outputPath + "/otherresearchproduct");
|
|
||||||
|
|
||||||
createUpdateForPublicationDataset(toupdateresultpublication, inputPath, spark)
|
|
||||||
.map(p -> new ObjectMapper().writeValueAsString(p))
|
|
||||||
.saveAsTextFile(outputPath + "/publication");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeUpdates(JavaRDD<Row> software, JavaRDD<Row> dataset, JavaRDD<Row> other , JavaRDD<Row> publication, String outputPath){
|
private static <R extends Result> void updateResultTable(SparkSession spark, Dataset<Row> potentialUpdates,
|
||||||
createUpdateForResultDatasetWrite(software, outputPath, "update_software");
|
String inputPath,
|
||||||
createUpdateForResultDatasetWrite(dataset, outputPath, "update_dataset");
|
Class<R> resultClazz,
|
||||||
createUpdateForResultDatasetWrite(other, outputPath, "update_other");
|
String outputPath) {
|
||||||
createUpdateForResultDatasetWrite(publication, outputPath, "update_publication");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static JavaRDD<OtherResearchProduct> createUpdateForOtherDataset(JavaRDD<Row> toupdateresult, String inputPath, SparkSession spark) {
|
Dataset<R> result = readPathEntity(spark, inputPath, resultClazz);
|
||||||
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
|
||||||
|
|
||||||
return sc.textFile(inputPath + "/otherresearchproduct")
|
Dataset<Tuple2<String, R>> result_pair = result
|
||||||
.map(item -> new ObjectMapper().readValue(item, OtherResearchProduct.class))
|
.map(r -> new Tuple2<>(r.getId(), r),
|
||||||
.mapToPair(s -> new Tuple2<>(s.getId(), s)).leftOuterJoin(getStringResultJavaPairRDD(toupdateresult))
|
Encoders.tuple(Encoders.STRING(), Encoders.bean(resultClazz)));
|
||||||
.map(c -> {
|
|
||||||
OtherResearchProduct oaf = c._2()._1();
|
Dataset<Tuple2<String, List>> potential_update_pair = potentialUpdates.map(pu -> new Tuple2<>(pu.getString(0), pu.getList(1)),
|
||||||
List<Country> countryList = oaf.getCountry();
|
Encoders.tuple(Encoders.STRING(), Encoders.bean(List.class)));
|
||||||
if (c._2()._2().isPresent()) {
|
|
||||||
|
Dataset<R> new_table = result_pair
|
||||||
|
.joinWith(potential_update_pair, result_pair.col("_1").equalTo(potential_update_pair.col("_1")), "left")
|
||||||
|
.map((MapFunction<Tuple2<Tuple2<String, R>, Tuple2<String, List>>, R>) value -> {
|
||||||
|
R r = value._1()._2();
|
||||||
|
Optional<List<Object>> potentialNewCountries = Optional.ofNullable(value._2()).map(Tuple2::_2);
|
||||||
|
if (potentialNewCountries != null) {
|
||||||
HashSet<String> countries = new HashSet<>();
|
HashSet<String> countries = new HashSet<>();
|
||||||
for (Qualifier country : countryList) {
|
for (Qualifier country : r.getCountry()) {
|
||||||
countries.add(country.getClassid());
|
countries.add(country.getClassid());
|
||||||
}
|
}
|
||||||
Result r = c._2()._2().get();
|
|
||||||
for (Country country : r.getCountry()) {
|
for (Object country : potentialNewCountries.get()) {
|
||||||
if (!countries.contains(country.getClassid())) {
|
if (!countries.contains(country)) {
|
||||||
countryList.add(country);
|
r.getCountry().add(getCountry((String) country));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
oaf.setCountry(countryList);
|
|
||||||
}
|
}
|
||||||
return oaf;
|
return r;
|
||||||
});
|
|
||||||
|
}, Encoders.bean(resultClazz));
|
||||||
|
|
||||||
|
|
||||||
|
log.info("Saving graph table to path: {}", outputPath);
|
||||||
|
result
|
||||||
|
.toJSON()
|
||||||
|
.write()
|
||||||
|
.option("compression", "gzip")
|
||||||
|
.text(outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static <R extends Result> Dataset<Row> getPotentialResultToUpdate(SparkSession spark, String inputPath,
|
||||||
|
Class<R> resultClazz,
|
||||||
|
Broadcast<Dataset<DatasourceCountry>> broadcast_datasourcecountryassoc) {
|
||||||
|
|
||||||
|
Dataset<R> result = readPathEntity(spark, inputPath, resultClazz);
|
||||||
|
result.createOrReplaceTempView("result");
|
||||||
|
createCfHbforresult(spark);
|
||||||
|
return countryPropagationAssoc(spark, broadcast_datasourcecountryassoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JavaRDD<Publication> createUpdateForPublicationDataset(JavaRDD<Row> toupdateresult, String inputPath, SparkSession spark) {
|
|
||||||
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
|
||||||
|
|
||||||
return sc.textFile(inputPath + "/publication")
|
private static void createCfHbforresult(SparkSession spark) {
|
||||||
.map(item -> new ObjectMapper().readValue(item, Publication.class))
|
|
||||||
.mapToPair(s -> new Tuple2<>(s.getId(), s)).leftOuterJoin(getStringResultJavaPairRDD(toupdateresult))
|
|
||||||
.map(c -> {
|
|
||||||
Publication oaf = c._2()._1();
|
|
||||||
List<Country> countryList = oaf.getCountry();
|
|
||||||
if (c._2()._2().isPresent()) {
|
|
||||||
HashSet<String> countries = new HashSet<>();
|
|
||||||
for (Qualifier country : countryList) {
|
|
||||||
countries.add(country.getClassid());
|
|
||||||
}
|
|
||||||
Result r = c._2()._2().get();
|
|
||||||
for (Country country : r.getCountry()) {
|
|
||||||
if (!countries.contains(country.getClassid())) {
|
|
||||||
countryList.add(country);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
oaf.setCountry(countryList);
|
|
||||||
}
|
|
||||||
return oaf;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static JavaRDD<Software> createUpdateForSoftwareDataset(JavaRDD<Row> toupdateresult, String inputPath, SparkSession spark) {
|
|
||||||
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
|
||||||
|
|
||||||
return sc.textFile(inputPath + "/software")
|
|
||||||
.map(item -> new ObjectMapper().readValue(item, Software.class))
|
|
||||||
.mapToPair(s -> new Tuple2<>(s.getId(), s)).leftOuterJoin(getStringResultJavaPairRDD(toupdateresult))
|
|
||||||
.map(c -> {
|
|
||||||
Software oaf = c._2()._1();
|
|
||||||
List<Country> countryList = oaf.getCountry();
|
|
||||||
if (c._2()._2().isPresent()) {
|
|
||||||
HashSet<String> countries = new HashSet<>();
|
|
||||||
for (Qualifier country : countryList) {
|
|
||||||
countries.add(country.getClassid());
|
|
||||||
}
|
|
||||||
Result r = c._2()._2().get();
|
|
||||||
for (Country country : r.getCountry()) {
|
|
||||||
if (!countries.contains(country.getClassid())) {
|
|
||||||
countryList.add(country);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
oaf.setCountry(countryList);
|
|
||||||
}
|
|
||||||
return oaf;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static JavaRDD<eu.dnetlib.dhp.schema.oaf.Dataset> createUpdateForDatasetDataset(JavaRDD<Row> toupdateresult, String inputPath, SparkSession spark) {
|
|
||||||
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
|
||||||
|
|
||||||
return sc.textFile(inputPath + "/dataset")
|
|
||||||
.map(item -> new ObjectMapper().readValue(item, eu.dnetlib.dhp.schema.oaf.Dataset.class))
|
|
||||||
.mapToPair(d -> new Tuple2<>(d.getId(), d)).leftOuterJoin(getStringResultJavaPairRDD(toupdateresult))
|
|
||||||
.map(c -> {
|
|
||||||
eu.dnetlib.dhp.schema.oaf.Dataset oaf = c._2()._1();
|
|
||||||
List<Country> countryList = oaf.getCountry();
|
|
||||||
if (c._2()._2().isPresent()) {
|
|
||||||
HashSet<String> countries = new HashSet<>();
|
|
||||||
for (Qualifier country : countryList) {
|
|
||||||
countries.add(country.getClassid());
|
|
||||||
}
|
|
||||||
Result r = c._2()._2().get();
|
|
||||||
for (Country country : r.getCountry()) {
|
|
||||||
if (!countries.contains(country.getClassid())) {
|
|
||||||
countryList.add(country);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
oaf.setCountry(countryList);
|
|
||||||
}
|
|
||||||
return oaf;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static JavaRDD<Row> propagateOnResult(SparkSession spark, String result_type) {
|
|
||||||
String query;
|
String query;
|
||||||
query = "SELECT id, inst.collectedfrom.key cf , inst.hostedby.key hb " +
|
query = "SELECT id, inst.collectedfrom.key cf , inst.hostedby.key hb " +
|
||||||
"FROM ( SELECT id, instance " +
|
"FROM ( SELECT id, instance " +
|
||||||
"FROM " + result_type +
|
"FROM result " +
|
||||||
" WHERE datainfo.deletedbyinference = false) ds " +
|
" WHERE datainfo.deletedbyinference = false) ds " +
|
||||||
"LATERAL VIEW EXPLODE(instance) i AS inst";
|
"LATERAL VIEW EXPLODE(instance) i AS inst";
|
||||||
Dataset<Row> cfhb = spark.sql(query);
|
Dataset<Row> cfhb = spark.sql(query);
|
||||||
cfhb.createOrReplaceTempView("cfhb");
|
cfhb.createOrReplaceTempView("cfhb");
|
||||||
|
|
||||||
return countryPropagationAssoc(spark, "cfhb").toJavaRDD();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dataset<Row> countryPropagationAssoc(SparkSession spark, String cfhbTable){
|
|
||||||
|
private static Dataset<Row> countryPropagationAssoc(SparkSession spark,
|
||||||
|
Broadcast<Dataset<DatasourceCountry>> broadcast_datasourcecountryassoc){
|
||||||
|
Dataset<DatasourceCountry> datasource_country = broadcast_datasourcecountryassoc.value();
|
||||||
|
datasource_country.createOrReplaceTempView("datasource_country");
|
||||||
|
|
||||||
String query = "SELECT id, collect_set(country) country "+
|
String query = "SELECT id, collect_set(country) country "+
|
||||||
"FROM ( SELECT id, country " +
|
"FROM ( SELECT id, country " +
|
||||||
"FROM rels " +
|
"FROM rels " +
|
||||||
"JOIN " + cfhbTable +
|
"JOIN cfhb " +
|
||||||
" ON cf = ds " +
|
" ON cf = ds " +
|
||||||
"UNION ALL " +
|
"UNION ALL " +
|
||||||
"SELECT id , country " +
|
"SELECT id , country " +
|
||||||
"FROM rels " +
|
"FROM rels " +
|
||||||
"JOIN " + cfhbTable +
|
"JOIN cfhb " +
|
||||||
" ON hb = ds ) tmp " +
|
" ON hb = ds ) tmp " +
|
||||||
"GROUP BY id";
|
"GROUP BY id";
|
||||||
return spark.sql(query);
|
return spark.sql(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JavaPairRDD<String, Result> getStringResultJavaPairRDD(JavaRDD<Row> toupdateresult) {
|
private static <R extends Result> Dataset<R> readPathEntity(SparkSession spark, String inputEntityPath, Class<R> resultClazz) {
|
||||||
return toupdateresult.map(c -> {
|
|
||||||
List<Country> countryList = new ArrayList<>();
|
log.info("Reading Graph table from: {}", inputEntityPath);
|
||||||
List<String> tmp = c.getList(1);
|
return spark
|
||||||
for (String country : tmp) {
|
.read()
|
||||||
countryList.add(getCountry(country));
|
.textFile(inputEntityPath)
|
||||||
}
|
.map((MapFunction<String, R>) value -> OBJECT_MAPPER.readValue(value, resultClazz), Encoders.bean(resultClazz));
|
||||||
Result r = new Result();
|
|
||||||
r.setId(c.getString(0));
|
|
||||||
r.setCountry(countryList);
|
|
||||||
return r;
|
|
||||||
}).mapToPair(r -> new Tuple2<>(r.getId(), r));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createUpdateForResultDatasetWrite(JavaRDD<Row> toupdateresult, String outputPath, String type){
|
private static Dataset<DatasourceCountry> readAssocDatasourceCountry(SparkSession spark, String relationPath) {
|
||||||
toupdateresult.map(c -> {
|
return spark.read()
|
||||||
List<Country> countryList = new ArrayList<>();
|
.load(relationPath)
|
||||||
List<String> tmp = c.getList(1);
|
.as(Encoders.bean(DatasourceCountry.class));
|
||||||
for (String country : tmp) {
|
}
|
||||||
countryList.add(getCountry(country));
|
|
||||||
}
|
|
||||||
Result r = new Result();
|
|
||||||
r.setId(c.getString(0));
|
|
||||||
r.setCountry(countryList);
|
|
||||||
return r;
|
|
||||||
|
|
||||||
}).map(r ->new ObjectMapper().writeValueAsString(r))
|
private static void writeUpdates(JavaRDD<Row> potentialUpdates, String outputPath){
|
||||||
.saveAsTextFile(outputPath+"/"+type);
|
potentialUpdates.map(u -> OBJECT_MAPPER.writeValueAsString(u))
|
||||||
|
.saveAsTextFile(outputPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue