BrBETA_dnet-hadoop/dhp-workflows/dhp-propagation/src/main/java/eu/dnetlib/dhp/resulttocommunityfromsemrel/SparkResultToCommunityThrou...

137 lines
6.2 KiB
Java
Raw Normal View History

2020-04-21 16:03:51 +02:00
package eu.dnetlib.dhp.resulttocommunityfromsemrel;
2020-04-27 10:34:03 +02:00
import static eu.dnetlib.dhp.PropagationConstant.*;
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkHiveSession;
2020-04-21 16:03:51 +02:00
import com.fasterxml.jackson.databind.ObjectMapper;
2020-04-27 10:34:03 +02:00
import com.ximpleware.extended.xpath.parser;
2020-04-21 16:03:51 +02:00
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
2020-04-27 10:34:03 +02:00
import eu.dnetlib.dhp.resulttocommunityfromorganization.ResultCommunityList;
2020-04-21 16:03:51 +02:00
import eu.dnetlib.dhp.schema.oaf.*;
2020-04-27 10:34:03 +02:00
import java.util.*;
import java.util.stream.Collectors;
2020-04-21 16:03:51 +02:00
import org.apache.commons.io.IOUtils;
import org.apache.spark.SparkConf;
import org.apache.spark.sql.Encoders;
2020-04-27 10:34:03 +02:00
import org.apache.spark.sql.SaveMode;
2020-04-21 16:03:51 +02:00
import org.apache.spark.sql.SparkSession;
2020-04-27 10:34:03 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
public class SparkResultToCommunityThroughSemRelJob4 {
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
private static final Logger log =
LoggerFactory.getLogger(SparkResultToCommunityThroughSemRelJob4.class);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
public static void main(String[] args) throws Exception {
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
String jsonConfiguration =
IOUtils.toString(
SparkResultToCommunityThroughSemRelJob4.class.getResourceAsStream(
"/eu/dnetlib/dhp/resulttocommunityfromsemrel/input_communitytoresult_parameters.json"));
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
parser.parseArgument(args);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
Boolean isSparkSessionManaged = isSparkSessionManaged(parser);
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
String inputPath = parser.get("sourcePath");
log.info("inputPath: {}", inputPath);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
final String outputPath = parser.get("outputPath");
log.info("outputPath: {}", outputPath);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
final String preparedInfoPath = parser.get("preparedInfoPath");
log.info("preparedInfoPath: {}", preparedInfoPath);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
SparkConf conf = new SparkConf();
conf.set("hive.metastore.uris", parser.get("hive_metastore_uris"));
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
final String resultClassName = parser.get("resultTableName");
log.info("resultTableName: {}", resultClassName);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
final Boolean saveGraph =
Optional.ofNullable(parser.get("saveGraph"))
.map(Boolean::valueOf)
.orElse(Boolean.TRUE);
log.info("saveGraph: {}", saveGraph);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
Class<? extends Result> resultClazz =
(Class<? extends Result>) Class.forName(resultClassName);
2020-04-21 16:03:51 +02:00
2020-04-27 10:34:03 +02:00
runWithSparkHiveSession(
conf,
isSparkSessionManaged,
spark -> {
if (isTest(parser)) {
removeOutputDir(spark, outputPath);
2020-04-21 16:03:51 +02:00
}
2020-04-27 10:34:03 +02:00
if (saveGraph) {
execPropagation(
spark, inputPath, outputPath, preparedInfoPath, resultClazz);
2020-04-21 16:03:51 +02:00
}
});
}
2020-04-27 10:34:03 +02:00
private static <R extends Result> void execPropagation(
SparkSession spark,
String inputPath,
String outputPath,
String preparedInfoPath,
Class<R> resultClazz) {
org.apache.spark.sql.Dataset<ResultCommunityList> possibleUpdates =
readResultCommunityList(spark, preparedInfoPath);
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()) {
Set<String> context_set = new HashSet<>();
ret.getContext().stream().forEach(c -> context_set.add(c.getId()));
List<Context> contextList =
rcl.get().getCommunityList().stream()
.map(
c -> {
if (!context_set.contains(c)) {
Context newContext = new Context();
newContext.setId(c);
newContext.setDataInfo(
Arrays.asList(
getDataInfo(
PROPAGATION_DATA_INFO_TYPE,
PROPAGATION_RESULT_COMMUNITY_SEMREL_CLASS_ID,
PROPAGATION_RESULT_COMMUNITY_SEMREL_CLASS_NAME)));
return newContext;
}
return null;
})
.filter(c -> c != null)
.collect(Collectors.toList());
Result r = new Result();
r.setId(ret.getId());
r.setContext(contextList);
ret.mergeFrom(r);
2020-04-21 16:03:51 +02:00
}
2020-04-27 10:34:03 +02:00
return ret;
},
Encoders.bean(resultClazz))
.toJSON()
.write()
.mode(SaveMode.Overwrite)
.option("compression", "gzip")
.text(outputPath);
2020-04-21 16:03:51 +02:00
}
}