Skip to content

Instantly share code, notes, and snippets.

@mildred
Forked from anonymous/README.md
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildred/3dd5ada5f2c2b4a39f7c to your computer and use it in GitHub Desktop.
Save mildred/3dd5ada5f2c2b4a39f7c to your computer and use it in GitHub Desktop.
Getting started with a SPARQL server

Getting started with a SPARQL server

OpenRDF Sesame

Sesame is a J2EE application that is contained in a servlet. This is generally deployed using Tomcat, but we are not going to use that or anything bloatware. Instead we will make use of Winstone, a lightweight application server.

Sesame is composed of two servlets. openrdf-sesame that is best served at /openrdf-sesame and that contain the HTTP REST API to the RDF store. Then there is openrdf-workbench (served from /openrdf-workbench) that is the UI to the server. You don't really need the UI but it's nice to get started and explore the server.

Running the server

  • First thing, download OpenRDF Sesame SDK

  • Uncompress the downloaded archive. You should have a war directory containing:

      openrdf-sesame-2.8.3/war/openrdf-sesame.war
      openrdf-sesame-2.8.3/war/openrdf-workbench.war
    
  • Download winstone-boot.jar from the maven central. For those who don't know, maven is to Java what gem is for Ruby and npm for Node.

  • Execute winstone with the two servlets:

      java \
          -Dinfo.aduna.platform.appdata.basedir=/var/lib/openrdf \
          -jar .../winstone-boot-1.7.0.jar \
          --webappsDir=.../openrdf-sesame-2.8.3/war \
          --port=8080
    

You can now connect to http://localhost:8080/openrdf-workbench. Repositories are stored at /var/lib/openrdf (choose other location if you like).

You can also execute winstone with just openrdf-sesame without the workbench:

java \
    -Dinfo.aduna.platform.appdata.basedir=/var/lib/openrdf \
    -jar .../winstone-boot-1.7.0.jar \
    --warfile=.../openrdf-sesame-2.8.3/war/openrdf-sesame.war \
    --port=8080

Then, the SPARQL endpoint location will slightly change from http://localhost:8080/openrdf-sesame/repositories/SYSTEM to http://localhost:8080/repositories/SYSTEM. The same is true for the other endpoints.

Creating the datastore

You have to create the datastore first. You can use the WorkBench, but I ran into bug SES-2240. It is also possible to use plain HTTP API calls using curl as explained in this mail.

Start by describing your datastore in a RDF file (in this example, smartweb.trig):

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix rep: <http://www.openrdf.org/config/repository#>.
@prefix sr: <http://www.openrdf.org/config/repository/sail#>.
@prefix sail: <http://www.openrdf.org/config/sail#>.
@prefix sys: <http://www.openrdf.org/config/repository#> .
@prefix ms: <http://www.openrdf.org/config/sail/memory#>.

_:node001 {
 [] a rep:Repository ;
   rep:repositoryID "smartweb" ;
   rdfs:label "SmartWeb Store" ;
   rep:repositoryImpl [
      rep:repositoryType "openrdf:SailRepository" ;
      sr:sailImpl [
         sail:sailType "openrdf:MemoryStore" ;
         ms:persist true ;
         ms:syncDelay 0
      ]
   ].
}
{
 _:node001 a sys:RepositoryContext .
} 

Then, add this to the SYSTEM datastore:

curl -v \
    -X POST \
    -d @smartweb.trig \
    -H "Content-Type: application/x-trig;charset=UTF-8" \
    http://localhost:8080/openrdf-sesame/repositories/SYSTEM/statements

See user documentation "Repository configuration templates".

If you want to create a native repository instead of a momery repository, use this template (native.ttl):

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix rep: <http://www.openrdf.org/config/repository#>.
@prefix sr: <http://www.openrdf.org/config/repository/sail#>.
@prefix sail: <http://www.openrdf.org/config/sail#>.
@prefix ns: <http://www.openrdf.org/config/sail/native#>.

[] a rep:Repository ;
   rep:repositoryID "native" ;
   rdfs:label "Native store" ;
   rep:repositoryImpl [
      rep:repositoryType "openrdf:SailRepository" ;
      sr:sailImpl [
         sail:sailType "openrdf:NativeStore" ;
         ns:tripleIndexes "spoc,posc"
      ]
   ].

Found on answers.semanticweb.com.

See also Create/Delete a Sesame Repository via HTTP (web archive link)

A Sesame 2 server keeps its repository configuration data in a seperate RDF repository called SYSTEM. At the protocol level, creating a new repository consists of simply adding RDF data to this SYSTEM repository - so this is why you don't see separate operations in the HTTP protocol.

