forked from D-Net/dnet-hadoop
new parametric implementation of country propagation. Exploits information compute before and broadcasts it to each executor
This commit is contained in:
parent
a2d833d5dd
commit
aef9b3aa90
|
@ -4,6 +4,9 @@ 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.hadoop.io.compress.CompressionCodec;
|
||||||
|
import org.apache.hadoop.io.compress.GzipCodec;
|
||||||
import org.apache.spark.SparkConf;
|
import org.apache.spark.SparkConf;
|
||||||
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;
|
||||||
|
@ -18,6 +21,9 @@ import java.util.*;
|
||||||
import static eu.dnetlib.dhp.PropagationConstant.*;
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkHiveSession;
|
||||||
|
|
||||||
public class SparkCountryPropagationJob2 {
|
public class SparkCountryPropagationJob2 {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(SparkCountryPropagationJob2.class);
|
private static final Logger log = LoggerFactory.getLogger(SparkCountryPropagationJob2.class);
|
||||||
|
@ -35,30 +41,54 @@ public class SparkCountryPropagationJob2 {
|
||||||
|
|
||||||
parser.parseArgument(args);
|
parser.parseArgument(args);
|
||||||
|
|
||||||
|
Boolean isSparkSessionManaged = isSparkSessionManaged(parser);
|
||||||
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
||||||
|
|
||||||
String inputPath = parser.get("sourcePath");
|
String inputPath = parser.get("sourcePath");
|
||||||
log.info("inputPath: {}", inputPath);
|
log.info("inputPath: {}", inputPath);
|
||||||
|
|
||||||
final String outputPath = "/tmp/provision/propagation/countrytoresultfrominstitutionalrepositories";
|
final String outputPath = parser.get("outputPath");
|
||||||
final String datasourcecountrypath = outputPath + "/prepared_datasource_country";
|
log.info("outputPath: {}", outputPath);
|
||||||
final String resultClassName = parser.get("resultTableName");
|
|
||||||
|
|
||||||
final String resultType = resultClassName.substring(resultClassName.lastIndexOf(".")+1);
|
final String datasourcecountrypath = parser.get("preparedInfoPath");
|
||||||
|
log.info("preparedInfoPath: {}", datasourcecountrypath);
|
||||||
|
|
||||||
|
final String resultClassName = parser.get("resultTableName");
|
||||||
|
log.info("resultTableName: {}", resultClassName);
|
||||||
|
|
||||||
|
final String resultType = resultClassName.substring(resultClassName.lastIndexOf(".") + 1).toLowerCase();
|
||||||
|
log.info("resultType: {}", resultType);
|
||||||
|
|
||||||
|
final Boolean writeUpdates = Optional
|
||||||
|
.ofNullable(parser.get("writeUpdate"))
|
||||||
|
.map(Boolean::valueOf)
|
||||||
|
.orElse(Boolean.TRUE);
|
||||||
|
log.info("writeUpdate: {}", writeUpdates);
|
||||||
|
|
||||||
|
final Boolean saveGraph = Optional
|
||||||
|
.ofNullable(parser.get("saveGraph"))
|
||||||
|
.map(Boolean::valueOf)
|
||||||
|
.orElse(Boolean.TRUE);
|
||||||
|
log.info("saveGraph: {}", saveGraph);
|
||||||
|
|
||||||
Class<? extends Result> resultClazz = (Class<? extends Result>) Class.forName(resultClassName);
|
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
|
|
||||||
.builder()
|
|
||||||
.appName(SparkCountryPropagationJob2.class.getSimpleName())
|
|
||||||
.master(parser.get("master"))
|
|
||||||
.config(conf)
|
|
||||||
.enableHiveSupport()
|
|
||||||
.getOrCreate();
|
|
||||||
|
|
||||||
final boolean writeUpdates = TRUE.equals(parser.get("writeUpdate"));
|
runWithSparkHiveSession(conf, isSparkSessionManaged,
|
||||||
final boolean saveGraph = TRUE.equals(parser.get("saveGraph"));
|
spark -> {
|
||||||
|
//createOutputDirs(outputPath, FileSystem.get(spark.sparkContext().hadoopConfiguration()));
|
||||||
|
removeOutputDir(spark, outputPath);
|
||||||
|
execPropagation(spark, datasourcecountrypath, inputPath, outputPath, resultClazz, resultType,
|
||||||
|
writeUpdates, saveGraph);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <R extends Result> void execPropagation(SparkSession spark, String datasourcecountrypath,
|
||||||
|
String inputPath, String outputPath, Class<R> resultClazz, String resultType,
|
||||||
|
boolean writeUpdates, boolean saveGraph){
|
||||||
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
||||||
|
|
||||||
//Load parque file with preprocessed association datasource - country
|
//Load parque file with preprocessed association datasource - country
|
||||||
|
@ -74,10 +104,9 @@ public class SparkCountryPropagationJob2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(saveGraph){
|
if(saveGraph){
|
||||||
updateResultTable(spark, potentialUpdates, inputPath, resultClazz, outputPath + "/" + resultType);
|
updateResultTable(spark, potentialUpdates, inputPath, resultClazz, outputPath);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <R extends Result> void updateResultTable(SparkSession spark, Dataset<ResultCountrySet> potentialUpdates,
|
private static <R extends Result> void updateResultTable(SparkSession spark, Dataset<ResultCountrySet> potentialUpdates,
|
||||||
|
@ -96,33 +125,32 @@ public class SparkCountryPropagationJob2 {
|
||||||
Encoders.tuple(Encoders.STRING(), Encoders.bean(ResultCountrySet.class)));
|
Encoders.tuple(Encoders.STRING(), Encoders.bean(ResultCountrySet.class)));
|
||||||
|
|
||||||
Dataset<R> new_table = result_pair
|
Dataset<R> new_table = result_pair
|
||||||
.joinWith(potential_update_pair, result_pair.col("_1").equalTo(potential_update_pair.col("_1")), "left")
|
.joinWith(potentialUpdates, result_pair.col("_1").equalTo(potentialUpdates.col("resultId")), "left_outer")
|
||||||
.map((MapFunction<Tuple2<Tuple2<String, R>, Tuple2<String, ResultCountrySet>>, R>) value -> {
|
.map((MapFunction<Tuple2<Tuple2<String, R>, ResultCountrySet>, R>) value -> {
|
||||||
R r = value._1()._2();
|
R r = value._1()._2();
|
||||||
Optional<ResultCountrySet> potentialNewCountries = Optional.ofNullable(value._2()).map(Tuple2::_2);
|
Optional<ResultCountrySet> potentialNewCountries = Optional.ofNullable(value._2());
|
||||||
if (potentialNewCountries.isPresent()) {
|
if (potentialNewCountries.isPresent()) {
|
||||||
HashSet<String> countries = new HashSet<>();
|
HashSet<String> countries = new HashSet<>();
|
||||||
for (Qualifier country : r.getCountry()) {
|
for (Qualifier country : r.getCountry()) {
|
||||||
countries.add(country.getClassid());
|
countries.add(country.getClassid());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Country country : potentialNewCountries.get().getCountrySet()) {
|
for (CountrySbs country : potentialNewCountries.get().getCountrySet()) {
|
||||||
if (!countries.contains(country.getClassid())) {
|
if (!countries.contains(country.getClassid())) {
|
||||||
r.getCountry().add(getCountry(country.getClassid(), country.getClassname()));
|
r.getCountry().add(getCountry(country.getClassid(), country.getClassname()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
}, Encoders.bean(resultClazz));
|
}, Encoders.bean(resultClazz));
|
||||||
|
|
||||||
|
|
||||||
log.info("Saving graph table to path: {}", outputPath);
|
log.info("Saving graph table to path: {}", outputPath);
|
||||||
|
//log.info("number of saved recordsa: {}", new_table.count());
|
||||||
new_table
|
new_table
|
||||||
.toJSON()
|
.toJavaRDD()
|
||||||
.write()
|
.map(r -> OBJECT_MAPPER.writeValueAsString(r))
|
||||||
.option("compression", "gzip")
|
.saveAsTextFile(outputPath , GzipCodec.class);
|
||||||
.text(outputPath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -133,6 +161,7 @@ public class SparkCountryPropagationJob2 {
|
||||||
|
|
||||||
Dataset<R> result = readPathEntity(spark, inputPath, resultClazz);
|
Dataset<R> result = readPathEntity(spark, inputPath, resultClazz);
|
||||||
result.createOrReplaceTempView("result");
|
result.createOrReplaceTempView("result");
|
||||||
|
//log.info("number of results: {}", result.count());
|
||||||
createCfHbforresult(spark);
|
createCfHbforresult(spark);
|
||||||
return countryPropagationAssoc(spark, broadcast_datasourcecountryassoc);
|
return countryPropagationAssoc(spark, broadcast_datasourcecountryassoc);
|
||||||
}
|
}
|
||||||
|
@ -147,6 +176,7 @@ public class SparkCountryPropagationJob2 {
|
||||||
"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");
|
||||||
|
log.info("cfhb_number : {}", cfhb.count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -155,19 +185,22 @@ public class SparkCountryPropagationJob2 {
|
||||||
|
|
||||||
Dataset<DatasourceCountry> datasource_country = broadcast_datasourcecountryassoc.value();
|
Dataset<DatasourceCountry> datasource_country = broadcast_datasourcecountryassoc.value();
|
||||||
datasource_country.createOrReplaceTempView("datasource_country");
|
datasource_country.createOrReplaceTempView("datasource_country");
|
||||||
|
log.info("datasource_country number : {}",datasource_country.count());
|
||||||
|
|
||||||
String query = "SELECT id, collect_set(country) country "+
|
String query = "SELECT id resultId, collect_set(country) countrySet "+
|
||||||
"FROM ( SELECT id, country " +
|
"FROM ( SELECT id, country " +
|
||||||
"FROM rels " +
|
"FROM datasource_country " +
|
||||||
"JOIN cfhb " +
|
"JOIN cfhb " +
|
||||||
" ON cf = ds " +
|
" ON cf = dataSourceId " +
|
||||||
"UNION ALL " +
|
"UNION ALL " +
|
||||||
"SELECT id , country " +
|
"SELECT id , country " +
|
||||||
"FROM rels " +
|
"FROM datasource_country " +
|
||||||
"JOIN cfhb " +
|
"JOIN cfhb " +
|
||||||
" ON hb = ds ) tmp " +
|
" ON hb = dataSourceId ) tmp " +
|
||||||
"GROUP BY id";
|
"GROUP BY id";
|
||||||
return spark.sql(query);
|
Dataset<Row> potentialUpdates = spark.sql(query);
|
||||||
|
log.info("potential update number : {}", potentialUpdates.count());
|
||||||
|
return potentialUpdates;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <R extends Result> Dataset<R> readPathEntity(SparkSession spark, String inputEntityPath, Class<R> resultClazz) {
|
private static <R extends Result> Dataset<R> readPathEntity(SparkSession spark, String inputEntityPath, Class<R> resultClazz) {
|
||||||
|
@ -180,14 +213,15 @@ public class SparkCountryPropagationJob2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dataset<DatasourceCountry> readAssocDatasourceCountry(SparkSession spark, String relationPath) {
|
private static Dataset<DatasourceCountry> readAssocDatasourceCountry(SparkSession spark, String relationPath) {
|
||||||
return spark.read()
|
return spark
|
||||||
.load(relationPath)
|
.read()
|
||||||
.as(Encoders.bean(DatasourceCountry.class));
|
.textFile(relationPath)
|
||||||
|
.map(value -> OBJECT_MAPPER.readValue(value, DatasourceCountry.class), Encoders.bean(DatasourceCountry.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeUpdates(JavaRDD<ResultCountrySet> potentialUpdates, String outputPath){
|
private static void writeUpdates(JavaRDD<ResultCountrySet> potentialUpdates, String outputPath){
|
||||||
potentialUpdates.map(u -> OBJECT_MAPPER.writeValueAsString(u))
|
potentialUpdates.map(u -> OBJECT_MAPPER.writeValueAsString(u))
|
||||||
.saveAsTextFile(outputPath);
|
.saveAsTextFile(outputPath, GzipCodec.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue