Java Tutorial

In the event that you are used only to Relation database systems, you may find OrientDB a very unfamiliar system to work with. Given that it also supports Document, Graph and Object-Oriented modes, it requires different Java API’s. But, there are some similarities between them too.

Similar to JDBC, a Blueprints API exists, made by Tinkerpop, which supports the basic operations on a graph database. There is an OrientDB driver, (or, to be more accurate, an adapter), which makes it possible to operate without having to deal with OrientDB classes. This means that the resulting code is more portable, given that Blueprints offers adapters to other graphing database systems.

If you need to tweak the database configuraiton, you need to use OrientDB API’s directly. It is recommend that in these situations you use a mix: Bluepringts when you can, the OrientDB API’s where necessary.

OrientDB Java APIs

There are three different API’s that OrientDB ships with. Choose one based on your mode.

OrientDB comes with 3 different APIs. Pick your based on your model (for more information look at Java API):

For more information on the API’s in general, see Java API

Graph API

Connecting to a Graph Database

The first object you need is a OrientGraph:

  1. import com.tinkerpop.blueprints.impls.orient.OrientGraph;
  2. OrientGraph graph = new OrientGraph("local:test", "username", "password");

Inserting Vertices and Edges

While OrientDB can work with the generic V class for verticies and E class for edges, you gain much more power by defining custom types for both vertices and edges.

  1. odb.createVertexType("Person");
  2. odb.createVertexType("Address");

The Blueprint adapter for OrientDB is thread-safe and automatically creates a transaction where necessary. That is, it creates a transaction at the first operation, in the event that a transaction has not yet explicitly been started. You have to specify where transactions end, for commits or rollbacks.

To add vertices into the database with the Blueprints API:

  1. Vertex vPerson = graph.addVertex("class:Person");
  2. vPerson.setProperty("firstName", "John");
  3. vPerson.setProperty("lastName", "Smith");
  4. Vertex vAddress = graph.addVertex("class:Address");
  5. vAddress.setProperty("street", "Van Ness Ave.");
  6. vAddress.setProperty("city", "San Francisco");
  7. vAddress.setProperty("state", "California");

Bear in mind, the specific syntax with Blueprint is class:<class name>. You must use this syntax in creating an object to specify its class. This is not mandatory. It is also possible to specify a null value, (which means a vertex is created with the class V, as its the superclass for all vertices in OrientDB).

  1. Vertex vPerson = graph.addVertex(null);

In consequence of this is that you cannot distinguish null vertices from other vertices in a query.

Use a similar API in adding an edge:

  1. OrientEdge eLives = graph.addEdge(null, vPerson, vAddress, "lives");

In OrientDB, the Blueprints label concept is bound to an edge’s class. You can create an edge of the class lives by passing it as a label or as a class name.

  1. OrientEdge eLives = graph.addEdge("class:lives", vPerson, vAddress, null);

You have now created:

  1. [John Smith:Person] --[lives]--> [Van Ness Ave:Address]

Bear in mind that, in this example, you have used a partially schema-full mode, as you defined the vertex types, but not their properties. By default, OrientDB dynamically accepts everything working in a schema-less mode.

SQL queries

The Tinkerpop interfaces allow you to execute fluent queries or Germlin queries, but you can still use the power of OrientDB SQL through the .command() method.

  1. for (Vertex v : (Iterable<Vertex>) graph.command(
  2. new OCommandSQL("SELECT EXPAND( OUT('bough') ) FROM Customer WHERE name='Jay'")).execute()) {
  3. System.out.println("- Bought: " + v);
  4. }

In addition to queries, you can also execute any SQL command, such as CREATE VERTEX, Update, or DELETE VERTEX.

Along with queries, you can execute any SQL command like CREATE VERTEX, UPDATE, or DELETE VERTEX. For example,

  1. int modified = graph.command(
  2. new OCommandSQL("UPDATE Customer SET local = true WHERE 'Rome' IN out('lives').name")).execute());

This sets a new property called local to true on all instances in the Customer class that live in Rome.