2023-09-14 17:43:25 +02:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.swh;
|
|
|
|
|
2023-09-27 18:00:54 +02:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Result;
|
2023-09-14 17:43:25 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
import org.apache.spark.sql.Dataset;
|
2023-09-27 18:00:54 +02:00
|
|
|
import org.apache.spark.sql.Row;
|
|
|
|
import org.apache.spark.sql.SaveMode;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
2023-09-14 17:43:25 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2023-09-27 18:00:54 +02:00
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.Optional;
|
2023-09-14 17:43:25 +02:00
|
|
|
|
2023-09-27 18:00:54 +02:00
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkHiveSession;
|
2023-09-14 17:43:25 +02:00
|
|
|
|
|
|
|
/**
|
2023-09-27 18:00:54 +02:00
|
|
|
* Collects unique software repository URLs in the Graph using Hive
|
|
|
|
*
|
|
|
|
* @author Serafeim Chatzopoulos
|
2023-09-14 17:43:25 +02:00
|
|
|
*/
|
|
|
|
public class CollectSoftwareRepositoryURLs implements Serializable {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(CollectSoftwareRepositoryURLs.class);
|
|
|
|
|
|
|
|
public static <I extends Result> void main(String[] args) throws Exception {
|
|
|
|
|
|
|
|
String jsonConfiguration = IOUtils
|
|
|
|
.toString(
|
|
|
|
CollectSoftwareRepositoryURLs.class
|
2023-09-27 18:00:54 +02:00
|
|
|
.getResourceAsStream("/eu/dnetlib/dhp/swh/input_collect_software_repository_urls.json"));
|
2023-09-14 17:43:25 +02:00
|
|
|
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
final Boolean isSparkSessionManaged = Optional
|
|
|
|
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
final String hiveDbName = parser.get("hiveDbName");
|
|
|
|
log.info("hiveDbName {}: ", hiveDbName);
|
|
|
|
|
|
|
|
final String outputPath = parser.get("softwareCodeRepositoryURLs");
|
|
|
|
log.info("softwareCodeRepositoryURLs {}: ", outputPath);
|
|
|
|
|
|
|
|
final String hiveMetastoreUris = parser.get("hiveMetastoreUris");
|
|
|
|
log.info("hiveMetastoreUris: {}", hiveMetastoreUris);
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.set("hive.metastore.uris", hiveMetastoreUris);
|
|
|
|
|
|
|
|
runWithSparkHiveSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
|
|
|
spark -> {
|
|
|
|
doRun(spark, hiveDbName, outputPath);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static <I extends Result> void doRun(SparkSession spark, String hiveDbName, String outputPath) {
|
|
|
|
|
|
|
|
String queryTemplate = "SELECT distinct coderepositoryurl.value " +
|
|
|
|
"FROM %s.software " +
|
2023-09-27 18:00:54 +02:00
|
|
|
"WHERE coderepositoryurl.value IS NOT NULL " +
|
|
|
|
"AND datainfo.deletedbyinference = FALSE " +
|
|
|
|
"AND datainfo.invisible = FALSE " +
|
|
|
|
"LIMIT 1000"; // TODO remove
|
2023-09-14 17:43:25 +02:00
|
|
|
String query = String.format(queryTemplate, hiveDbName);
|
|
|
|
|
|
|
|
log.info("Hive query to fetch software code URLs: {}", query);
|
|
|
|
|
|
|
|
Dataset<Row> df = spark.sql(query);
|
|
|
|
|
|
|
|
// write distinct repository URLs
|
|
|
|
df
|
|
|
|
.write()
|
|
|
|
.mode(SaveMode.Overwrite)
|
|
|
|
.csv(outputPath);
|
|
|
|
}
|
|
|
|
}
|