The format for the RDF config file is loosely described with examples in the user documentation. You can find additional examples in the Sesame SVN [svn ls http://repo.aduna-software.org/svn/org.openrdf/sesame/tags/2.6.9/core/console/src/main/resources/org/openrdf/console].

Note that the format is decribed in terms of templates. The reason for this is that the Sesame command line console and the Workbench use these RDF templates to provide configuration settings for typical types of repository, where the only data required to be filled in by the user are things like the repository name and optional config parameters (index settings, etc.).

When working from Java code, by the way, typically users are not expected to 'manually' upload such RDF config files to the SYSTEM repository, but instead can use the RepositoryManager functionality.

also:

the recommended way to remove a repository is to issue a HTTP DELETE on the actual repository URI (see www.openrdf.org/doc/sesame2/system/ch08.html#d0e320 ).

Playing with your server

Assuming you've installed sparql-query, you can have:

$ sparql-query http://localhost:8080/openrdf-sesame/repositories/SYSTEM 'SELECT *  WHERE { ?s ?p ?o } LIMIT 10'
┌───────────────────┬───────────────────────────────────────────────────────────┬──────────────────────────────────────────────────────────────┐
│ ?s                │ ?p                                                        │ ?o                                                           │
├───────────────────┼───────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ _:node19l60umrcx1 │ <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>         │ <http://www.openrdf.org/config/repository#RepositoryContext> │
│ _:node19l60umrcx2 │ <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>         │ <http://www.openrdf.org/config/repository#Repository>        │
│ _:node19l60umrcx2 │ <http://www.openrdf.org/config/repository#repositoryID>   │ SYSTEM                                                       │
│ _:node19l60umrcx2 │ <http://www.w3.org/2000/01/rdf-schema#label>              │ System configuration repository                              │
│ _:node19l60umrcx3 │ <http://www.openrdf.org/config/repository#repositoryType> │ openrdf:SystemRepository                                     │
│ _:node19l60umrcx2 │ <http://www.openrdf.org/config/repository#repositoryImpl> │ _:node19l60umrcx3                                            │
└───────────────────┴───────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────┘

First, you need to go to the workbench, click on the SYSTEM repository and copy the URL. You can then run queries on this URL using sparql-query.

You can also play with curl:

curl -v \
  -H 'Accept: text/turtle' \
  --data-urlencode 'query=CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o } LIMIT 10' \
  http://localhost:8080/openrdf-sesame/repositories/SYSTEM

curl -v \
  -H 'Accept: application/sparql-results+json' \
  --data-urlencode 'query=SELECT * WHERE { ?s ?p ?o } LIMIT 10' \
  http://localhost:8080/openrdf-sesame/repositories/SYSTEM

Read the SPARQL 1.1 W3C Recommendation to understand the HTTP query parameters

4store

After installing 4store, nothing is easier. You first have to create the store and dive it a name (the kbname):

4s-backend-setup *store_name*

Then, you have to run a backend (or multiple backends if you want to run in cluster mode), and then the http frontend:

4s-backend -D -l 2.0 *store_name*
4s-httpd -X -D -p 8008 *store_name*

One neat thing about 4store is access control, you can grant access to some graphs to some users and not other graphs. The queries can also be limited to prevent loading the backend too much by accident.

Something less inviting about 4store is that stores are in /var/lib/4store and that can't be changed, unless at compile time (or for the latest version, 1.1.6 onwards). But that's a write by all directory with the sticky bit set (by default). Also, the backend seems to leak to the local network through mDNS.

Also, 4store doesn"t support RDF Schema (reasoning on top of RDF) and is not fully compatible with SPARQL 1.1. In particular SPARQL Query Paths. This can be limiting for some applications.

Run queries

You can now run queries:

$ sparql-update http://localhost:8008/update/ 'INSERT DATA { GRAPH <system:config> {
  <graph:x> <http://4store.org/acl#onlyAccessBy> "user1","user2" .
  <graph:y> <http://4store.org/acl#onlyAccessBy> "user1" .
} }'

$ sparql-query http://localhost:8008/sparql/ 'SELECT *  WHERE { GRAPH ?g { ?s ?p ?o } } LIMIT 10'
┌─────────────────┬───────────┬──────────────────────────────────────┬───────┐
│ ?g              │ ?s        │ ?p                                   │ ?o    │
├─────────────────┼───────────┼──────────────────────────────────────┼───────┤
│ <system:config> │ <graph:y> │ <http://4store.org/acl#onlyAccessBy> │ user1 │
│ <system:config> │ <graph:x> │ <http://4store.org/acl#onlyAccessBy> │ user1 │
│ <system:config> │ <graph:x> │ <http://4store.org/acl#onlyAccessBy> │ user2 │
└─────────────────┴───────────┴──────────────────────────────────────┴───────┘

You also have a minimal web interface for the status (at http://localhost:8008/status/) and executing a test query (at http://localhost:8008/test/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment