Skip to content

Code Examples

okram edited this page Dec 1, 2010 · 25 revisions

This section will provide a collection of code examples that work with the Blueprints graph API. The in-memory TinkerGraph database will be used throughout the examples. Please feel free to alter the graph constructor to work with different graph databases. These code examples can be found in the main Blueprints distribution at this location.


Create a graph. Add two vertices. Set the name property of each vertex. Create an knows edge between the two vertices. Shutdown the graph.

Graph graph = new TinkerGraph();
Vertex a = graph.addVertex(null);
Vertex b = graph.addVertex(null);
a.setProperty("name", "marko");
a.setProperty("name", "peter");
Edge e = graph.addEdge(null, a, b, "knows");
graph.shutdown();


Load the TinkerPop play graph diagrammed in Property Graph Model. Get vertex 1 from the graph by its id. Print some information about the vertex. Iterate through the outgoing edges of the vertex and print the edges.

Graph graph = TinkerGraphFactory.createTinkerGraph();
Vertex a = graph.getVertex("1");
System.out.println("vertex " + a.getId() + " has name " + a.getProperty("name"));
for(Edge e : a.getOutEdges()) {
  System.out.println(e);
}

The System.out after the code executes is:

vertex 1 has name marko
e[7][1-knows->2]
e[9][1-created->3]
e[8][1-knows->4]