2020-04-08 13:04:04 +02:00
|
|
|
package eu.dnetlib.dhp.countrypropagation;
|
|
|
|
|
2020-04-23 11:58:26 +02:00
|
|
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkHiveSession;
|
|
|
|
|
2020-04-08 13:04:04 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
2020-04-23 11:58:26 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2020-04-08 13:04:04 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2020-04-11 16:36:03 +02:00
|
|
|
import org.apache.hadoop.io.compress.GzipCodec;
|
2020-04-08 13:04:04 +02:00
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
import org.apache.spark.sql.Dataset;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
2020-04-23 11:58:26 +02:00
|
|
|
* For the association of the country to the datasource The association is computed only for
|
|
|
|
* datasource of specific type or having whitelisted ids The country is registered in the
|
|
|
|
* Organization associated to the Datasource, so the relation provides between Datasource and
|
|
|
|
* Organization is exploited to get the country for the datasource
|
2020-04-08 13:04:04 +02:00
|
|
|
*/
|
2020-04-08 19:12:59 +02:00
|
|
|
public class PrepareDatasourceCountryAssociation {
|
2020-04-08 13:04:04 +02:00
|
|
|
|
2020-04-23 11:58:26 +02:00
|
|
|
private static final Logger log =
|
|
|
|
LoggerFactory.getLogger(PrepareDatasourceCountryAssociation.class);
|
2020-04-08 13:04:04 +02:00
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
2020-04-23 11:58:26 +02:00
|
|
|
String jsonConfiguration =
|
|
|
|
IOUtils.toString(
|
|
|
|
PrepareDatasourceCountryAssociation.class.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/countrypropagation/input_prepareassoc_parameters.json"));
|
2020-04-08 13:04:04 +02:00
|
|
|
|
2020-04-23 11:58:26 +02:00
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
2020-04-08 13:04:04 +02:00
|
|
|
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
2020-04-11 16:36:03 +02:00
|
|
|
Boolean isSparkSessionManaged = isSparkSessionManaged(parser);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
2020-04-08 13:04:04 +02:00
|
|
|
String inputPath = parser.get("sourcePath");
|
|
|
|
log.info("inputPath: {}", inputPath);
|
|
|
|
|
2020-04-11 16:36:03 +02:00
|
|
|
final String outputPath = parser.get("outputPath");
|
|
|
|
log.info("outputPath {}: ", outputPath);
|
|
|
|
|
2020-04-08 13:04:04 +02:00
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.set("hive.metastore.uris", parser.get("hive_metastore_uris"));
|
|
|
|
|
2020-04-23 11:58:26 +02:00
|
|
|
runWithSparkHiveSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
2020-04-11 16:36:03 +02:00
|
|
|
spark -> {
|
|
|
|
removeOutputDir(spark, outputPath);
|
2020-04-23 11:58:26 +02:00
|
|
|
prepareDatasourceCountryAssociation(
|
|
|
|
spark,
|
2020-04-11 16:36:03 +02:00
|
|
|
Arrays.asList(parser.get("whitelist").split(";")),
|
|
|
|
Arrays.asList(parser.get("allowedtypes").split(";")),
|
|
|
|
inputPath,
|
|
|
|
outputPath);
|
|
|
|
});
|
2020-04-08 18:08:41 +02:00
|
|
|
}
|
2020-04-08 13:04:04 +02:00
|
|
|
|
2020-04-23 11:58:26 +02:00
|
|
|
private static void prepareDatasourceCountryAssociation(
|
|
|
|
SparkSession spark,
|
|
|
|
List<String> whitelist,
|
|
|
|
List<String> allowedtypes,
|
|
|
|
String inputPath,
|
|
|
|
String outputPath) {
|
2020-04-08 13:04:04 +02:00
|
|
|
String whitelisted = "";
|
2020-04-23 11:58:26 +02:00
|
|
|
for (String i : whitelist) {
|
2020-04-08 13:04:04 +02:00
|
|
|
whitelisted += " OR id = '" + i + "'";
|
|
|
|
}
|
|
|
|
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
|
|
|
|
2020-04-23 11:58:26 +02:00
|
|
|
Dataset<Datasource> datasource =
|
|
|
|
spark.createDataset(
|
|
|
|
sc.textFile(inputPath + "/datasource")
|
|
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Datasource.class))
|
|
|
|
.rdd(),
|
|
|
|
Encoders.bean(Datasource.class));
|
|
|
|
|
|
|
|
Dataset<Relation> relation =
|
|
|
|
spark.createDataset(
|
|
|
|
sc.textFile(inputPath + "/relation")
|
|
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Relation.class))
|
|
|
|
.rdd(),
|
|
|
|
Encoders.bean(Relation.class));
|
|
|
|
|
|
|
|
Dataset<Organization> organization =
|
|
|
|
spark.createDataset(
|
|
|
|
sc.textFile(inputPath + "/organization")
|
|
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Organization.class))
|
|
|
|
.rdd(),
|
|
|
|
Encoders.bean(Organization.class));
|
2020-04-08 13:04:04 +02:00
|
|
|
|
|
|
|
datasource.createOrReplaceTempView("datasource");
|
|
|
|
relation.createOrReplaceTempView("relation");
|
|
|
|
organization.createOrReplaceTempView("organization");
|
|
|
|
|
2020-04-23 11:58:26 +02:00
|
|
|
String query =
|
|
|
|
"SELECT source dataSourceId, named_struct('classid', country.classid, 'classname', country.classname) country "
|
|
|
|
+ "FROM ( SELECT id "
|
|
|
|
+ " FROM datasource "
|
|
|
|
+ " WHERE (datainfo.deletedbyinference = false "
|
|
|
|
+ whitelisted
|
|
|
|
+ ") "
|
|
|
|
+ getConstraintList("datasourcetype.classid = '", allowedtypes)
|
|
|
|
+ ") d "
|
|
|
|
+ "JOIN ( SELECT source, target "
|
|
|
|
+ " FROM relation "
|
|
|
|
+ " WHERE relclass = '"
|
|
|
|
+ RELATION_DATASOURCE_ORGANIZATION_REL_CLASS
|
|
|
|
+ "' "
|
|
|
|
+ " AND datainfo.deletedbyinference = false ) rel "
|
|
|
|
+ "ON d.id = rel.source "
|
|
|
|
+ "JOIN (SELECT id, country "
|
|
|
|
+ " FROM organization "
|
|
|
|
+ " WHERE datainfo.deletedbyinference = false "
|
|
|
|
+ " AND length(country.classid)>0) o "
|
|
|
|
+ "ON o.id = rel.target";
|
2020-04-08 13:04:04 +02:00
|
|
|
|
|
|
|
spark.sql(query)
|
|
|
|
.as(Encoders.bean(DatasourceCountry.class))
|
2020-04-11 16:36:03 +02:00
|
|
|
.toJavaRDD()
|
|
|
|
.map(c -> OBJECT_MAPPER.writeValueAsString(c))
|
|
|
|
.saveAsTextFile(outputPath, GzipCodec.class);
|
2020-04-08 13:04:04 +02:00
|
|
|
}
|
|
|
|
}
|