Skip to content
Snippets Groups Projects
Commit f7d7d003 authored by Valerian Wintner's avatar Valerian Wintner
Browse files

Merge branch 'valerian-step1-week1-query'

# Conflicts:
#	.gitignore
#	pom.xml
parents 6e2ed060 01015dbe
No related branches found
No related tags found
No related merge requests found
...@@ -18,58 +18,84 @@ ...@@ -18,58 +18,84 @@
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.11</version> <version>4.11</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> <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> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins> <plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin> <plugin>
<artifactId>maven-clean-plugin</artifactId> <artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version> <version>3.1.0</version>
</plugin> </plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin> <plugin>
<artifactId>maven-resources-plugin</artifactId> <artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version> <version>3.0.2</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version> <version>3.8.0</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version> <version>2.22.1</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version> <version>3.0.2</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-install-plugin</artifactId> <artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version> <version>2.5.2</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version> <version>2.8.2</version>
</plugin> </plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin> <plugin>
<artifactId>maven-site-plugin</artifactId> <artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version> <version>3.7.1</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-project-info-reports-plugin</artifactId> <artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version> <version>3.0.0</version>
</plugin> </plugin>
</plugins> </plugins>
</pluginManagement> </pluginManagement>
</build> <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> </project>
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;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment