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

Cleanup fetching/writing.

parent ad4be187
No related branches found
No related tags found
No related merge requests found
...@@ -13,12 +13,13 @@ import org.apache.logging.log4j.core.config.DefaultConfiguration; ...@@ -13,12 +13,13 @@ import org.apache.logging.log4j.core.config.DefaultConfiguration;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileSystemNotFoundException;
/** /**
* See: https://jena.apache.org/documentation/query/app_api.html * Queries a remote endpoint.
*/ */
public class Query { public class Query {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
// https://stackoverflow.com/questions/41442024/basicconfigurator-replacement-in-log4j2 // https://stackoverflow.com/questions/41442024/basicconfigurator-replacement-in-log4j2
Configurator.initialize(new DefaultConfiguration()); Configurator.initialize(new DefaultConfiguration());
Configurator.setRootLevel(Level.INFO); Configurator.setRootLevel(Level.INFO);
...@@ -45,23 +46,27 @@ public class Query { ...@@ -45,23 +46,27 @@ public class Query {
// You can also open this endpoint in the browser and test the above query // You can also open this endpoint in the browser and test the above query
String endpoint = "https://dbpedia.org/sparql"; 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 // https://jena.apache.org/documentation/query/sparql-remote.html
QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, construct_query); // Also see: https://jena.apache.org/documentation/query/app_api.html
QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, query);
Model resultModel = qexec.execConstruct(); Model resultModel = qexec.execConstruct();
qexec.close();
return resultModel;
}
// RDFDataMgr.write(System.out, resultModel, Lang.TURTLE); public static void writeModel(Model model, String filename) throws FileSystemNotFoundException, IOException {
// RDFDataMgr.write(System.out, resultModel, Lang.TURTLE); // Write it to stdout
try (FileOutputStream fos = new FileOutputStream("rdf.ttl")) { try (FileOutputStream fos = new FileOutputStream(filename)) {
// See: https://jena.apache.org/documentation/io/rdf-output.html#opt-turtle-trig // See: https://jena.apache.org/documentation/io/rdf-output.html#opt-turtle-trig
RDFDataMgr.write(fos, resultModel, Lang.TURTLE); RDFDataMgr.write(fos, model, Lang.TURTLE);
} catch (FileNotFoundException e) { } catch (Exception e) {
e.printStackTrace(); throw e;
} catch (IOException e) {
e.printStackTrace();
} }
qexec.close();
System.out.println("Hello Query!");
} }
} }
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