Hello world in GraphDB

The following program opens a connection to a repository, evaluates a SPARQL query and prints the result. The example uses the GraphDBHTTPRepository class, which is an extension of RDF4J’s HTTPRepository that adds support for GraphDB features such as the GraphDB cluster.

In order to run the example program, you need to build from the appropriate .pom file:

mvn install

Followed by running the resultant .jar file:

java -jar dev-examples-1.0-SNAPSHOT.jar
package com.ontotext.graphdb.example.app.hello;

import com.ontotext.graphdb.repository.http.GraphDBHTTPRepository;
import com.ontotext.graphdb.repository.http.GraphDBHTTPRepositoryBuilder;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.query.*;
import org.eclipse.rdf4j.repository.RepositoryConnection;

 * Hello World app for GraphDB
public class HelloWorld {
    public void hello() throws Exception {
        // Connect to a remote repository using the GraphDB client API
        // (ruleset is irrelevant for this example)
        GraphDBHTTPRepository repository = new GraphDBHTTPRepositoryBuilder()
                //.withCluster(); // uncomment this line to enable cluster mode

        // Alternative access to a remote repository using pure RDF4J
        // HTTPRepository repository = new HTTPRepository("http://localhost:7200/repositories/myrepo");

        // Separate connection to a repository
        RepositoryConnection connection = repository.getConnection();

        try {
            // Preparing a SELECT query for later evaluation
            TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL,
                    "SELECT ?x WHERE {" +
                            "BIND('Hello world!' as ?x)" +

            // Evaluating a prepared query returns an iterator-like object
            // that can be traversed with the methods hasNext() and next()
            TupleQueryResult tupleQueryResult = tupleQuery.evaluate();
            while (tupleQueryResult.hasNext()) {
                // Each result is represented by a BindingSet, which corresponds to a result row
                BindingSet bindingSet = tupleQueryResult.next();

                // Each BindingSet contains one or more Bindings
                for (Binding binding : bindingSet) {
                    // Each Binding contains the variable name and the value for this result row
                    String name = binding.getName();
                    Value value = binding.getValue();

                    System.out.println(name + " = " + value);

                // Bindings can also be accessed explicitly by variable name
                //Binding binding = bindingSet.getBinding("x");

            // Once we are done with a particular result we need to close it

            // Doing more with the same connection object
            // ...
        } finally {
            // It is best to close the connection in a finally block

    public static void main(String[] args) throws Exception {
        new HelloWorld().hello();