Link Search Menu Expand Document
Start for Free


This page discusses using Groovy to interact with Stardog.

Page Contents
  1. Overview
  2. API Overview
  3. Examples
    1. Create a Connection
    2. SPARQL Vars Projected into Groovy Closures
    3. Add & Remove Triples
    4. withConnection closure
    5. SPARQL Update


Groovy is an agile and dynamic programming language for the JVM, making popular programming features such as closures available to Java developers. Stardog’s Groovy support makes life easier for developers who need to work with RDF, SPARQL, and OWL by way of Stardog.

The Groovy for Stardog source code is available on Github.

Binary releases are available on via Maven central using the following dependency declaration (Gradle style): com.complexible.stardog:stardog-groovy:6.1.0.

Stardog-Groovy can be included via com.complexible.stardog:stardog-groovy:6.1.0 from Maven central.

You must include our public repository in your build script to get the Stardog client dependencies into your local repository.

The Stardog-Groovy version always matches the Stardog release, e.g. for Stardog 6.1.0 use stardog-groovy-6.1.0.

API Overview

Groovy for Stardog provides a set of Groovy API wrappers for developers to build applications with Stardog and take advantage of native Groovy features. For example, you can create a Stardog connection pool in a single line, much like Groovy SQL support. In Groovy for Stardog, queries can be iterated over using closures and transaction safe closures can be executed over a connection.

Groovy for Stardog includes com.complexible.stardog.ext.groovy.Stardog with the following methods:

  • Stardog(map) constructor for managing Stardog connection pools
  • each(String, Closure) for executing a closure over a query’s results, including projecting SPARQL result variables into the closure.
  • query(String, Closure) for executing a closure over a query’s results, passing the BindingSet to the closure
  • insert(List) for inserting a list of vars as a triple, or a list of list of triples for insertion
  • remove(List) for removing a triple from the database
  • withConnection for executing a closure with a transaction safe instance of Connection.


Create a Connection

def stardog = new Stardog([url: "snarl://localhost:5820/", to:"testdb", username:"admin", password:"admin"])
stardog.query("select ?x ?y ?z WHERE { ?x ?y ?z } LIMIT 2", { println it } )
// in this case, it is a BindingSet, ie called until exhausted and closure executed

SPARQL Vars Projected into Groovy Closures

// there is also a projection of the results into the closure's binding
// if x, y, or z are not populated in the answer, then they are still valid binidng but are null
stardog.each("select ?x ?y ?z WHERE { ?x ?y ?z } LIMIT 2", {
  println x
  println y
  println z // may be a LiteralImpl, so you get full access to manipulate Value objects

Add & Remove Triples

// insert and remove
stardog.insert([["urn:test3", "urn:test:predicate", "hello world"],
  	["urn:test4", "urn:test:predicate", "hello world2"]])
stardog.remove(["urn:test3", "urn:test:predicate", "hello world"])
stardog.remove(["urn:test4", "urn:test:predicate", "hello world2"])

withConnection closure

// withConnection, tx safe
stardog.withConnection { con ->
    def queryString = """
		SELECT ?s ?p ?o
		  ?s ?p ?o
	TupleQueryResult result = null;
	try {
		Query query = con.query(queryString);
		result = query.executeSelect();
		while (result.hasNext()) {


	} catch (Exception e) {
		println "Caught exception ${e}"


// Accepts the SPARQL Update queries
stardog.update("DELETE { ?a ?b \"hello world2\" } INSERT { ?a ?b \"aloha world2\" } WHERE { ?a ?b \"hello world2\" }")

def list = []
stardog.query("SELECT ?x ?y ?z WHERE { ?x ?y \"aloha world2\" } LIMIT 2", { list << it } )
assertTrue(list.size == 1)