2020-04-27 14:52:31 +02:00
|
|
|
|
2020-03-27 10:42:17 +01:00
|
|
|
package eu.dnetlib.dhp.oa.provision;
|
2020-02-13 16:53:27 +01:00
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.io.StringWriter;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Optional;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2020-12-10 16:14:16 +01:00
|
|
|
import javax.swing.text.html.Option;
|
2020-04-18 12:42:58 +02:00
|
|
|
import javax.xml.transform.Transformer;
|
|
|
|
import javax.xml.transform.TransformerException;
|
|
|
|
import javax.xml.transform.stream.StreamResult;
|
|
|
|
import javax.xml.transform.stream.StreamSource;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2020-02-13 16:53:27 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.hadoop.io.Text;
|
|
|
|
import org.apache.solr.common.SolrInputDocument;
|
|
|
|
import org.apache.spark.SparkConf;
|
2020-11-18 14:34:55 +01:00
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
2020-02-13 16:53:27 +01:00
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
2020-11-18 14:34:55 +01:00
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.SaveMode;
|
2020-11-18 10:58:05 +01:00
|
|
|
import org.apache.spark.sql.SparkSession;
|
2020-04-04 14:03:43 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2020-02-13 16:53:27 +01:00
|
|
|
|
2020-04-28 11:23:29 +02:00
|
|
|
import com.lucidworks.spark.util.SolrSupport;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2020-11-19 14:34:54 +01:00
|
|
|
import eu.dnetlib.dhp.oa.provision.model.SerializableSolrInputDocument;
|
|
|
|
import eu.dnetlib.dhp.oa.provision.utils.ISLookupClient;
|
2020-04-28 11:23:29 +02:00
|
|
|
import eu.dnetlib.dhp.oa.provision.utils.StreamingInputDocumentFactory;
|
|
|
|
import eu.dnetlib.dhp.utils.ISLookupClientFactory;
|
|
|
|
import eu.dnetlib.dhp.utils.saxon.SaxonTransformerFactory;
|
|
|
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
|
|
|
|
2020-11-18 10:58:05 +01:00
|
|
|
public class XmlIndexingJob {
|
2020-02-13 16:53:27 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(XmlIndexingJob.class);
|
|
|
|
|
2020-12-10 16:14:16 +01:00
|
|
|
public enum OutputFormat {
|
|
|
|
SOLR, HDFS
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
private static final Integer DEFAULT_BATCH_SIZE = 1000;
|
|
|
|
|
2020-11-18 10:58:05 +01:00
|
|
|
protected static final String DATE_FORMAT = "yyyy-MM-dd'T'hh:mm:ss'Z'";
|
|
|
|
|
2021-05-14 10:58:12 +02:00
|
|
|
private final String inputPath;
|
2020-11-18 10:58:05 +01:00
|
|
|
|
2021-05-14 10:58:12 +02:00
|
|
|
private final String format;
|
2020-11-18 10:58:05 +01:00
|
|
|
|
2021-05-14 10:58:12 +02:00
|
|
|
private final int batchSize;
|
2020-11-18 10:58:05 +01:00
|
|
|
|
2021-05-14 10:58:12 +02:00
|
|
|
private final OutputFormat outputFormat;
|
2020-12-10 16:14:16 +01:00
|
|
|
|
2021-05-14 10:58:12 +02:00
|
|
|
private final String outputPath;
|
2020-11-18 14:34:55 +01:00
|
|
|
|
2021-05-14 10:58:12 +02:00
|
|
|
private final SparkSession spark;
|
2020-11-18 10:58:05 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
XmlIndexingJob.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/oa/provision/input_params_update_index.json")));
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
Boolean isSparkSessionManaged = Optional
|
|
|
|
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
final String inputPath = parser.get("inputPath");
|
|
|
|
log.info("inputPath: {}", inputPath);
|
|
|
|
|
|
|
|
final String format = parser.get("format");
|
|
|
|
log.info("format: {}", format);
|
|
|
|
|
2020-11-19 14:34:54 +01:00
|
|
|
final String outputPath = Optional
|
|
|
|
.ofNullable(parser.get("outputPath"))
|
2020-12-10 16:14:16 +01:00
|
|
|
.map(StringUtils::trim)
|
2020-11-19 14:34:54 +01:00
|
|
|
.orElse(null);
|
2020-11-18 14:34:55 +01:00
|
|
|
log.info("outputPath: {}", outputPath);
|
|
|
|
|
2020-12-10 16:14:16 +01:00
|
|
|
final Integer batchSize = Optional
|
|
|
|
.ofNullable(parser.get("batchSize"))
|
|
|
|
.map(Integer::valueOf)
|
|
|
|
.orElse(DEFAULT_BATCH_SIZE);
|
2020-04-27 14:52:31 +02:00
|
|
|
log.info("batchSize: {}", batchSize);
|
|
|
|
|
2020-12-10 16:14:16 +01:00
|
|
|
final OutputFormat outputFormat = Optional
|
|
|
|
.ofNullable(parser.get("outputFormat"))
|
|
|
|
.map(OutputFormat::valueOf)
|
|
|
|
.orElse(OutputFormat.SOLR);
|
|
|
|
log.info("outputFormat: {}", outputFormat);
|
|
|
|
|
2020-11-18 10:58:05 +01:00
|
|
|
final SparkConf conf = new SparkConf();
|
2020-11-19 14:34:54 +01:00
|
|
|
conf.registerKryoClasses(new Class[] {
|
|
|
|
SerializableSolrInputDocument.class
|
|
|
|
});
|
2020-11-18 10:58:05 +01:00
|
|
|
|
|
|
|
runWithSparkSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
|
|
|
spark -> {
|
|
|
|
final String isLookupUrl = parser.get("isLookupUrl");
|
|
|
|
log.info("isLookupUrl: {}", isLookupUrl);
|
|
|
|
final ISLookupClient isLookup = new ISLookupClient(ISLookupClientFactory.getLookUpService(isLookupUrl));
|
2020-12-10 16:14:16 +01:00
|
|
|
new XmlIndexingJob(spark, inputPath, format, batchSize, outputFormat, outputPath).run(isLookup);
|
2020-11-18 10:58:05 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:14:16 +01:00
|
|
|
public XmlIndexingJob(SparkSession spark, String inputPath, String format, Integer batchSize,
|
|
|
|
OutputFormat outputFormat,
|
|
|
|
String outputPath) {
|
2020-11-18 10:58:05 +01:00
|
|
|
this.spark = spark;
|
|
|
|
this.inputPath = inputPath;
|
|
|
|
this.format = format;
|
|
|
|
this.batchSize = batchSize;
|
2020-12-10 16:14:16 +01:00
|
|
|
this.outputFormat = outputFormat;
|
2020-11-18 14:34:55 +01:00
|
|
|
this.outputPath = outputPath;
|
2020-11-18 10:58:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void run(ISLookupClient isLookup) throws ISLookUpException, TransformerException {
|
|
|
|
final String fields = isLookup.getLayoutSource(format);
|
2020-04-27 14:52:31 +02:00
|
|
|
log.info("fields: {}", fields);
|
|
|
|
|
2020-11-18 10:58:05 +01:00
|
|
|
final String xslt = isLookup.getLayoutTransformer();
|
2020-04-27 14:52:31 +02:00
|
|
|
|
2020-11-18 10:58:05 +01:00
|
|
|
final String dsId = isLookup.getDsId(format);
|
2020-04-27 14:52:31 +02:00
|
|
|
log.info("dsId: {}", dsId);
|
|
|
|
|
2020-11-18 10:58:05 +01:00
|
|
|
final String zkHost = isLookup.getZkHost();
|
2020-04-27 14:52:31 +02:00
|
|
|
log.info("zkHost: {}", zkHost);
|
|
|
|
|
|
|
|
final String version = getRecordDatestamp();
|
|
|
|
|
|
|
|
final String indexRecordXslt = getLayoutTransformer(format, fields, xslt);
|
|
|
|
log.info("indexRecordTransformer {}", indexRecordXslt);
|
|
|
|
|
2020-11-18 10:58:05 +01:00
|
|
|
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
2020-04-27 14:52:31 +02:00
|
|
|
|
2020-11-18 14:34:55 +01:00
|
|
|
JavaRDD<SolrInputDocument> docs = sc
|
2020-11-19 14:34:54 +01:00
|
|
|
.sequenceFile(inputPath, Text.class, Text.class)
|
|
|
|
.map(t -> t._2().toString())
|
|
|
|
.map(s -> toIndexRecord(SaxonTransformerFactory.newInstance(indexRecordXslt), s))
|
|
|
|
.map(s -> new StreamingInputDocumentFactory(version, dsId).parseDocument(s));
|
2020-11-18 14:34:55 +01:00
|
|
|
|
2020-12-10 16:14:16 +01:00
|
|
|
switch (outputFormat) {
|
|
|
|
case SOLR:
|
|
|
|
final String collection = ProvisionConstants.getCollectionName(format);
|
|
|
|
SolrSupport.indexDocs(zkHost, collection, batchSize, docs.rdd());
|
|
|
|
break;
|
|
|
|
case HDFS:
|
|
|
|
spark
|
|
|
|
.createDataset(
|
|
|
|
docs.map(s -> new SerializableSolrInputDocument(s)).rdd(),
|
|
|
|
Encoders.kryo(SerializableSolrInputDocument.class))
|
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.parquet(outputPath);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("invalid outputFormat: " + outputFormat);
|
2020-11-18 14:34:55 +01:00
|
|
|
}
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 11:51:17 +02:00
|
|
|
protected static String toIndexRecord(Transformer tr, final String record) {
|
2020-04-27 14:52:31 +02:00
|
|
|
final StreamResult res = new StreamResult(new StringWriter());
|
|
|
|
try {
|
|
|
|
tr.transform(new StreamSource(new StringReader(record)), res);
|
|
|
|
return res.getWriter().toString();
|
|
|
|
} catch (Throwable e) {
|
|
|
|
log.error("XPathException on record: \n {}", record, e);
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the XSLT responsible for building the index xml records.
|
|
|
|
*
|
|
|
|
* @param format Metadata format name (DMF|TMF)
|
|
|
|
* @param xslt xslt for building the index record transformer
|
|
|
|
* @param fields the list of fields
|
|
|
|
* @return the javax.xml.transform.Transformer
|
|
|
|
* @throws ISLookUpException could happen
|
|
|
|
* @throws IOException could happen
|
|
|
|
* @throws TransformerException could happen
|
|
|
|
*/
|
2020-05-04 11:51:17 +02:00
|
|
|
protected static String getLayoutTransformer(String format, String fields, String xslt)
|
2020-04-27 14:52:31 +02:00
|
|
|
throws TransformerException {
|
|
|
|
|
|
|
|
final Transformer layoutTransformer = SaxonTransformerFactory.newInstance(xslt);
|
|
|
|
final StreamResult layoutToXsltXslt = new StreamResult(new StringWriter());
|
|
|
|
|
|
|
|
layoutTransformer.setParameter("format", format);
|
|
|
|
layoutTransformer.transform(new StreamSource(new StringReader(fields)), layoutToXsltXslt);
|
|
|
|
|
|
|
|
return layoutToXsltXslt.getWriter().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* method return a solr-compatible string representation of a date, used to mark all records as indexed today
|
|
|
|
*
|
|
|
|
* @return the parsed date
|
|
|
|
*/
|
|
|
|
public static String getRecordDatestamp() {
|
|
|
|
return new SimpleDateFormat(DATE_FORMAT).format(new Date());
|
|
|
|
}
|
|
|
|
|
2020-02-13 16:53:27 +01:00
|
|
|
}
|