Create your first application with Java and OrientDB!

Jaque.
The Ksquare Group
Published in
6 min readAug 20, 2019

These days, businesses are looking for innovative ways to handle customer-related graph database operations, such as functions on Amazon like “Customers who viewed this product also viewed” or on Facebook, with the operation “People you may also know…”.

On e-commerce, social media, mobile data, etc., things have become a little more complex due to the rapid increase of data (there’s just so much data out there to handle!).

One of those solutions is a graph database. Choosing a graph database to contain all data is actually a great idea because they allow a higher level of flexibility when it comes to representing the data while handling elaborated interactions. In fact, graph databases have so many perks, that 2018 was considered the year of the graph database.

Following this trend, and the fact that graph databases have grown in popularity, we decided to do a quick tutorial to easily create an application using Java and OrientDB -a well-known graph database.

A brief introduction to OrientDB

OrientDB is the first multi-model open source NoSQL DBMS that combines the traditional DBMS features with the power of Graph and the flexibility of Document. It is capable of bringing high-performance operational into a scalable database.

OrientDB vs Neo4j

Today, everybody is using the Neo4j graph database. It is the most popular, yes, but, we wanted to make an easy tutorial to work with a different graph model database, as doing it with Neo4j requires knowing it’s “Cypher” query language. Working with OrientDB is much easier because it has a language based on SQL, and you could get the performance of Neo4j while using skills -like SQL- you already have!

Installation on Ubuntu 18.04 LTS

Download the latest version of OrientDB from this link and unzip it. Now, you have to navigate into the directory we previously unpacked and run the following command to start OrientDB Server:

cd orientdb-3.0.4/
cd bin/
./server.sh

This will start OrientDB server.

If it is the first time starting the server, you will be asked to enter a root password. Once you have done that, your OrientDB service will be running. That way, you can open the UI using the link that the server gives you.

Log in with the username root and the password you configured earlier.

Let’s create a new database!

Click on NEW DB and enter “Music” on the database name. Then, click on the CREATE DATABASE button. Looks nice, but, how can I connect to the DB and manipulate it?

With a Java application, of course!

Open your favorite IDE capable of supporting Java and Maven, in this example, we will use IntelliJ.

To create a new project we have to choose the menu File → New → Project…

Select Maven, and now click on Next. Select your GroupId and ArtifactId, and then select ‘Next’ one more time.

Everything looks fine over here. Now, let’s click on the ‘Finish’ button.

Open the pom.xml file and add the following dependencies, finally enable Java 8 as a target compiler.

Create a basic schema and connect to the DB

Now we will create a simple application with a few steps. To do this, we have to open the folder src → main → java.

Right-click on the folder java → New → Java Class, and write main.

Connecting to OrientDB

The first object will manage the remote server. Here, we are going to specify the address of the host where our server is running.

Below, we see that we open a database session. At this point, we need to introduce the db name we previously created, the username and the password for the connection.

OrientDB orient = new OrientDB("remote:localhost", OrientDBConfig.defaultConfig());
ODatabaseSession db = orient.open("Music", "admin", "admin");
db.close();
orient.close();

Creating the schema

To stick with the music theme (db name), we want to create a graph of singers who have songs and the year they released that specific song.

Vertex

  • Singer: Contain the name and surname of the Artist.
  • Song: Name of the song.
  • Year: Release year.

Edge

  • SongBy: Connects the Song with the Singer.
  • ReleaseYear: Connects the song with the released year.

To do this, we create a schema with 3 classes: Singer, Year and Song.

private static void createSchema(ODatabaseSession db) {
OClass singer = db.getClass("Singer");
OClass year = db.getClass("Year");
OClass song = db.getClass("Song");
if (singer == null) {
singer = db.createVertexClass("Singer");
}
if (year == null) {
year = db.createVertexClass("Year");
}
if (song == null) {
song = db.createVertexClass("Song");
}
if (singer.getProperty("name") == null) {
singer.createProperty("name", OType.STRING);
singer.createIndex("Song_name_index", OClass.INDEX_TYPE.NOTUNIQUE, "name");
}
if (year.getProperty("year") == null) {
year.createProperty("year", OType.STRING);
year.createIndex("Song_year_index", OClass.INDEX_TYPE.NOTUNIQUE, "year");
}
if (song.getProperty("title") == null) {
song.createProperty("title", OType.STRING);
song.createIndex("Song_title_index", OClass.INDEX_TYPE.NOTUNIQUE, "title");
}
if (db.getClass("SongBy") == null) {
db.createEdgeClass("SongBy");
}
if (db.getClass("ReleaseYear") == null) {
db.createEdgeClass("ReleaseYear");
}
}

Now, we have to create one method for each vertex. The way of doing it is by setting the name of our vertex and the properties.

private static OVertex createSinger(ODatabaseSession db, String name, String surname) {
OVertex result = db.newVertex("Singer");
result.setProperty("name", name);
result.setProperty("surname", surname);
result.save();
return result;
}
private static OVertex createYear(ODatabaseSession db, String year) {
OVertex result2 = db.newVertex("Year");
result2.setProperty("year", year);
result2.save();
return result2;
}
private static OVertex createSong(ODatabaseSession db, String title) {
OVertex result3 = db.newVertex("Song");
result3.setProperty("title", title);
result3.save();
return result3;
}

Inserting data to prove everything is fine

It’s time to insert data, so let’s create some vertex and edges in the database.

private static void createSongList(ODatabaseSession db) {
OVertex singer = createSinger(db, "Jordy", "Lemoine");
OVertex release = createYear(db, "2019");
OVertex song = createSong(db, "Love me right");
OEdge edge1 = song.addEdge(singer, "SongBy");
edge1.save();
OEdge edge2 = song.addEdge(release, "ReleaseYear");
edge2.save();
OVertex singer2 = createSinger(db, "Sam", "Smith");
OVertex song2 = createSong(db, "Dancing with a stranger");
OEdge edge3 = song2.addEdge(singer2, "SongBy");
edge3.save();
OEdge edge4 = song2.addEdge(release, "ReleaseYear");
edge4.save();
}

And maybe a query?

private static void executeAQuery(ODatabaseSession db) {
String query = "SELECT * FROM Singer WHERE name = ?";
OResultSet rs = db.query(query, "Sam");
while (rs.hasNext()) {
OResult item = rs.next();
System.out.println("Singer: " + item.getProperty("name"));
}
rs.close();
}

And that’s all! You’ve made your first Java application. Let’s RUN our project, menu Run→ Run ‘main’.

Time to see what it actually does on the OrientDB UI

We’ll click on menu GRAPH and write a SELECT query.

If we double-click the circle where the Year is, it will expand. So, let’s do that with every vertex that appears to finally have something like this:

Conclusion

In this quick tutorial, we focused on communicating with an OrientDB database. We saw how to connect to OrientDB from the Java client library and how to run various basic operations.

You can find the full code here: main.java; this is a Maven project, so it should be easy to import and run it as it is.

--

--