Build a Java App
Learn how to build a Java app using Stardog.
Introduction
Stardog makes the Enterprise Knowledge Graph fast and easy. We’re going to build a Java app that talks to Stardog’s RDF graph database in 5 easy steps.
- Download and install Stardog
- Create a Java project with Gradle
- Build a database
- Import data into the database
- Query and update the database
All of the code for this tutorial is available in the stardog-examples repo on Github. This guide was written using Stardog version 7.7.2 and Gradle version 7.2.
1. Download and Install Stardog
Install Stardog by following the instructions for your system on the Install Stardog page.
2. Set up a Gradle Project
Now let’s setup Gradle. In our project’s directory, run gradle init
. You will receive a series of prompts like the following:
Input the appropriate selections for your project when prompted. You can see the selections made for this example above. This will generate the necessary Gradle files for our project, which we will add to as we go.
The only dependency we need is com.complexible.stardog:client-http:VERSION
where VERSION
is your Stardog server version, which we will add to the dependencies
section of our build.gradle
, found in the app
directory. The build.gradle
is as follows:
We should now have a directory structure that looks like the following:
I have deleted app/src/test/java/JavaStardogClient/AppTest.java
since tests are beyond the scope of this tutorial, but this is where tests should be located.
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. We are going to start by creating an admin connection which will be used to make sure our demo database is in the same state each time the program is run. This is done by dropping then re-creating the database at runtime and is simply done for demonstration purposes. In production, you most likely do not want to do this.
You will also need to adjust the url
, username
, password
, and to
(the database you are connecting to) based on your Stardog instance. Example values for a default local installation are provided below.
4. Import Data
We will now add methods to create a ConnectionPool
, acquire a connection from that pool with getConnection()
, and release a connection with releaseConnection()
. 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. Define these variables according to your use case. Once again, example values are provided below. 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 the rdf data below as an example in this tutorial. In this example, we are going to store it in the app/src/main/resources
folder.
5. Query the Database
Now that everything is set up, we can begin interacting with Stardog. In our main()
method, we will start by calling createAdminConnection()
, defining our ConnectionConfiguration
, and using the configuration to create our ConnectionPool
.
Next we will create our connection, add our data, then commit the change. After that, we can begin querying the data. Once we are done, we shutdown our connection pool, and the connection is automatically closed by the try/catch block.
Within the try
block, we can do additional operations on our database such as adding additional data. First we will create IRI
variables for the other people in our database and then define our new person using these variables.
We can run the same query as before to now see Iron Man in the results.
We can remove data as well. In this example we will remove every instance where Captain America is the subject or object.
If we re-run our query, we will now see that Captain America has been removed from the database.
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.
The full code for this example can be found on our Github.