|
IntroductionEven if OrientDB already provides own APIs to handle graphs in easy way, starting from release 0.9.22 OrientDB provides an implementation of the Tinkerpop Blueprints APIs. Tinkerpop is a complete stack of projects to handle Graphs:
Get startedDownload the Graph(Ed) and follow this tutorial. Database typesThe root class to handle graphs is OrientGraph. The kind of database used depends by the Database URL used.
Persistent GraphDB can be:
Work with GraphDBBefore to work with a graph you need an instance of OrientGraph class. The constructor gets a URL that is the location of database. If the database already exists, it will be opened, otherwise will be created. Remember always to close the graph once done using the .shutdown() method. Example: OrientGraph graph = null;
try {
graph = new OrientGraph("local:C:/temp/graph/db");
...
}finally{
if( graph != null )
graph.shutdown();
}Gremlin usageIf you use GREMLIN language with OrientDB remember to initialize it with: OGremlinHelper.global().create() SecurityIf you want to use the OrientDB security use the constructor that get the URL, user and password. To know more about OrientDB security visit Security. Reuse a graphTo reuse an opened graph use the void OrientGraph.clear() method. This method remove all the vertexes, edges, properties and indexes permanently. Example: OrientGraph graph = new OrientGraph("local:C:/temp/graph/db", "admin", "admin");
graph.clear();TransactionsTransaction modesAvailable transaction modes are:
To know the current transaction mode call the Mode getTransactionMode() method. To change it use void setTransactionMode(Mode iMode) where Mode can be MANUAL or AUTOMATIC. Use transactionsTo group multiple operations inside the same transaction you need to mark the begin of the transaction with the method void startTransaction(). This method can be called only if database is in MANUAL transaction mode (see Transaction mode). Example: graph.startTransaction();
try{
Vertex luca = graph.addVertex(null);
luca.setProperty( "name", "Luca" );
Vertex marko = graph.addVertex(null);
marko.setProperty( "name", "Marko" );
Edge lucaKnowsMarko = graph.addEdge(null, luca, marko, "knows");
graph.stopTransaction(Conclusion.SUCCESS);
} catch( Exception e ) {
graph.stopTransaction(Conclusion.FAILURE);
}Surrounding the transaction between a try/catch assure that any errors will rollback the transaction to the previous status for all the involved elements. Work with vertexes and edgesCreate a vertexTo create a new Vertex in the current Graph call the Vertex OrientGraph.addVertex(Object id) method. Note that the id parameter is ignored since OrientDB implementation assigns a unique-id once the vertex is created. To return it use Vertex.getId(). Example: Vertex v = graph.addVertex(null); System.out.println( "Created vertex: " + v.getId() ); Create an edgeAn edge links two vertexes previously created. To create a new Edge in the current Graph call the Edge OrientGraph.addEdge(Object id, Vertex outVertex, Vertex inVertex, String label ) method. Note that the id parameter is ignored since OrientDB implementation assigns a unique-id once the vertex is created. To return it use Edge.getId(). outVertex is the vertex instance where the edge starts and inVertex is the vertex instance where the edge ends. label is the edge's label. Null to not assign it. Example: Vertex luca = graph.addVertex(null); luca.setProperty( "name", "Luca" ); Vertex marko = graph.addVertex(null); marko.setProperty( "name", "Marko" ); Edge lucaKnowsMarko = graph.addEdge(null, luca, marko, "knows"); System.out.println( "Created edge: " + e.getId() ); Remove a vertexTo remove a vertex from the current Graph call the void OrientGraph.removeVertex(Vertex vertex) method. The vertex will be disconnected from the graph and then removed. Disconnection means that all the vertex's edges will be deleted as well. Example: graph.removeVertex(luca); Remove an edgeTo remove an edge from the current Graph call the void OrientGraph.removeEdge(Edge edge) method. The edge will be removed and the two vertexes will result not connected anymore. Example: graph.removeEdge(lucaKnowsMarko); Set and get propertiesVertexes and Edges can have multiple properties where the key is a String and the value can be any supported OrientDB types.
Example: vertex2.setProperty( "x", 30.0f );
vertex2.setProperty( "y", ((float) vertex1.getProperty( "y" )) / 2 );
for( String property : vertex2.getPropertyKeys() ){
System.out.println("Property: " + property + "=" + vertex2.getProperty( property ) );
}
vertex1.removeProperty( "y" );Access to the underlying GraphSince TinkerPop Blueprints API is quite raw and doesn't provide ad-hoc methods for very common use cases you could need to access to the underlying ODatabaseGraphTx object to better use the graph-engine under the hood. Commons operations are:
The OrientGraph class provides the method .getRawGraph() to return the underlying native root Graph class: ODatabaseGraphTx. Follow the Graph Database Native APIs to know its usage. Example: final OrientGraph graph = new OrientGraph("local:C:/temp/graph/db");
try{
List<OGraphVertex> result = graph.getRawGraph().query( new OSQLSynchQuery("select from ographvertex where outEdges contains ( in.label = 'knows' )"));
} finally {
graph.shutdown();
}Use the Gremlin language
TuningAs reported in Access to the underlying Graph you could use the native API to speed up some operations. Furthermore since TinkerPop Blueprints API doesn't provide a connection pool mechanism you can avoid to close and reopen the underlying database by setting this property. OGlobalConfiguration.STORAGE_KEEP_OPEN.setValue(Boolean.TRUE); or by launching your application with this parameter: java -Dorientdb.storage.keepOpen=true ... This avoids to close and reopen the storage every time. It will be closed automatically when the JVM exits. BenchmarksIf you've more information about new benchmarks or you've done your own please share it on the OrientDB Group |