Hi. I'm Mark Wallace. In my role as a an Ontologist and Software Architect, I am continually working with new and fun semantic technologies. Be it RDF/OWL, Triple-stores, Semantic Wikis, or Text Extraction, I am learning more all the time and want to share my experiences in hopes of helping others along with these technologies. I hope post a new article every month or two, so check back in every so often to see what’s cooking!

Wednesday, April 17, 2024

A working triple store in about 5 minutes

There are many great triples stores out there. I am not endorsing any particular one. However, if you are wanting a quick way to start playing around with triples and SPARQL, I've found RDF4J to be one of the fastest ways to get started with the RDF/SPARQL stack.

This example is on Windows 10. But setting up on unix should be similar.

This also assumes that you have JAVA installed. As of this writing, Java 11 is the latest version.

Set Up a Tomcat Web Server

Google for Tomcat download. Download tomcat and unpack to a folder, e.g. C:\Programs (Downloads for me took about 1 minute total.)

Deploy the RDF4J Webapps in Tomcat

Google for RDF4J download. Download the ZIP file. Open the ZIP and find the war folder.
Copy rdf4j-server.war and rdf4j-workbench.war into tomcat's webapps folder.

Run Tomcat

Start tomcat using its bin folder's startup.bat script. (Wait till console shows Server startup in ... ms)

Start using your triple store!

In a browser, go to http://localhost:8080/rdf4j-workbench

Congratulations, you have a working triple store! (If you don't, reply/comment and tell me what you're seeing.)

Create a new repository

RDF4J lets you have multiple "repositories" which are basically independent triple stores within the RDF4J server. Let's set up one.

On the workbench user interface, click New Repository and create a repository, give it an id like test, a title like a test repo and click Next then Create.

Put in some data

On the workbench user interface, Click SPARQL Update Paste this in:

PREFIX : <http://example/>
INSERT DATA {
  :Mark a :Person .
  :Judi a :Person .
  :Linda a :Person .
  :Mark :knows :Judi .
  :Judi :knows :Linda .
  :Mark :knows :Linda .
}

Click Execute

You can also load data by uploading files.

Explore the data

  • Click Types to see that it figured out that <http://example/Person> is a type.
  • Click on <http://example/Person> to see the instances of this type.
  • Click on <http://example/Judi> to all the triples with <http://example/Judi> as the subject or object.

Add namespaces to make things less verbose

  • Click Namespaces.
  • Enter test in prefix box, and http://example/ in Namespace box
  • Click Update.

Now explore data again and see that it uses the test: namespace prefix rather than the full URI when showing data, e.g. test:Mark and test:knows.

SPARQL Query your data

Click Query Enter this:

select * where {?x a ?y} limit 10

Click Execute.

Beyond 5 minutes...

Want to query as a SPARQL Endpoint?

Use URL of form below, where test is the repository id you specified. The part after query= must be URL encoded. e.g.,

curl  http://localhost:8080/rdf4j-server/repositories/test?query=select%20%2A%20%7B%3Fx%20a%20%3Fy%7D

Want to load your triple store over a REST API?

Use URL of form below to push statements.

curl --data @mydata.ttl -H "Content-Type: application/x-turtle" http://localhost:8080/rdf4j-server/repositories/test/statements 

You can store to a named graph by adding a ?context parameter to the URL, e.g.,

curl -v --data @mydata.ttl -H "Content-Type: application/x-turtle" \
http://localhost:8080/rdf4j-server/repositories/test/statements?context=%3Chttp://graph1%3E

More realistic dataset

Try with more triples, and multiple named graphs.

Approach: 

  • Use LUBM data set (e.g. 1 University)
  • Put ontology into 1 named graph, data for Univ in another.
  • Query against joined graphs

RDF4J: 

  • Create repo named lubm1 
  • For ontology:
    • Click Add
    • Put in RDF Data URL of  http://swat.cse.lehigh.edu/onto/univ-bench.owl 
    • Select Data format of RDF/XML
    • Click Upload
  • For data:
    • make a zip of all the University*.owl files from LUBM 1 University data set
    • click Add and choose the zip file you made
    • set Base URI to http://www.University0.edu (which will make graph name <http://www.University0.edu>)
    • Select Data format of RDF/XML
    • Click Upload

Now you will have 2 graphs: 1 with TBox and 1 with University0 ABox data.

Inferred triples land in the default graph. 

Followers