2020-04-16 18:40:14 +02:00
|
|
|
package eu.dnetlib.dhp.resulttocommunityfromorganization;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
2020-04-16 18:45:22 +02:00
|
|
|
import org.apache.spark.sql.SaveMode;
|
2020-04-16 18:40:14 +02:00
|
|
|
import org.apache.spark.sql.SparkSession;
|
2020-04-16 18:45:22 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2020-04-16 18:40:14 +02:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import static eu.dnetlib.dhp.PropagationConstant.*;
|
2020-04-16 18:45:22 +02:00
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkHiveSession;
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
public class SparkResultToCommunityFromOrganizationJob2 {
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(SparkResultToCommunityFromOrganizationJob2.class);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
2020-04-16 18:40:14 +02:00
|
|
|
|
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
String jsonConfiguration = IOUtils.toString(SparkResultToCommunityFromOrganizationJob2.class
|
|
|
|
.getResourceAsStream("/eu/dnetlib/dhp/resulttocommunityfromorganization/input_communitytoresult_parameters.json"));
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
jsonConfiguration);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
parser.parseArgument(args);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
Boolean isSparkSessionManaged = isSparkSessionManaged(parser);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
String inputPath = parser.get("sourcePath");
|
|
|
|
log.info("inputPath: {}", inputPath);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
final String outputPath = parser.get("outputPath");
|
|
|
|
log.info("outputPath: {}", outputPath);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
final String possibleupdatespath = parser.get("preparedInfoPath");
|
|
|
|
log.info("preparedInfoPath: {}", possibleupdatespath);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
final String resultClassName = parser.get("resultTableName");
|
|
|
|
log.info("resultTableName: {}", resultClassName);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
final Boolean saveGraph = Optional
|
|
|
|
.ofNullable(parser.get("saveGraph"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
|
|
|
log.info("saveGraph: {}", saveGraph);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
Class<? extends Result> resultClazz = (Class<? extends Result>) Class.forName(resultClassName);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.set("hive.metastore.uris", parser.get("hive_metastore_uris"));
|
2020-04-16 18:40:14 +02:00
|
|
|
|
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
runWithSparkHiveSession(conf, isSparkSessionManaged,
|
|
|
|
spark -> {
|
|
|
|
if (isTest(parser)) {
|
|
|
|
removeOutputDir(spark, outputPath);
|
|
|
|
}
|
|
|
|
execPropagation(spark, inputPath, outputPath, resultClazz, possibleupdatespath);
|
|
|
|
});
|
2020-04-16 18:40:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
private static <R extends Result> void execPropagation(SparkSession spark, String inputPath, String outputPath,
|
|
|
|
Class<R> resultClazz, String possibleUpdatesPath) {
|
|
|
|
org.apache.spark.sql.Dataset<ResultCommunityList> possibleUpdates = readResultCommunityList(spark, possibleUpdatesPath);
|
|
|
|
org.apache.spark.sql.Dataset<R> result = readPathEntity(spark, inputPath, resultClazz);
|
|
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
.joinWith(possibleUpdates, result.col("id").equalTo(possibleUpdates.col("resultId")),
|
|
|
|
"left_outer")
|
|
|
|
.map(value -> {
|
|
|
|
R ret = value._1();
|
|
|
|
Optional<ResultCommunityList> rcl = Optional.ofNullable(value._2());
|
|
|
|
if(rcl.isPresent()){
|
|
|
|
ArrayList<String> communitySet = rcl.get().getCommunityList();
|
|
|
|
List<String> contextList = ret.getContext().stream().map(con -> con.getId()).collect(Collectors.toList());
|
2020-04-17 17:13:18 +02:00
|
|
|
Result res = new Result();
|
|
|
|
res.setId(ret.getId());
|
|
|
|
List<Context> propagatedContexts = new ArrayList<>();
|
2020-04-16 18:40:14 +02:00
|
|
|
for(String cId:communitySet){
|
|
|
|
if(!contextList.contains(cId)){
|
|
|
|
Context newContext = new Context();
|
|
|
|
newContext.setId(cId);
|
|
|
|
newContext.setDataInfo(Arrays.asList(getDataInfo(PROPAGATION_DATA_INFO_TYPE,
|
|
|
|
PROPAGATION_RESULT_COMMUNITY_ORGANIZATION_CLASS_ID, PROPAGATION_RESULT_COMMUNITY_ORGANIZATION_CLASS_NAME)));
|
2020-04-17 17:13:18 +02:00
|
|
|
propagatedContexts.add(newContext);
|
2020-04-16 18:40:14 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-17 17:13:18 +02:00
|
|
|
res.setContext(propagatedContexts);
|
|
|
|
ret.mergeFrom(res);
|
2020-04-16 18:40:14 +02:00
|
|
|
}
|
2020-04-16 18:45:22 +02:00
|
|
|
return ret;
|
|
|
|
}, Encoders.bean(resultClazz))
|
|
|
|
.toJSON()
|
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.option("compression","gzip")
|
|
|
|
.text(outputPath);
|
2020-04-16 18:40:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:45:22 +02:00
|
|
|
private static org.apache.spark.sql.Dataset<ResultCommunityList> readResultCommunityList(SparkSession spark, String possibleUpdatesPath) {
|
|
|
|
return spark
|
|
|
|
.read()
|
|
|
|
.textFile(possibleUpdatesPath)
|
|
|
|
.map(value -> OBJECT_MAPPER.readValue(value, ResultCommunityList.class), Encoders.bean(ResultCommunityList.class));
|
2020-04-16 18:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|