Introduction to OrientDB Graph Edition
This is my second tutorial on databases. First being the Neo4J tutorial. This time I’m presenting you OrientDB. It is a completely free document-graph database solution. Unlike Neo4J where the database stores nodes and relationships, OrientDB stores documents. The difference between a node and a document here is, that documents can have a more complex structure. Node contains a set of key-value pairs and a document is a more complex structure (image JSON document here), where nodes can be nested inside and form a tree type structure. However any relations between documents are stored directly, just like in a graph.
In this tutorial I’ll be using maven build tool.
Create an empty maven application project and add following lines to pom.xml:
<repositories>
<repository>
<id>Sonatype Releases</id>
<url>https://oss.sonatype.org/content/repositories/releases/</url>
</repository>
<repository>
<id>Sonatype Snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
We will be using snapshot version of OrientDB Graph Edition as it seems it is the only way to get it via maven.
And add this to dependencies:
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-graphdb</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
Try to build the project and make sure that all dependencies are downloaded. And then we can start with the actual code.
First lets initialize the database object:
OGraphDatabase db = new OGraphDatabase(PATH);
The PATH is a path to where the database is located. For the sake of this tutorial I will be using an in-memory database, so my path is “memory:db”.
We need to create a database:
db.create();
If you are using an in-file database, you would do this as:
if (!db.exists()) {
db.create();
} else {
db.open("admin", "admin");
}
The default username/password is always admin/admin.
OK, so in the Graph Edition of the OrientDB we have a (vertex, edge) structure (yes, a graph), where each vertex and edge is a document. We can create custom classes for vertices and edges. Under the hood, what will happen is new classes in OrientDB extending OGraphVertex or OGraphEdge will be created.
db.createVertexType("Student");
db.createVertexType("Course");
db.createEdgeType("Attends");
So here we create Student, Course and Attends classes. Now lets populate some data into the graph:
ODocument sJohn = db.createVertex("Student");
sJohn.field("name", "John");
sJohn.save();
ODocument sBetty = db.createVertex("Student");
sBetty.field("name", "Betty");
sBetty.save();
ODocument cAlgebra = db.createVertex("Course");
cAlgebra.field("title", "Algebra");
cAlgebra.save();
ODocument cLiterature = db.createVertex("Course");
cLiterature.field("title", "Literature");
cLiterature.save();
So, now we have two students: John and Betty, 2 courses: Algebra and Literature, and 3 edges of type Attends, where John attends Algebra and Betty attends Algebra and Literature.
Lets try and write some queries against our database. OrientDB supports SQL-like queries and a more sophisticated language for graph queries - GREMLIN. And you can also mix the two.
First lets select the names of all the students in a database.
List<ODocument> results = db.query(new OSQLSynchQuery("select name from Student"));
for (ODocument result: results) {
System.out.println(result.field("name"));
}
As you can see, the query is just like an SQL query. Now lets try to query an answer to ‘which courses does Betty attend’.
List<ODocument> results = db.query(new OSQLSynchQuery("select GREMLIN('current.outE.filter{it['@class']=='Attends'}.inV').title as value from Student where name = 'Betty'"));
Here we are selecting students named Betty and then passing results to the GREMLIN query. Each result is passed as as current. Next we access outE which holds all the edges that point from the current vertex (edges in OrientDB are directional). We take the edge lister and filter only those that are of class ‘Attends’ and then we take all the vertices (inV) that the edges point to. And then we select the course title from the resulting vertices.
After we completed using our database, we should close the connection:
db.close();