How do you create a Fuseki SPARQL server using the Apache Jena Java API?

3.7k views Asked by At

I am trying to create a Fuseki SPARQL server on my machine. The documentation on the Jena website describes how to create such a server from the command-line, here: http://jena.apache.org/documentation/serving_data/. I am looking for a way of creating and initializing such a server just using the Jena Java API. I have looked over the Jena API but have not made any progress in working out how to proceed. Has anyone done this before?

3

There are 3 answers

2
RobV On BEST ANSWER

Yes this is possible but it is not how Fuseki was designed to operate so do so at your own risk.

You will need to pull in Fuseki as a dependency, via maven this would be the following:

<dependency>
  <groupId>org.apache.jena</groupId>
  <artifactId>jena-fuseki</artifactId>
  <version>0.2.7</version>
</dependency>

Then you can use the SPARQLServer class to create a server and call start() to actually run the server and stop() when you are done. (This is located in the org.apache.jena.fuseki.server package)

It is important to note that if you do this the server is embedded in the JVM from which you start it, when that JVM shuts down the server shuts down. This may be your intention but it may not.

Btw you question is unclear as to exactly why you want to do this? There may be alternative ways to achieve your goal without embedding Fuseki if you explain your goals further

0
user0221441 On

The answers here are quite old and I am afraid they would yield results anymore with the more recent versions of the Jena API.

//Creating a persistent triple store (Jena TDB)
String directory = "C:\\Users\\..."; // Directory where you want to create the triplestore
Dataset dataset = TDBFactory.createDataset(directory);

//Loading an ontology stored on the disk
String ontPath = "C:\\Users... ...product.owl"; //Directory of your RDF/OWL file
Model KB = RDFDataMgr.loadModel(ontPath);
KB.write(System.out, "RDF/XML"); // (optional) checking what you have loaded in the console
dataset.addNamedModel("https://joedavid91.github.io/ontologies/camo/product",KB);

// Starting the fuseki server
FusekiServer fusekiServer = FusekiServer.create()
            .port(3001)
            .add("/ds", dataset, true)
            .build();

fusekiServer.start();

Now lets try to fetch some results using a REST client (e.g POSTMAN) to see if this works. You can see that it indeed does. Make sure your follow the SPARQL Protocol for the REST request.

Results from Jena Fuseki via POSTMAN

0
Julian Dehne On

If you use

<dependency>
  <groupId>org.apache.jena</groupId>
  <artifactId>apache-jena-libs</artifactId>
  <type>pom</type>
  <version>2.11.2-SNAPSHOT</version>
</dependency>

<dependency>
  <groupId>org.apache.jena</groupId>
  <artifactId>jena-fuseki</artifactId>
  <version>1.0.2-SNAPSHOT</version>
</dependency>

You can start an embedded Server.

Dataset dataset = TDBFactory.createDataset(MagicStrings.TDBLocation);
dataset.begin(ReadWrite.WRITE);
Model tdb = dataset.getDefaultModel();
EmbeddedFusekiServer server = EmbeddedFusekiServer.
create(3030,getDataset().asDatasetGraph(), "comp");