2020-04-27 14:52:31 +02:00
|
|
|
|
2019-04-11 15:39:29 +02:00
|
|
|
package eu.dnetlib.dhp.transformation;
|
|
|
|
|
2020-05-05 12:39:04 +02:00
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
2021-02-02 12:28:21 +01:00
|
|
|
import static eu.dnetlib.dhp.aggregation.common.AggregationUtility.*;
|
|
|
|
import static eu.dnetlib.dhp.aggregation.common.AggregationConstants.*;
|
2020-05-05 12:39:04 +02:00
|
|
|
|
2021-02-01 13:56:05 +01:00
|
|
|
import java.io.IOException;
|
2020-04-18 12:42:58 +02:00
|
|
|
import java.util.Map;
|
2020-05-05 12:39:04 +02:00
|
|
|
import java.util.Optional;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2021-02-02 12:28:21 +01:00
|
|
|
import eu.dnetlib.dhp.aggregation.common.AggregationConstants;
|
2019-10-10 11:33:51 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2020-05-05 12:39:04 +02:00
|
|
|
import org.apache.spark.SparkConf;
|
2021-02-02 12:28:21 +01:00
|
|
|
|
2019-04-11 15:39:29 +02:00
|
|
|
import org.apache.spark.sql.Dataset;
|
|
|
|
import org.apache.spark.sql.Encoder;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.apache.spark.util.LongAccumulator;
|
2020-05-05 12:39:04 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2019-04-11 15:39:29 +02:00
|
|
|
|
2021-02-01 13:56:05 +01:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreVersion;
|
2021-01-28 09:51:17 +01:00
|
|
|
import eu.dnetlib.dhp.aggregation.common.AggregationCounter;
|
2021-02-01 13:56:05 +01:00
|
|
|
import eu.dnetlib.dhp.aggregation.common.AggregationUtility;
|
2020-04-28 11:23:29 +02:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2021-02-02 12:12:14 +01:00
|
|
|
import eu.dnetlib.dhp.common.vocabulary.VocabularyGroup;
|
2020-04-28 11:23:29 +02:00
|
|
|
import eu.dnetlib.dhp.model.mdstore.MetadataRecord;
|
2021-01-28 09:51:17 +01:00
|
|
|
import eu.dnetlib.dhp.utils.ISLookupClientFactory;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2019-04-11 15:39:29 +02:00
|
|
|
public class TransformSparkJobNode {
|
|
|
|
|
2020-05-05 12:39:04 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(TransformSparkJobNode.class);
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public static void main(String[] args) throws Exception {
|
2019-04-11 15:39:29 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
TransformSparkJobNode.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/transformation/transformation_input_parameters.json")));
|
2019-04-11 15:39:29 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
parser.parseArgument(args);
|
2019-04-11 15:39:29 +02:00
|
|
|
|
2020-05-05 12:39:04 +02:00
|
|
|
Boolean isSparkSessionManaged = Optional
|
|
|
|
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
2021-02-01 13:56:05 +01:00
|
|
|
final String mdstoreInputVersion = parser.get("mdstoreInputVersion");
|
|
|
|
final String mdstoreOutputVersion = parser.get("mdstoreOutputVersion");
|
2021-01-27 15:43:08 +01:00
|
|
|
// TODO this variable will be used after implementing Messaging with DNet Aggregator
|
2020-05-05 12:39:04 +02:00
|
|
|
|
2021-02-01 13:56:05 +01:00
|
|
|
final ObjectMapper jsonMapper = new ObjectMapper();
|
|
|
|
final MDStoreVersion nativeMdStoreVersion = jsonMapper.readValue(mdstoreInputVersion, MDStoreVersion.class);
|
|
|
|
final MDStoreVersion cleanedMdStoreVersion = jsonMapper.readValue(mdstoreOutputVersion, MDStoreVersion.class);
|
|
|
|
|
2021-01-27 15:43:08 +01:00
|
|
|
final String isLookupUrl = parser.get("isLookupUrl");
|
|
|
|
log.info(String.format("isLookupUrl: %s", isLookupUrl));
|
|
|
|
|
2021-02-02 12:12:14 +01:00
|
|
|
final String dateOfTransformation = parser.get("dateOfTransformation");
|
|
|
|
log.info(String.format("dateOfTransformation: %s", dateOfTransformation));
|
|
|
|
|
|
|
|
|
2021-01-27 15:43:08 +01:00
|
|
|
final ISLookUpService isLookupService = ISLookupClientFactory.getLookUpService(isLookupUrl);
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2021-02-02 12:12:14 +01:00
|
|
|
final VocabularyGroup vocabularies = VocabularyGroup.loadVocsFromIS(isLookupService);
|
|
|
|
|
|
|
|
log.info("Retrieved {} vocabularies", vocabularies.vocabularyNames().size());
|
|
|
|
|
2020-05-05 12:39:04 +02:00
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
runWithSparkSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
2021-02-01 13:56:05 +01:00
|
|
|
spark -> transformRecords(
|
2021-02-02 12:28:21 +01:00
|
|
|
parser.getObjectMap(), isLookupService, spark, nativeMdStoreVersion.getHdfsPath() + MDSTORE_DATA_PATH,
|
|
|
|
cleanedMdStoreVersion.getHdfsPath() + MDSTORE_DATA_PATH));
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
2019-04-11 15:39:29 +02:00
|
|
|
|
2021-01-28 09:51:17 +01:00
|
|
|
public static void transformRecords(final Map<String, String> args, final ISLookUpService isLookUpService,
|
2021-02-01 13:56:05 +01:00
|
|
|
final SparkSession spark, final String inputPath, final String outputPath)
|
|
|
|
throws DnetTransformationException, IOException {
|
2021-01-27 15:43:08 +01:00
|
|
|
|
2021-02-02 12:28:21 +01:00
|
|
|
final LongAccumulator totalItems = spark.sparkContext().longAccumulator(CONTENT_TOTALITEMS);
|
|
|
|
final LongAccumulator errorItems = spark.sparkContext().longAccumulator(CONTENT_INVALIDRECORDS);
|
|
|
|
final LongAccumulator transformedItems = spark.sparkContext().longAccumulator(CONTENT_TRANSFORMEDRECORDS);
|
2021-01-28 09:51:17 +01:00
|
|
|
final AggregationCounter ct = new AggregationCounter(totalItems, errorItems, transformedItems);
|
2021-01-27 15:43:08 +01:00
|
|
|
final Encoder<MetadataRecord> encoder = Encoders.bean(MetadataRecord.class);
|
2021-02-02 12:28:21 +01:00
|
|
|
|
|
|
|
saveDataset(
|
|
|
|
spark.read()
|
|
|
|
.format("parquet")
|
|
|
|
.load(inputPath)
|
|
|
|
.as(encoder)
|
|
|
|
.map(
|
|
|
|
TransformationFactory.getTransformationPlugin(args, ct, isLookUpService),
|
|
|
|
encoder),
|
|
|
|
outputPath + MDSTORE_DATA_PATH);
|
|
|
|
|
2021-01-27 15:43:08 +01:00
|
|
|
|
2021-01-28 09:51:17 +01:00
|
|
|
log.info("Transformed item " + ct.getProcessedItems().count());
|
|
|
|
log.info("Total item " + ct.getTotalItems().count());
|
|
|
|
log.info("Transformation Error item " + ct.getErrorItems().count());
|
2021-02-01 13:56:05 +01:00
|
|
|
|
2021-02-02 12:28:21 +01:00
|
|
|
writeTotalSizeOnHDFS(spark, ct.getProcessedItems().count(), outputPath + MDSTORE_SIZE_PATH);
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
2021-01-27 15:43:08 +01:00
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
}
|