2021-07-13 17:10:02 +02:00
|
|
|
|
2021-07-13 15:23:09 +02:00
|
|
|
package eu.dnetlib.dhp.oa.graph.dump.complete;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
import org.apache.spark.api.java.function.ForeachFunction;
|
|
|
|
import org.apache.spark.sql.Dataset;
|
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.Row;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
import org.junit.jupiter.api.AfterAll;
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
|
|
|
import net.sf.saxon.expr.instruct.ForEach;
|
|
|
|
|
|
|
|
public class SelectRelationTest {
|
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
private static SparkSession spark;
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
private static Path workingDir;
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
private static final Logger log = LoggerFactory
|
|
|
|
.getLogger(SelectRelationTest.class);
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
private static HashMap<String, String> map = new HashMap<>();
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
@BeforeAll
|
|
|
|
public static void beforeAll() throws IOException {
|
|
|
|
workingDir = Files
|
|
|
|
.createTempDirectory(SelectRelationTest.class.getSimpleName());
|
|
|
|
log.info("using work dir {}", workingDir);
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.setAppName(SelectRelationTest.class.getSimpleName());
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
conf.setMaster("local[*]");
|
|
|
|
conf.set("spark.driver.host", "localhost");
|
|
|
|
conf.set("hive.metastore.local", "true");
|
|
|
|
conf.set("spark.ui.enabled", "false");
|
|
|
|
conf.set("spark.sql.warehouse.dir", workingDir.toString());
|
|
|
|
conf.set("hive.metastore.warehouse.dir", workingDir.resolve("warehouse").toString());
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
spark = SparkSession
|
|
|
|
.builder()
|
|
|
|
.appName(SelectRelationTest.class.getSimpleName())
|
|
|
|
.config(conf)
|
|
|
|
.getOrCreate();
|
|
|
|
}
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
@AfterAll
|
|
|
|
public static void afterAll() throws IOException {
|
|
|
|
FileUtils.deleteDirectory(workingDir.toFile());
|
|
|
|
spark.stop();
|
|
|
|
}
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
@Test
|
|
|
|
public void test1() throws Exception {
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
final String sourcePath = getClass()
|
|
|
|
.getResource("/eu/dnetlib/dhp/oa/graph/dump/selectrelations")
|
|
|
|
.getPath();
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
SparkSelectValidRelationsJob.main(new String[] {
|
|
|
|
"-isSparkSessionManaged", Boolean.FALSE.toString(),
|
|
|
|
"-outputPath", workingDir.toString() + "/relation",
|
|
|
|
"-sourcePath", sourcePath
|
|
|
|
});
|
2021-07-13 15:23:09 +02:00
|
|
|
|
|
|
|
// dumpCommunityProducts.exec(MOCK_IS_LOOK_UP_URL,Boolean.FALSE, workingDir.toString()+"/dataset",sourcePath,"eu.dnetlib.dhp.schema.oaf.Dataset","eu.dnetlib.dhp.schema.dump.oaf.Dataset");
|
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
JavaRDD<eu.dnetlib.dhp.schema.oaf.Relation> tmp = sc
|
|
|
|
.textFile(workingDir.toString() + "/relation")
|
|
|
|
.map(item -> OBJECT_MAPPER.readValue(item, eu.dnetlib.dhp.schema.oaf.Relation.class));
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
org.apache.spark.sql.Dataset<Relation> verificationDataset = spark
|
|
|
|
.createDataset(tmp.rdd(), Encoders.bean(eu.dnetlib.dhp.schema.oaf.Relation.class));
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
Assertions.assertTrue(verificationDataset.count() == 7);
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
}
|
2021-07-13 15:23:09 +02:00
|
|
|
|
2021-07-13 17:10:02 +02:00
|
|
|
}
|