2020-04-30 11:05:17 +02:00
|
|
|
|
2020-04-16 18:40:14 +02:00
|
|
|
package eu.dnetlib.dhp.resulttocommunityfromorganization;
|
|
|
|
|
2020-04-23 12:57:35 +02:00
|
|
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkHiveSession;
|
|
|
|
|
|
|
|
import java.util.*;
|
2020-04-30 11:05:17 +02:00
|
|
|
|
2020-04-16 18:43:55 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
2020-05-07 18:22:26 +02:00
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
2020-04-16 18:43:55 +02:00
|
|
|
import org.apache.spark.sql.*;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2020-04-30 11:05:17 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2020-05-15 10:21:09 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
2020-04-30 11:05:17 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
|
|
|
|
2020-04-16 18:43:55 +02:00
|
|
|
public class PrepareResultCommunitySet {
|
|
|
|
|
2020-04-30 11:05:17 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(PrepareResultCommunitySet.class);
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
String jsonConfiguration = IOUtils
|
|
|
|
.toString(
|
|
|
|
PrepareResultCommunitySet.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/resulttocommunityfromorganization/input_preparecommunitytoresult_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);
|
|
|
|
|
|
|
|
final String outputPath = parser.get("outputPath");
|
|
|
|
log.info("outputPath: {}", outputPath);
|
|
|
|
|
|
|
|
final OrganizationMap organizationMap = new Gson()
|
|
|
|
.fromJson(
|
|
|
|
parser.get("organizationtoresultcommunitymap"),
|
|
|
|
OrganizationMap.class);
|
|
|
|
log.info("organizationMap: {}", new Gson().toJson(organizationMap));
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.set("hive.metastore.uris", parser.get("hive_metastore_uris"));
|
|
|
|
|
|
|
|
runWithSparkHiveSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
|
|
|
spark -> {
|
2020-05-14 10:25:41 +02:00
|
|
|
removeOutputDir(spark, outputPath);
|
2020-04-30 11:05:17 +02:00
|
|
|
prepareInfo(spark, inputPath, outputPath, organizationMap);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void prepareInfo(
|
|
|
|
SparkSession spark,
|
|
|
|
String inputPath,
|
|
|
|
String outputPath,
|
|
|
|
OrganizationMap organizationMap) {
|
2020-05-07 18:22:26 +02:00
|
|
|
|
|
|
|
Dataset<Relation> relation = readPath(spark, inputPath, Relation.class);
|
2020-04-30 11:05:17 +02:00
|
|
|
relation.createOrReplaceTempView("relation");
|
|
|
|
|
|
|
|
String query = "SELECT result_organization.source resultId, result_organization.target orgId, org_set merges "
|
|
|
|
+ "FROM (SELECT source, target "
|
|
|
|
+ " FROM relation "
|
|
|
|
+ " WHERE datainfo.deletedbyinference = false "
|
|
|
|
+ " AND relClass = '"
|
2020-05-14 18:29:24 +02:00
|
|
|
+ ModelConstants.HAS_AUTHOR_INSTITUTION
|
2020-04-30 11:05:17 +02:00
|
|
|
+ "') result_organization "
|
|
|
|
+ "LEFT JOIN (SELECT source, collect_set(target) org_set "
|
|
|
|
+ " FROM relation "
|
|
|
|
+ " WHERE datainfo.deletedbyinference = false "
|
|
|
|
+ " AND relClass = '"
|
2020-05-14 18:29:24 +02:00
|
|
|
+ ModelConstants.MERGES
|
2020-04-30 11:05:17 +02:00
|
|
|
+ "' "
|
|
|
|
+ " GROUP BY source) organization_organization "
|
|
|
|
+ "ON result_organization.target = organization_organization.source ";
|
|
|
|
|
2020-05-07 18:22:26 +02:00
|
|
|
Dataset<ResultOrganizations> result_organizationset = spark
|
2020-04-30 11:05:17 +02:00
|
|
|
.sql(query)
|
|
|
|
.as(Encoders.bean(ResultOrganizations.class));
|
|
|
|
|
|
|
|
result_organizationset
|
2020-05-07 18:22:26 +02:00
|
|
|
.map(mapResultCommunityFn(organizationMap), Encoders.bean(ResultCommunityList.class))
|
|
|
|
.filter(Objects::nonNull)
|
2020-04-30 11:05:17 +02:00
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.option("compression", "gzip")
|
2020-05-07 18:22:26 +02:00
|
|
|
.json(outputPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static MapFunction<ResultOrganizations, ResultCommunityList> mapResultCommunityFn(
|
|
|
|
OrganizationMap organizationMap) {
|
|
|
|
return (MapFunction<ResultOrganizations, ResultCommunityList>) value -> {
|
|
|
|
String rId = value.getResultId();
|
|
|
|
Optional<List<String>> orgs = Optional.ofNullable(value.getMerges());
|
|
|
|
String oTarget = value.getOrgId();
|
|
|
|
Set<String> communitySet = new HashSet<>();
|
|
|
|
if (organizationMap.containsKey(oTarget)) {
|
|
|
|
communitySet.addAll(organizationMap.get(oTarget));
|
|
|
|
}
|
|
|
|
if (orgs.isPresent())
|
|
|
|
for (String oId : orgs.get()) {
|
|
|
|
if (organizationMap.containsKey(oId)) {
|
|
|
|
communitySet.addAll(organizationMap.get(oId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (communitySet.size() > 0) {
|
|
|
|
ResultCommunityList rcl = new ResultCommunityList();
|
|
|
|
rcl.setResultId(rId);
|
|
|
|
ArrayList<String> communityList = new ArrayList<>();
|
|
|
|
communityList.addAll(communitySet);
|
|
|
|
rcl.setCommunityList(communityList);
|
|
|
|
return rcl;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2020-04-30 11:05:17 +02:00
|
|
|
}
|
2020-04-16 18:40:14 +02:00
|
|
|
}
|