2021-01-11 14:39:47 +01:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.actionmanager.bipfinder;
|
|
|
|
|
2021-08-11 12:13:22 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
2021-01-11 14:39:47 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
2023-08-11 14:56:53 +02:00
|
|
|
import javax.xml.crypto.Data;
|
|
|
|
|
2021-01-11 14:39:47 +01:00
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.apache.hadoop.io.Text;
|
|
|
|
import org.apache.spark.SparkConf;
|
|
|
|
import org.apache.spark.api.java.JavaRDD;
|
|
|
|
import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
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.action.AtomicAction;
|
2023-07-21 14:26:50 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.KeyValue;
|
2023-08-11 14:56:53 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.OafEntity;
|
2023-07-21 14:26:50 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Project;
|
2021-12-14 11:12:17 +01:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.Result;
|
2021-01-11 14:39:47 +01:00
|
|
|
|
|
|
|
public class SparkAtomicActionScoreJobTest {
|
|
|
|
|
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
|
|
|
|
private static SparkSession spark;
|
|
|
|
|
|
|
|
private static Path workingDir;
|
2023-04-26 19:23:46 +02:00
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(SparkAtomicActionScoreJobTest.class);
|
2021-01-11 14:39:47 +01:00
|
|
|
|
|
|
|
@BeforeAll
|
|
|
|
public static void beforeAll() throws IOException {
|
|
|
|
workingDir = Files
|
|
|
|
.createTempDirectory(SparkAtomicActionScoreJobTest.class.getSimpleName());
|
|
|
|
log.info("using work dir {}", workingDir);
|
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
conf.setAppName(SparkAtomicActionScoreJobTest.class.getSimpleName());
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
spark = SparkSession
|
|
|
|
.builder()
|
|
|
|
.appName(SparkAtomicActionScoreJobTest.class.getSimpleName())
|
|
|
|
.config(conf)
|
|
|
|
.getOrCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@AfterAll
|
|
|
|
public static void afterAll() throws IOException {
|
|
|
|
FileUtils.deleteDirectory(workingDir.toFile());
|
|
|
|
spark.stop();
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:56:53 +02:00
|
|
|
private void runJob(String resultsInputPath, String projectsInputPath, String outputPath) throws Exception {
|
2023-07-21 14:26:50 +02:00
|
|
|
SparkAtomicActionScoreJob
|
|
|
|
.main(
|
|
|
|
new String[] {
|
2023-04-26 19:23:46 +02:00
|
|
|
"-isSparkSessionManaged", Boolean.FALSE.toString(),
|
2023-08-11 14:56:53 +02:00
|
|
|
"-resultsInputPath", resultsInputPath,
|
|
|
|
"-projectsInputPath", projectsInputPath,
|
2023-04-26 19:23:46 +02:00
|
|
|
"-outputPath", outputPath,
|
2023-07-21 14:26:50 +02:00
|
|
|
});
|
2023-04-26 19:23:46 +02:00
|
|
|
}
|
2023-07-21 14:26:50 +02:00
|
|
|
|
2021-01-11 14:39:47 +01:00
|
|
|
@Test
|
2023-08-11 14:56:53 +02:00
|
|
|
void testScores() throws Exception {
|
|
|
|
|
|
|
|
String resultsInputPath = getClass()
|
2023-04-26 19:23:46 +02:00
|
|
|
.getResource("/eu/dnetlib/dhp/actionmanager/bipfinder/result_bip_scores.json")
|
2021-01-11 14:39:47 +01:00
|
|
|
.getPath();
|
2023-08-11 14:56:53 +02:00
|
|
|
|
|
|
|
String projectsInputPath = getClass()
|
|
|
|
.getResource("/eu/dnetlib/dhp/actionmanager/bipfinder/project_bip_scores.json")
|
|
|
|
.getPath();
|
|
|
|
|
|
|
|
String outputPath = workingDir.toString() + "/actionSet";
|
2021-01-11 14:39:47 +01:00
|
|
|
|
2023-04-26 19:23:46 +02:00
|
|
|
// execute the job to generate the action sets for result scores
|
2023-08-11 14:56:53 +02:00
|
|
|
runJob(resultsInputPath, projectsInputPath, outputPath);
|
2021-01-11 14:39:47 +01:00
|
|
|
|
|
|
|
final JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
|
|
|
|
|
2023-08-11 14:56:53 +02:00
|
|
|
JavaRDD<OafEntity> tmp = sc
|
2023-04-26 19:23:46 +02:00
|
|
|
.sequenceFile(outputPath, Text.class, Text.class)
|
2021-01-11 14:39:47 +01:00
|
|
|
.map(value -> OBJECT_MAPPER.readValue(value._2().toString(), AtomicAction.class))
|
2023-08-11 14:56:53 +02:00
|
|
|
.map(aa -> ((OafEntity) aa.getPayload()));
|
2021-01-11 14:39:47 +01:00
|
|
|
|
2023-08-11 14:56:53 +02:00
|
|
|
assertEquals(8, tmp.count());
|
2021-01-11 14:39:47 +01:00
|
|
|
|
2023-08-11 14:56:53 +02:00
|
|
|
Dataset<OafEntity> verificationDataset = spark.createDataset(tmp.rdd(), Encoders.bean(OafEntity.class));
|
2021-12-14 11:12:17 +01:00
|
|
|
verificationDataset.createOrReplaceTempView("result");
|
2021-01-11 14:39:47 +01:00
|
|
|
|
2023-08-11 14:56:53 +02:00
|
|
|
Dataset<Row> testDataset = spark
|
2021-01-11 14:39:47 +01:00
|
|
|
.sql(
|
2021-12-14 11:12:17 +01:00
|
|
|
"Select p.id oaid, mes.id, mUnit.value from result p " +
|
2021-01-11 14:39:47 +01:00
|
|
|
"lateral view explode(measures) m as mes " +
|
|
|
|
"lateral view explode(mes.unit) u as mUnit ");
|
|
|
|
|
2023-08-11 14:56:53 +02:00
|
|
|
// execVerification.show();
|
|
|
|
|
|
|
|
Assertions.assertEquals(28, testDataset.count());
|
|
|
|
|
|
|
|
assertResultImpactScores(testDataset);
|
|
|
|
assertProjectImpactScores(testDataset);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void assertResultImpactScores(Dataset<Row> testDataset) {
|
2021-01-11 14:39:47 +01:00
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
2023-08-11 14:56:53 +02:00
|
|
|
"6.63451994567e-09", testDataset
|
2021-01-11 14:39:47 +01:00
|
|
|
.filter(
|
2021-12-14 11:12:17 +01:00
|
|
|
"oaid='50|arXiv_dedup_::4a2d5fd8d71daec016c176ec71d957b1' " +
|
2021-01-11 14:39:47 +01:00
|
|
|
"and id = 'influence'")
|
|
|
|
.select("value")
|
|
|
|
.collectAsList()
|
|
|
|
.get(0)
|
|
|
|
.getString(0));
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
2023-08-11 14:56:53 +02:00
|
|
|
"0.348694533145", testDataset
|
2021-01-11 14:39:47 +01:00
|
|
|
.filter(
|
2021-12-14 11:12:17 +01:00
|
|
|
"oaid='50|arXiv_dedup_::4a2d5fd8d71daec016c176ec71d957b1' " +
|
|
|
|
"and id = 'popularity_alt'")
|
2021-01-11 14:39:47 +01:00
|
|
|
.select("value")
|
|
|
|
.collectAsList()
|
|
|
|
.get(0)
|
|
|
|
.getString(0));
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
2023-08-11 14:56:53 +02:00
|
|
|
"2.16094680115e-09", testDataset
|
2021-01-11 14:39:47 +01:00
|
|
|
.filter(
|
2021-12-14 11:12:17 +01:00
|
|
|
"oaid='50|arXiv_dedup_::4a2d5fd8d71daec016c176ec71d957b1' " +
|
2021-01-11 14:39:47 +01:00
|
|
|
"and id = 'popularity'")
|
|
|
|
.select("value")
|
|
|
|
.collectAsList()
|
|
|
|
.get(0)
|
|
|
|
.getString(0));
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:56:53 +02:00
|
|
|
void assertProjectImpactScores(Dataset<Row> testDataset) throws Exception {
|
2023-04-26 19:23:46 +02:00
|
|
|
|
2023-08-11 14:56:53 +02:00
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
"0", testDataset
|
|
|
|
.filter(
|
|
|
|
"oaid='40|nih_________::c02a8233e9b60f05bb418f0c9b714833' " +
|
|
|
|
"and id = 'numOfInfluentialResults'")
|
|
|
|
.select("value")
|
|
|
|
.collectAsList()
|
|
|
|
.get(0)
|
|
|
|
.getString(0));
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
"1", testDataset
|
|
|
|
.filter(
|
|
|
|
"oaid='40|nih_________::c02a8233e9b60f05bb418f0c9b714833' " +
|
|
|
|
"and id = 'numOfPopularResults'")
|
|
|
|
.select("value")
|
|
|
|
.collectAsList()
|
|
|
|
.get(0)
|
|
|
|
.getString(0));
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
"25", testDataset
|
|
|
|
.filter(
|
|
|
|
"oaid='40|nih_________::c02a8233e9b60f05bb418f0c9b714833' " +
|
|
|
|
"and id = 'totalImpulse'")
|
|
|
|
.select("value")
|
|
|
|
.collectAsList()
|
|
|
|
.get(0)
|
|
|
|
.getString(0));
|
|
|
|
Assertions
|
|
|
|
.assertEquals(
|
|
|
|
"43", testDataset
|
|
|
|
.filter(
|
|
|
|
"oaid='40|nih_________::c02a8233e9b60f05bb418f0c9b714833' " +
|
|
|
|
"and id = 'totalCitationCount'")
|
|
|
|
.select("value")
|
|
|
|
.collectAsList()
|
|
|
|
.get(0)
|
|
|
|
.getString(0));
|
2023-04-26 19:23:46 +02:00
|
|
|
}
|
2021-01-11 14:39:47 +01:00
|
|
|
}
|