2. Setup a Gradle Project

Now let’s setup Gradle. Stardog components and subsystems are modular, so we can
use just the parts we need. For this post, we will start with a basic Gradle
configuration and add as we go. In this example we are just connecting to
Stardog over HTTP and the only dependency that we need is
com.complexible.stardog:client-http:4.2.4. The build.gradle is as follows:

3. Build a Database

We have Stardog installed and Gradle configured. Let’s start building our
database. The first thing is to create a connection to Stardog. This will allow
us to perform administrative actions such as creating a new database,
adding/removing data, etc.

We will use the AdminConnectionConfiguration utility to create the connection.
In the code snippet below, we create the AdminConnectionConfiguration by
specifying the server URL and user credentials. In this case, we are using the
default values—

url = http://localhost:5820

username = admin

password = admin

Once connected, we will print out available databases, search for the database
we are about to create (just in case the code was run before), and create the
database on disk.

/**
* Creates a connection to the DBMS itself so we
* can perform some administrative actions.
*/
public static void createAdminConnection() {
try (final AdminConnection aConn = AdminConnectionConfiguration.toServer(url)
.credentials(username, password)
.connect()) {
// A look at what databases are currently in Stardog
aConn.list().forEach(item -> System.out.println(item));
// Checks to see if the 'myNewDB' is in Stardog. If it is, we are
// going to drop it so we are starting fresh
if (aConn.list().contains(to)) {aConn.drop(to);}
// Convenience function for creating a persistent
// database with all the default settings.
aConn.disk(to).create();
}
}

4. Import Data

Next it’s time to import data and run queries against it. We need to create a
connection ConnectionPool. The ConnectionConfiguration will tell the pool
how to create the new connections. We then pass the ConnectionConfiguration to
the ConnectionPoolConfig as it is the basis of the pool.

We can also provide additional detail about the pool such as min/max pool size,
expiration, and block wait time. We then create the pool and return so we can
start using it.

After we get a Stardog connection, we will use it to populate our database.
Changes to a database must occur within a transaction; i.e., begin followed by
commit or rollback. Changes are not visible to others until the transaction
is committed or until you perform a query operation to inspect the state of the
database within the transaction.

We will use begin and commit for import. Below is a snippet of that data
that we are importing followed by the code to import it. As you can see we
perform the begin, add the data by importing the file, and finally commit the
transaction.

// first start a transaction. This will generate the contents of
// the databse from the N3 file.
connection.begin();
// declare the transaction
connection.add().io()
.format(RDFFormat.N3)
.stream(new FileInputStream("src/main/resources/marvel.rdf"));
// and commit the change
connection.commit();

5. Query the Database

Since we have a populated database we will use the obtained Stardog connection
to query. We first create the SPARQL select statement and store it in the
SelectQuery. We then execute the query and store the result set in the
TupleQueryResult object. The result set can be used for many different
purposes. We are just going to print it to the console using QueryResultIO.

We can add and remove data using the API statements feature. Just like the
initial data import transaction, we will use the begin and commit options to
update the database. We add IronMan to the list of superheroes and then we
will remove all references to CaptainAmerica.

As you can see in the add, we have IronMan as the subject for all the
statements while using different FOAF objects for the predicate and either a
FOAF object, literal, or another superhero as the object. In the remove
statement, we are removing all instances where CaptainAmerica is either the
subject or the object.

Finally, we must remember to release the connection that we obtained to perform
our tasks and shutdown the connection pool.

connectionPool.release(connection);
connectionPool.shutdown();

Conclusion

I showed you how to install Stardog on a Linux environment, create an
administration connection in order to perform administrative actions, create a
connection pool to the database, and use a connection from the pool to perform
transactions and queries.

In my next post I’ll expand on more of the features that Stardog has to offer as
well as provide an example on how to wire a Stardog client using Spring. The
full version of the code we’ve discussed here is in
the
stardog-examples repo on Github.

451 Research says adoption of Knowledge Graph technology is on the rise. Read why