dnet-hadoop/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/collection/plugin/base/BaseAnalyzerJob.java

191 lines
6.2 KiB
Java
Raw Normal View History

2024-02-14 11:39:37 +01:00
package eu.dnetlib.dhp.collection.plugin.base;
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
import java.io.IOException;
2024-02-14 10:37:39 +01:00
import java.util.ArrayList;
import java.util.HashMap;
2024-02-14 10:37:39 +01:00
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
2024-02-14 11:39:37 +01:00
import java.util.concurrent.atomic.AtomicLong;
2024-02-14 10:37:39 +01:00
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.DeflateCodec;
import org.apache.spark.SparkConf;
import org.apache.spark.sql.SparkSession;
import org.dom4j.Attribute;
import org.dom4j.Document;
2024-02-15 08:21:52 +01:00
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
2024-02-14 10:37:39 +01:00
import org.dom4j.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
import eu.dnetlib.dhp.common.aggregation.AggregatorReport;
public class BaseAnalyzerJob {
2024-02-14 15:52:31 +01:00
private static final Logger log = LoggerFactory.getLogger(BaseAnalyzerJob.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static void main(final String[] args) throws Exception {
final String jsonConfiguration = IOUtils
2024-02-15 08:21:52 +01:00
.toString(
BaseAnalyzerJob.class
.getResourceAsStream("/eu/dnetlib/dhp/collection/plugin/base/action_set_parameters.json"));
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
parser.parseArgument(args);
final Boolean isSparkSessionManaged = Optional
2024-02-15 08:21:52 +01:00
.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 outputPath = parser.get("outputPath");
log.info("outputPath {}: ", outputPath);
final SparkConf conf = new SparkConf();
2024-02-14 10:37:39 +01:00
runWithSparkSession(conf, isSparkSessionManaged, spark -> processBaseRecords(spark, inputPath, outputPath));
}
private static void processBaseRecords(final SparkSession spark,
2024-02-15 08:21:52 +01:00
final String inputPath,
final String outputPath) throws IOException {
2024-02-14 10:37:39 +01:00
2024-02-14 11:39:37 +01:00
try (final FileSystem fs = FileSystem.get(new Configuration());
2024-02-15 08:21:52 +01:00
final AggregatorReport report = new AggregatorReport()) {
2024-02-14 11:39:37 +01:00
final Map<String, AtomicLong> fields = new HashMap<>();
final Map<String, AtomicLong> types = new HashMap<>();
final Map<String, AtomicLong> collections = new HashMap<>();
final Map<String, AtomicLong> totals = new HashMap<>();
analyze(fs, inputPath, fields, types, collections, totals, report);
2024-02-14 10:37:39 +01:00
saveReport(fs, outputPath + "/fields", fields);
saveReport(fs, outputPath + "/types", types);
saveReport(fs, outputPath + "/collections", collections);
2024-02-14 11:39:37 +01:00
saveReport(fs, outputPath + "/totals", totals);
} catch (final Throwable e) {
throw new RuntimeException(e);
}
}
2024-02-14 10:37:39 +01:00
private static void analyze(final FileSystem fs,
2024-02-15 08:21:52 +01:00
final String inputPath,
final Map<String, AtomicLong> fields,
final Map<String, AtomicLong> types,
final Map<String, AtomicLong> collections,
final Map<String, AtomicLong> totals,
final AggregatorReport report) throws JsonProcessingException, IOException, DocumentException {
2024-02-14 11:39:37 +01:00
final AtomicLong recordsCounter = new AtomicLong(0);
totals.put("Records", recordsCounter);
2024-02-14 10:37:39 +01:00
final BaseCollectorIterator iteraror = new BaseCollectorIterator(fs, new Path(inputPath), report);
2024-02-14 10:37:39 +01:00
while (iteraror.hasNext()) {
2024-02-15 08:21:52 +01:00
final Document record = DocumentHelper.parseText(iteraror.next());
2024-02-14 11:39:37 +01:00
final long i = recordsCounter.incrementAndGet();
2024-02-14 10:37:39 +01:00
if ((i % 10000) == 0) {
log.info("# Read records: " + i);
2024-02-14 14:40:27 +01:00
log.info("# fields: " + fields.size());
log.info("# types: " + types.size());
log.info("# collections: " + collections.size());
log.info("# totals: " + totals.size());
2024-02-14 10:37:39 +01:00
}
2024-02-14 10:37:39 +01:00
final List<String> recTypes = new ArrayList<>();
2024-02-14 10:37:39 +01:00
for (final Object o : record.selectNodes("//*[local-name()='metadata']//*")) {
2024-02-14 10:37:39 +01:00
incrementMapCounter(fields, ((Node) o).getPath());
2024-02-14 10:37:39 +01:00
final String nodeName = ((Node) o).getName();
if ("collection".equals(nodeName)) {
final Element n = (Element) o;
final String collName = n.getText().trim();
2024-02-14 10:37:39 +01:00
if (StringUtils.isNotBlank(collName)) {
final Map<String, String> map = new HashMap<>();
for (final Object ao : n.attributes()) {
map.put(((Attribute) ao).getName(), ((Attribute) ao).getValue());
}
2024-02-14 10:37:39 +01:00
incrementMapCounter(collections, collName + ": " + OBJECT_MAPPER.writeValueAsString(map));
}
2024-02-14 10:37:39 +01:00
} else if ("type".equals(nodeName)) {
recTypes.add("TYPE: " + nodeName);
} else if ("typenorm".equals(nodeName)) {
recTypes.add("TYPE_NORM: " + nodeName);
}
}
2024-02-14 10:37:39 +01:00
incrementMapCounter(types, recTypes.stream().sorted().distinct().collect(Collectors.joining(", ")));
}
}
2024-02-14 11:39:37 +01:00
private static void incrementMapCounter(final Map<String, AtomicLong> map, final String key) {
2024-02-14 10:37:39 +01:00
if (StringUtils.isNotBlank(key)) {
if (map.containsKey(key)) {
map.get(key).incrementAndGet();
} else {
2024-02-14 11:39:37 +01:00
map.put(key, new AtomicLong(1));
2024-02-14 10:37:39 +01:00
}
}
}
2024-02-14 11:39:37 +01:00
private static void saveReport(final FileSystem fs, final String outputPath, final Map<String, AtomicLong> fields)
2024-02-15 08:21:52 +01:00
throws JsonProcessingException, IOException {
try (final SequenceFile.Writer writer = SequenceFile
2024-02-15 08:21:52 +01:00
.createWriter(
fs.getConf(), SequenceFile.Writer.file(new Path(outputPath)), SequenceFile.Writer
.keyClass(IntWritable.class),
SequenceFile.Writer
.valueClass(Text.class),
SequenceFile.Writer.compression(SequenceFile.CompressionType.BLOCK, new DeflateCodec()))) {
final Text key = new Text();
final Text value = new Text();
2024-02-14 11:39:37 +01:00
for (final Entry<String, AtomicLong> e : fields.entrySet()) {
key.set(e.getKey());
value.set(e.getKey() + ": " + e.getValue());
try {
writer.append(key, value);
} catch (final Throwable e1) {
throw new RuntimeException(e1);
}
}
}
}
}