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.util.Optional;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
import javax.xml.transform.TransformerException;
|
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.solr.common.SolrInputDocument;
|
|
|
|
import org.apache.spark.SparkConf;
|
2020-11-18 14:34:55 +01:00
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
2024-03-08 11:43:20 +01:00
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
2024-03-12 09:56:09 +01:00
|
|
|
import org.apache.spark.sql.Encoder;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
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;
|
2024-03-08 11:43:20 +01:00
|
|
|
import eu.dnetlib.dhp.oa.provision.model.TupleWrapper;
|
2020-11-19 14:34:54 +01:00
|
|
|
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;
|
|
|
|
|
2024-03-12 09:56:09 +01:00
|
|
|
public class XmlIndexingJob extends AbstractSolrRecordTransformJob {
|
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);
|
|
|
|
|
|
|
|
private static final Integer DEFAULT_BATCH_SIZE = 1000;
|
|
|
|
|
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 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-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-11-18 10:58:05 +01:00
|
|
|
final SparkConf conf = new SparkConf();
|
2024-03-08 11:43:20 +01:00
|
|
|
|
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));
|
2024-03-12 09:56:09 +01:00
|
|
|
new XmlIndexingJob(spark, inputPath, format, batchSize)
|
2024-03-08 11:43:20 +01:00
|
|
|
.run(isLookup);
|
2020-11-18 10:58:05 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-12 09:56:09 +01:00
|
|
|
public XmlIndexingJob(SparkSession spark, String inputPath, String format, Integer batchSize) {
|
2020-11-18 10:58:05 +01:00
|
|
|
this.spark = spark;
|
|
|
|
this.inputPath = inputPath;
|
|
|
|
this.format = format;
|
|
|
|
this.batchSize = batchSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-03-12 09:56:09 +01:00
|
|
|
final String collection = ProvisionConstants.getCollectionName(format);
|
|
|
|
log.info("collection: {}", collection);
|
|
|
|
|
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 indexRecordXslt = getLayoutTransformer(format, fields, xslt);
|
|
|
|
log.info("indexRecordTransformer {}", indexRecordXslt);
|
|
|
|
|
2024-03-08 11:43:20 +01:00
|
|
|
final Encoder<TupleWrapper> encoder = Encoders.bean(TupleWrapper.class);
|
2024-03-12 09:56:09 +01:00
|
|
|
|
|
|
|
JavaRDD<SolrInputDocument> docs = spark
|
2024-03-08 11:43:20 +01:00
|
|
|
.read()
|
|
|
|
.schema(encoder.schema())
|
|
|
|
.json(inputPath)
|
|
|
|
.as(encoder)
|
|
|
|
.map(
|
|
|
|
(MapFunction<TupleWrapper, TupleWrapper>) t -> new TupleWrapper(
|
|
|
|
toIndexRecord(SaxonTransformerFactory.newInstance(indexRecordXslt), t.getXml()),
|
|
|
|
t.getJson()),
|
2024-03-12 09:56:09 +01:00
|
|
|
Encoders.bean(TupleWrapper.class))
|
|
|
|
.javaRDD()
|
|
|
|
.map(
|
|
|
|
t -> new StreamingInputDocumentFactory().parseDocument(t.getXml(), t.getJson()));
|
|
|
|
SolrSupport.indexDocs(zkHost, collection, batchSize, docs.rdd());
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 16:53:27 +01:00
|
|
|
}
|