RDF store with Alibaba library example

By , last updated September 25, 2016

Alibaba is a great library to work with RDF Store as a Java developer. It generates Java interfaces from RDF triples that are very easy to work with.

Here is a simple tutorial on how to start working with complex RDF data in Java.

Install Alibaba

Warning: Alibaba works with the latest version of OpenRDF Sesame. It is currently not supported by RDF4J project.

Start by installing Alibaba and generate Java objects.

You may create your own Java/Maven project for this purpose.

Connect to RDF Store

Time to connect to our RDF repository:

Repository repository = new HTTPRepository(REPOSITORY_SERVER_URL, REPOSITORY_NAME);

ObjectRepository objectRepository = new ObjectRepositoryFactory()
ObjectConnection objectConnection = objectRepository.getConnection();

Disconnect from repository:


Read from RDF Store

Retreive data from RDF as Java objects:

public void doStuff() throws RepositoryException {
	Result<Person> persons = objectConnection.getObjects(Person.class);

	while (persons.hasNext()) {
		Person person = persons.next();
		String name = person.getName();

The beauty of working with objects instead of statements is the ability to with logical entities directly in the code, which is true to the Java spirit. The logic in your program becomes clear as day.

Insert into RDF Store

Create and insert new objects into RDF repository using Java classes.

This step was a little bit tricky to figure out. The reason was the way Alibaba saves triples into the store.

To do it right I had to “initialize” the object in the RDF Store first. You can do it by “getting” the object from the store first. If you ask the database for the object that doesn’t exist it will create an empty placeholder for your object and return a default ID:

Object o = objectConnection.getObject(id);

Next step is to tell Alibaba to make the object of a special class:

Girl girl = objectConnection.addDesignation(o, Girl.class);

You need to do it for each class type so I’ve created a template function:

public <T extends Person> T initObject(String uri, Class<T> classType) {
	ValueFactory vf = objectConnection.getValueFactory();
	URI id = vf.createURI(uri);
	Object o = objectConnection.getObject(id);
	T unit = objectConnection.addDesignation(o, classType);

	return unit;

Now you may fill your new object with some data and save it:

private void createNewPerson() throws RepositoryException {
	Girl girl = repository.initObject(NAME, Girl.class);

Add new statements into RDF Store

Make some one time changes to the data in your RDF Store by making casual updates directly with SPARQL:

public void changeMyId() {
	RepositoryConnection connection = repository.getConnection();

		"INSERT ... WHERE...").execute();

Best practice

  1. Always have a split between RDF repository read and write functionality and application logic. Alibaba may or may not be a part of RDF store solution in the future. It will be much easier to change the read/write logic if it’s not integrated into the application. Remember – encapsulation.
  2. Prefetch as much as possible. Read and write operations are always the most expensive ones. Most RDF stores have caching functionality. Use it to your advantage.


Be the first to comment.

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>