2020-02-17 12:31:44 +01:00
|
|
|
package eu.dnetlib.dhp.migration;
|
|
|
|
|
2020-02-25 15:59:21 +01:00
|
|
|
import java.io.IOException;
|
2020-02-19 15:11:17 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2020-02-25 15:59:21 +01:00
|
|
|
import java.util.stream.Collectors;
|
2020-02-19 15:11:17 +01:00
|
|
|
|
2020-02-17 12:31:44 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2020-02-25 15:59:21 +01:00
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.hadoop.fs.FileSystem;
|
|
|
|
import org.apache.hadoop.fs.Path;
|
2020-02-17 12:31:44 +01:00
|
|
|
import org.apache.hadoop.io.Text;
|
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
|
2020-02-19 15:11:17 +01:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Dataset;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Datasource;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Organization;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.OtherResearchProduct;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Project;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Publication;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Software;
|
|
|
|
import scala.Tuple2;
|
2020-02-17 12:31:44 +01:00
|
|
|
|
|
|
|
public class ExtractEntitiesFromHDFSJob {
|
|
|
|
|
2020-02-25 15:59:21 +01:00
|
|
|
private static final Log log = LogFactory.getLog(ExtractEntitiesFromHDFSJob.class);
|
|
|
|
|
2020-02-19 15:11:17 +01:00
|
|
|
public static void main(final String[] args) throws Exception {
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils.toString(MigrateMongoMdstoresApplication.class
|
|
|
|
.getResourceAsStream("/eu/dnetlib/dhp/migration/extract_entities_from_hdfs_parameters.json")));
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
final SparkSession spark = SparkSession
|
|
|
|
.builder()
|
|
|
|
.appName(ExtractEntitiesFromHDFSJob.class.getSimpleName())
|
|
|
|
.master(parser.get("master"))
|
|
|
|
.getOrCreate();
|
|
|
|
|
|
|
|
try (final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext())) {
|
2020-02-25 15:59:21 +01:00
|
|
|
|
|
|
|
final List<String> sourcePaths = Arrays.stream(parser.get("sourcePaths").split(",")).filter(p -> exists(sc, p)).collect(Collectors.toList());
|
|
|
|
final String targetPath = parser.get("graphRawPath");
|
|
|
|
|
2020-02-19 15:11:17 +01:00
|
|
|
processEntity(sc, Publication.class, sourcePaths, targetPath);
|
|
|
|
processEntity(sc, Dataset.class, sourcePaths, targetPath);
|
|
|
|
processEntity(sc, Software.class, sourcePaths, targetPath);
|
|
|
|
processEntity(sc, OtherResearchProduct.class, sourcePaths, targetPath);
|
|
|
|
processEntity(sc, Datasource.class, sourcePaths, targetPath);
|
|
|
|
processEntity(sc, Organization.class, sourcePaths, targetPath);
|
|
|
|
processEntity(sc, Project.class, sourcePaths, targetPath);
|
|
|
|
processEntity(sc, Relation.class, sourcePaths, targetPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void processEntity(final JavaSparkContext sc, final Class<?> clazz, final List<String> sourcePaths, final String targetPath) {
|
|
|
|
final String type = clazz.getSimpleName().toLowerCase();
|
|
|
|
|
2020-02-25 15:59:21 +01:00
|
|
|
log.info(String.format("Processing entities (%s) in files:", type));
|
|
|
|
sourcePaths.forEach(log::info);
|
|
|
|
|
|
|
|
JavaRDD<String> inputRdd = sc.emptyRDD();
|
|
|
|
|
|
|
|
for (final String sp : sourcePaths) {
|
|
|
|
inputRdd = inputRdd.union(sc.sequenceFile(sp, Text.class, Text.class)
|
|
|
|
.map(k -> new Tuple2<>(k._1().toString(), k._2().toString()))
|
|
|
|
.filter(k -> isEntityType(k._1(), type))
|
|
|
|
.map(Tuple2::_2));
|
|
|
|
}
|
2020-02-19 15:11:17 +01:00
|
|
|
|
|
|
|
inputRdd.saveAsTextFile(targetPath + "/" + type);
|
2020-02-25 15:59:21 +01:00
|
|
|
|
2020-02-19 15:11:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 15:59:21 +01:00
|
|
|
private static boolean isEntityType(final String item, final String type) {
|
|
|
|
return StringUtils.substringAfter(item, ":").equalsIgnoreCase(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean exists(final JavaSparkContext context, final String pathToFile) {
|
|
|
|
try {
|
|
|
|
final FileSystem hdfs = org.apache.hadoop.fs.FileSystem.get(context.hadoopConfiguration());
|
|
|
|
final Path path = new Path(pathToFile);
|
|
|
|
return hdfs.exists(path);
|
|
|
|
} catch (final IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2020-02-19 15:11:17 +01:00
|
|
|
}
|
2020-02-17 12:31:44 +01:00
|
|
|
}
|