2020-07-29 17:43:24 +02:00
|
|
|
|
2020-11-05 17:38:18 +01:00
|
|
|
package eu.dnetlib.dhp.oa.graph.dump.complete;
|
2020-07-24 17:21:48 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2021-12-02 14:09:46 +01:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2021-12-02 14:09:46 +01:00
|
|
|
|
2020-07-29 17:32:37 +02:00
|
|
|
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;
|
2021-08-06 18:56:18 +02:00
|
|
|
import org.apache.spark.api.java.function.ForeachFunction;
|
2020-07-29 17:32:37 +02:00
|
|
|
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;
|
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.schema.dump.oaf.graph.Relation;
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-24 17:21:48 +02:00
|
|
|
public class DumpRelationTest {
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
private static SparkSession spark;
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
private static Path workingDir;
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
private static final Logger log = LoggerFactory
|
|
|
|
.getLogger(DumpRelationTest.class);
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2021-05-14 10:58:12 +02:00
|
|
|
private static final HashMap<String, String> map = new HashMap<>();
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
@BeforeAll
|
|
|
|
public static void beforeAll() throws IOException {
|
|
|
|
workingDir = Files
|
|
|
|
.createTempDirectory(DumpRelationTest.class.getSimpleName());
|
|
|
|
log.info("using work dir {}", workingDir);
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.setAppName(DumpRelationTest.class.getSimpleName());
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +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());
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
spark = SparkSession
|
|
|
|
.builder()
|
|
|
|
.appName(DumpRelationTest.class.getSimpleName())
|
|
|
|
.config(conf)
|
|
|
|
.getOrCreate();
|
|
|
|
}
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
@AfterAll
|
|
|
|
public static void afterAll() throws IOException {
|
|
|
|
FileUtils.deleteDirectory(workingDir.toFile());
|
|
|
|
spark.stop();
|
|
|
|
}
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
@Test
|
|
|
|
public void test1() throws Exception {
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
final String sourcePath = getClass()
|
2021-08-06 18:56:18 +02:00
|
|
|
.getResource("/eu/dnetlib/dhp/oa/graph/dump/relation/relation")
|
2020-07-29 17:43:24 +02:00
|
|
|
.getPath();
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
SparkDumpRelationJob.main(new String[] {
|
|
|
|
"-isSparkSessionManaged", Boolean.FALSE.toString(),
|
|
|
|
"-outputPath", workingDir.toString() + "/relation",
|
|
|
|
"-sourcePath", sourcePath
|
|
|
|
});
|
2020-07-29 17:32:37 +02:00
|
|
|
|
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
|
|
|
|
|
|
|
JavaRDD<Relation> tmp = sc
|
|
|
|
.textFile(workingDir.toString() + "/relation")
|
|
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Relation.class));
|
|
|
|
|
|
|
|
org.apache.spark.sql.Dataset<Relation> verificationDataset = spark
|
|
|
|
.createDataset(tmp.rdd(), Encoders.bean(Relation.class));
|
|
|
|
|
|
|
|
verificationDataset.createOrReplaceTempView("table");
|
|
|
|
|
2021-10-01 12:59:47 +02:00
|
|
|
verificationDataset
|
|
|
|
.foreach((ForeachFunction<Relation>) r -> System.out.println(new ObjectMapper().writeValueAsString(r)));
|
2021-08-06 18:56:18 +02:00
|
|
|
|
2020-07-29 17:43:24 +02:00
|
|
|
Dataset<Row> check = spark
|
|
|
|
.sql(
|
|
|
|
"SELECT reltype.name, source.id source, source.type stype, target.id target,target.type ttype, provenance.provenance "
|
|
|
|
+
|
|
|
|
"from table ");
|
|
|
|
|
|
|
|
Assertions.assertEquals(22, check.filter("name = 'isProvidedBy'").count());
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
22, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isProvidedBy' and stype = 'datasource' and ttype = 'organization' and " +
|
|
|
|
"provenance = 'Harvested'")
|
|
|
|
.count());
|
|
|
|
|
|
|
|
Assertions.assertEquals(7, check.filter("name = 'isParticipant'").count());
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
7, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isParticipant' and stype = 'organization' and ttype = 'project' " +
|
|
|
|
"and provenance = 'Harvested'")
|
|
|
|
.count());
|
|
|
|
|
|
|
|
Assertions.assertEquals(1, check.filter("name = 'isAuthorInstitutionOf'").count());
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
1, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isAuthorInstitutionOf' and stype = 'organization' and ttype = 'result' " +
|
|
|
|
"and provenance = 'Inferred by OpenAIRE'")
|
|
|
|
.count());
|
|
|
|
}
|
2020-07-29 17:32:37 +02:00
|
|
|
|
2021-08-06 18:56:18 +02:00
|
|
|
@Test
|
|
|
|
public void test2() throws Exception {
|
|
|
|
|
|
|
|
final String sourcePath = getClass()
|
2021-10-01 12:59:47 +02:00
|
|
|
.getResource("/eu/dnetlib/dhp/oa/graph/dump/relation/relation_validated")
|
|
|
|
.getPath();
|
2021-08-06 18:56:18 +02:00
|
|
|
|
|
|
|
SparkDumpRelationJob.main(new String[] {
|
2021-10-01 12:59:47 +02:00
|
|
|
"-isSparkSessionManaged", Boolean.FALSE.toString(),
|
|
|
|
"-outputPath", workingDir.toString() + "/relation",
|
|
|
|
"-sourcePath", sourcePath
|
2021-08-06 18:56:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
|
|
|
|
|
|
|
JavaRDD<Relation> tmp = sc
|
2021-10-01 12:59:47 +02:00
|
|
|
.textFile(workingDir.toString() + "/relation")
|
|
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Relation.class));
|
2021-08-06 18:56:18 +02:00
|
|
|
|
|
|
|
org.apache.spark.sql.Dataset<Relation> verificationDataset = spark
|
2021-10-01 12:59:47 +02:00
|
|
|
.createDataset(tmp.rdd(), Encoders.bean(Relation.class));
|
2021-08-06 18:56:18 +02:00
|
|
|
|
|
|
|
verificationDataset.createOrReplaceTempView("table");
|
|
|
|
|
2021-10-01 12:59:47 +02:00
|
|
|
verificationDataset
|
|
|
|
.foreach((ForeachFunction<Relation>) r -> System.out.println(new ObjectMapper().writeValueAsString(r)));
|
2021-08-06 18:56:18 +02:00
|
|
|
|
|
|
|
Dataset<Row> check = spark
|
2021-10-01 12:59:47 +02:00
|
|
|
.sql(
|
|
|
|
"SELECT reltype.name, source.id source, source.type stype, target.id target,target.type ttype, provenance.provenance "
|
|
|
|
+
|
|
|
|
"from table ");
|
2021-08-06 18:56:18 +02:00
|
|
|
|
|
|
|
Assertions.assertEquals(20, check.filter("name = 'isProvidedBy'").count());
|
|
|
|
Assertions
|
2021-10-01 12:59:47 +02:00
|
|
|
.assertEquals(
|
|
|
|
20, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isProvidedBy' and stype = 'datasource' and ttype = 'organization' and " +
|
|
|
|
"provenance = 'Harvested'")
|
|
|
|
.count());
|
2021-08-06 18:56:18 +02:00
|
|
|
|
|
|
|
Assertions.assertEquals(7, check.filter("name = 'isParticipant'").count());
|
|
|
|
Assertions
|
2021-10-01 12:59:47 +02:00
|
|
|
.assertEquals(
|
|
|
|
7, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isParticipant' and stype = 'organization' and ttype = 'project' " +
|
|
|
|
"and provenance = 'Harvested'")
|
|
|
|
.count());
|
2021-08-06 18:56:18 +02:00
|
|
|
|
|
|
|
Assertions.assertEquals(1, check.filter("name = 'isAuthorInstitutionOf'").count());
|
|
|
|
Assertions
|
2021-10-01 12:59:47 +02:00
|
|
|
.assertEquals(
|
|
|
|
1, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isAuthorInstitutionOf' and stype = 'organization' and ttype = 'result' " +
|
|
|
|
"and provenance = 'Inferred by OpenAIRE'")
|
|
|
|
.count());
|
2021-08-06 18:56:18 +02:00
|
|
|
|
|
|
|
Assertions.assertEquals(2, check.filter("name = 'isProducedBy'").count());
|
|
|
|
Assertions
|
2021-10-01 12:59:47 +02:00
|
|
|
.assertEquals(
|
|
|
|
2, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isProducedBy' and stype = 'project' and ttype = 'result' " +
|
|
|
|
"and provenance = 'Harvested' and validated = true " +
|
|
|
|
"and validationDate = '2021-08-06'")
|
|
|
|
.count());
|
2021-08-06 18:56:18 +02:00
|
|
|
}
|
2021-12-02 14:09:46 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test3() throws Exception {//
|
|
|
|
final String sourcePath = getClass()
|
|
|
|
.getResource("/eu/dnetlib/dhp/oa/graph/dump/relation/relation")
|
|
|
|
.getPath();
|
|
|
|
|
|
|
|
SparkDumpRelationJob.main(new String[] {
|
|
|
|
"-isSparkSessionManaged", Boolean.FALSE.toString(),
|
|
|
|
"-outputPath", workingDir.toString() + "/relation",
|
|
|
|
"-sourcePath", sourcePath,
|
|
|
|
"-removeSet", "isParticipant"
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
|
|
|
|
|
|
|
JavaRDD<Relation> tmp = sc
|
|
|
|
.textFile(workingDir.toString() + "/relation")
|
|
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Relation.class));
|
|
|
|
|
|
|
|
org.apache.spark.sql.Dataset<Relation> verificationDataset = spark
|
|
|
|
.createDataset(tmp.rdd(), Encoders.bean(Relation.class));
|
|
|
|
|
|
|
|
verificationDataset.createOrReplaceTempView("table");
|
|
|
|
|
|
|
|
verificationDataset
|
|
|
|
.foreach((ForeachFunction<Relation>) r -> System.out.println(new ObjectMapper().writeValueAsString(r)));
|
|
|
|
|
|
|
|
Dataset<Row> check = spark
|
|
|
|
.sql(
|
|
|
|
"SELECT reltype.name, source.id source, source.type stype, target.id target,target.type ttype, provenance.provenance "
|
|
|
|
+
|
|
|
|
"from table ");
|
|
|
|
|
|
|
|
Assertions.assertEquals(22, check.filter("name = 'isProvidedBy'").count());
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
22, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isProvidedBy' and stype = 'datasource' and ttype = 'organization' and " +
|
|
|
|
"provenance = 'Harvested'")
|
|
|
|
.count());
|
|
|
|
|
|
|
|
Assertions.assertEquals(0, check.filter("name = 'isParticipant'").count());
|
|
|
|
|
|
|
|
|
|
|
|
Assertions.assertEquals(1, check.filter("name = 'isAuthorInstitutionOf'").count());
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
1, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isAuthorInstitutionOf' and stype = 'organization' and ttype = 'result' " +
|
|
|
|
"and provenance = 'Inferred by OpenAIRE'")
|
|
|
|
.count());
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:15:19 +01:00
|
|
|
@Test
|
|
|
|
public void test4() throws Exception {//
|
|
|
|
final String sourcePath = getClass()
|
|
|
|
.getResource("/eu/dnetlib/dhp/oa/graph/dump/relation/relation")
|
|
|
|
.getPath();
|
|
|
|
|
|
|
|
SparkDumpRelationJob.main(new String[] {
|
|
|
|
"-isSparkSessionManaged", Boolean.FALSE.toString(),
|
|
|
|
"-outputPath", workingDir.toString() + "/relation",
|
|
|
|
"-sourcePath", sourcePath,
|
|
|
|
"-removeSet", "isParticipant;isAuthorInstitutionOf"
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
final JavaSparkContext sc = JavaSparkContext.fromSparkContext(spark.sparkContext());
|
|
|
|
|
|
|
|
JavaRDD<Relation> tmp = sc
|
|
|
|
.textFile(workingDir.toString() + "/relation")
|
|
|
|
.map(item -> OBJECT_MAPPER.readValue(item, Relation.class));
|
|
|
|
|
|
|
|
org.apache.spark.sql.Dataset<Relation> verificationDataset = spark
|
|
|
|
.createDataset(tmp.rdd(), Encoders.bean(Relation.class));
|
|
|
|
|
|
|
|
verificationDataset.createOrReplaceTempView("table");
|
|
|
|
|
|
|
|
verificationDataset
|
|
|
|
.foreach((ForeachFunction<Relation>) r -> System.out.println(new ObjectMapper().writeValueAsString(r)));
|
|
|
|
|
|
|
|
Dataset<Row> check = spark
|
|
|
|
.sql(
|
|
|
|
"SELECT reltype.name, source.id source, source.type stype, target.id target,target.type ttype, provenance.provenance "
|
|
|
|
+
|
|
|
|
"from table ");
|
|
|
|
|
|
|
|
Assertions.assertEquals(22, check.filter("name = 'isProvidedBy'").count());
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
22, check
|
|
|
|
.filter(
|
|
|
|
"name = 'isProvidedBy' and stype = 'datasource' and ttype = 'organization' and " +
|
|
|
|
"provenance = 'Harvested'")
|
|
|
|
.count());
|
|
|
|
|
|
|
|
Assertions.assertEquals(0, check.filter("name = 'isParticipant'").count());
|
|
|
|
|
|
|
|
|
|
|
|
Assertions.assertEquals(0, check.filter("name = 'isAuthorInstitutionOf'").count());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-24 17:21:48 +02:00
|
|
|
}
|