diff --git a/pom.xml b/pom.xml index 61412fd9f2987954410ca0e90dea3f2be3015a8e..e5075ff54a36a4fa46cbd3c36878cc0d0039c0af 100644 --- a/pom.xml +++ b/pom.xml @@ -18,58 +18,84 @@ <maven.compiler.target>17</maven.compiler.target> </properties> - <dependencies> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.11</version> - <scope>test</scope> - </dependency> - </dependencies> + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.jena</groupId> + <artifactId>apache-jena-libs</artifactId> + <type>pom</type> + <version>4.2.0</version> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>2.14.1</version> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>2.14.1</version> + </dependency> + </dependencies> - <build> - <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> - <plugins> - <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> - <plugin> - <artifactId>maven-clean-plugin</artifactId> - <version>3.1.0</version> - </plugin> - <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> - <plugin> - <artifactId>maven-resources-plugin</artifactId> - <version>3.0.2</version> - </plugin> - <plugin> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.8.0</version> - </plugin> - <plugin> - <artifactId>maven-surefire-plugin</artifactId> - <version>2.22.1</version> - </plugin> - <plugin> - <artifactId>maven-jar-plugin</artifactId> - <version>3.0.2</version> - </plugin> - <plugin> - <artifactId>maven-install-plugin</artifactId> - <version>2.5.2</version> - </plugin> - <plugin> - <artifactId>maven-deploy-plugin</artifactId> - <version>2.8.2</version> - </plugin> - <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> - <plugin> - <artifactId>maven-site-plugin</artifactId> - <version>3.7.1</version> - </plugin> - <plugin> - <artifactId>maven-project-info-reports-plugin</artifactId> - <version>3.0.0</version> - </plugin> - </plugins> - </pluginManagement> - </build> + <build> + <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> + <plugins> + <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> + <plugin> + <artifactId>maven-clean-plugin</artifactId> + <version>3.1.0</version> + </plugin> + <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <version>3.0.2</version> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.0</version> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.22.1</version> + </plugin> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <version>3.0.2</version> + </plugin> + <plugin> + <artifactId>maven-install-plugin</artifactId> + <version>2.5.2</version> + </plugin> + <plugin> + <artifactId>maven-deploy-plugin</artifactId> + <version>2.8.2</version> + </plugin> + <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> + <plugin> + <artifactId>maven-site-plugin</artifactId> + <version>3.7.1</version> + </plugin> + <plugin> + <artifactId>maven-project-info-reports-plugin</artifactId> + <version>3.0.0</version> + </plugin> + </plugins> + </pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>17</source> + <target>17</target> + </configuration> + </plugin> + </plugins> + </build> </project> diff --git a/src/main/java/com/kgshape/app/Query.java b/src/main/java/com/kgshape/app/Query.java new file mode 100644 index 0000000000000000000000000000000000000000..2d6085526badc2235ce05f9ea48a1b59deed61f4 --- /dev/null +++ b/src/main/java/com/kgshape/app/Query.java @@ -0,0 +1,80 @@ +package com.kgshape.app; + +import org.apache.jena.query.*; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; +import org.apache.jena.rdf.model.ModelFactoryBase; +import org.apache.jena.riot.Lang; +import org.apache.jena.riot.RDFDataMgr; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.logging.log4j.core.config.DefaultConfiguration; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.FileSystemNotFoundException; + +/** + * Queries a remote endpoint. + */ +public class Query { + public static void main(String[] args) throws IOException { + // https://stackoverflow.com/questions/41442024/basicconfigurator-replacement-in-log4j2 + Configurator.initialize(new DefaultConfiguration()); + Configurator.setRootLevel(Level.INFO); + + // Get 20 different languages + String select_query = """ + PREFIX o: <http://dbpedia.org/ontology/> + select distinct ?subj where {?subj a o:Language} LIMIT 20 + """; + // Get 100 different actors + String construct_query = """ + PREFIX o: <http://dbpedia.org/ontology/> + CONSTRUCT {?person ?pred ?obj} + WHERE { + ?person ?pred ?obj + { + SELECT DISTINCT(?person) + WHERE { + ?person a o:Actor + } LIMIT 100 + } + } + """; + String construct_query2 = """ + PREFIX o: <http://dbpedia.org/ontology/> + PREFIX dbr: <http://dbpedia.org/resource/> + CONSTRUCT {dbr:August_Westermark ?pred ?obj} + WHERE { + dbr:August_Westermark ?pred ?obj + } + """; + // You can also open this endpoint in the browser and test the above query + String endpoint = "https://dbpedia.org/sparql"; + + Model fetchedModel = fetchConstruct(endpoint, construct_query); + writeModel(fetchedModel, "rdf_file.ttl"); + } + + public static Model fetchConstruct(String endpoint, String query) { + // https://jena.apache.org/documentation/query/sparql-remote.html + // Also see: https://jena.apache.org/documentation/query/app_api.html + QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, query); + Model resultModel = qexec.execConstruct(); + qexec.close(); + return resultModel; + } + + public static void writeModel(Model model, String filename) throws FileSystemNotFoundException, IOException { +// RDFDataMgr.write(System.out, model, Lang.TURTLE); // Write it to stdout + + try (FileOutputStream fos = new FileOutputStream(filename)) { + // See: https://jena.apache.org/documentation/io/rdf-output.html#opt-turtle-trig + RDFDataMgr.write(fos, model, Lang.TURTLE); + } catch (Exception e) { + throw e; + } + } +}