2020-05-07 09:59:58 +02:00
|
|
|
|
2020-05-05 17:03:43 +02:00
|
|
|
package eu.dnetlib.dhp.countrypropagation;
|
|
|
|
|
2020-05-07 09:59:58 +02:00
|
|
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkHiveSession;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
import org.apache.spark.sql.*;
|
|
|
|
import org.apache.spark.sql.Dataset;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
|
|
|
|
2020-05-05 17:03:43 +02:00
|
|
|
public class PrepareResultCountrySet {
|
2020-05-07 09:59:58 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(PrepareResultCountrySet.class);
|
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
private static final String RESULT_COUNTRYSET_QUERY = "SELECT id resultId, collect_set(country) countrySet "
|
|
|
|
+ "FROM ( SELECT id, country "
|
|
|
|
+ "FROM datasource_country JOIN cfhb ON cf = dataSourceId "
|
|
|
|
+ "UNION ALL "
|
|
|
|
+ "SELECT id, country FROM datasource_country "
|
|
|
|
+ "JOIN cfhb ON hb = dataSourceId ) tmp "
|
|
|
|
+ "GROUP BY id";
|
2020-05-07 09:59:58 +02:00
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
String jsonConfiguration = IOUtils
|
|
|
|
.toString(
|
2020-05-07 18:22:26 +02:00
|
|
|
PrepareResultCountrySet.class
|
2020-05-07 09:59:58 +02:00
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/countrypropagation/input_prepareresultcountry_parameters.json"));
|
|
|
|
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
|
|
|
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
Boolean isSparkSessionManaged = isSparkSessionManaged(parser);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
String inputPath = parser.get("sourcePath");
|
|
|
|
log.info("inputPath: {}", inputPath);
|
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
String outputPath = parser.get("outputPath");
|
|
|
|
log.info("outputPath: {}", outputPath);
|
|
|
|
|
2020-05-07 09:59:58 +02:00
|
|
|
final String datasourcecountrypath = parser.get("preparedInfoPath");
|
|
|
|
log.info("preparedInfoPath: {}", datasourcecountrypath);
|
|
|
|
|
|
|
|
final String resultClassName = parser.get("resultTableName");
|
|
|
|
log.info("resultTableName: {}", resultClassName);
|
|
|
|
|
|
|
|
Class<? extends Result> resultClazz = (Class<? extends Result>) Class.forName(resultClassName);
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.set("hive.metastore.uris", parser.get("hive_metastore_uris"));
|
|
|
|
|
|
|
|
runWithSparkHiveSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
|
|
|
spark -> {
|
|
|
|
getPotentialResultToUpdate(
|
|
|
|
spark,
|
|
|
|
inputPath,
|
2020-05-07 18:22:26 +02:00
|
|
|
outputPath,
|
2020-05-07 09:59:58 +02:00
|
|
|
datasourcecountrypath,
|
|
|
|
resultClazz);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static <R extends Result> void getPotentialResultToUpdate(
|
|
|
|
SparkSession spark,
|
|
|
|
String inputPath,
|
2020-05-07 18:22:26 +02:00
|
|
|
String outputPath,
|
2020-05-07 09:59:58 +02:00
|
|
|
String datasourcecountrypath,
|
|
|
|
Class<R> resultClazz) {
|
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
Dataset<R> result = readPath(spark, inputPath, resultClazz);
|
2020-05-07 09:59:58 +02:00
|
|
|
result.createOrReplaceTempView("result");
|
|
|
|
// log.info("number of results: {}", result.count());
|
2020-05-07 18:22:26 +02:00
|
|
|
createCfHbforResult(spark);
|
2020-05-07 09:59:58 +02:00
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
Dataset<DatasourceCountry> datasource_country = readPath(spark, datasourcecountrypath, DatasourceCountry.class);
|
2020-05-07 09:59:58 +02:00
|
|
|
|
|
|
|
datasource_country.createOrReplaceTempView("datasource_country");
|
2020-05-07 18:22:26 +02:00
|
|
|
// log.info("datasource_country number : {}", datasource_country.count());
|
2020-05-07 09:59:58 +02:00
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
spark
|
|
|
|
.sql(RESULT_COUNTRYSET_QUERY)
|
|
|
|
.as(Encoders.bean(ResultCountrySet.class))
|
|
|
|
.write()
|
|
|
|
.option("compression", "gzip")
|
|
|
|
.mode(SaveMode.Append)
|
|
|
|
.json(outputPath);
|
2020-05-07 09:59:58 +02:00
|
|
|
}
|
2020-05-07 18:22:26 +02:00
|
|
|
|
2020-05-05 17:03:43 +02:00
|
|
|
}
|