Skip to content
okram edited this page Dec 1, 2010 · 16 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.

  1. Create a Simple Graph
  2. Iterate through the Elements of a Graph
  3. Iterate through the Edges of a Vertex
  4. Use Indices to Get a Vertex by its Property

Create a Simple Graph

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();

Iterate through the Elements of a Graph

public void testIteratingGraph() {
  Graph graph = TinkerGraphFactory.createTinkerGraph();
  System.out.println("Vertices of " + graph);
  for (Vertex vertex : graph.getVertices()) {
    System.out.println(vertex);
  }
  System.out.println("Edges of " + graph);
  for (Edge edge : graph.getEdges()) {
    System.out.println(edge);
   }
}

The System.out after the code executes is:

Vertices of tinkergraph[vertices:6 edges:6]
v[3]
v[2]
v[1]
v[6]
v[5]
v[4]
Edges of tinkergraph[vertices:6 edges:6]
e[10][4-created->5]
e[7][1-knows->2]
e[9][1-created->3]
e[8][1-knows->4]
e[11][4-created->3]
e[12][6-created->3]

Iterate through the Edges of a Vertex

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]

Use Indices to Get a Vertex by its Property

Load the TinkerPop play graph diagrammed in Property Graph Model. TinkerGraph implements the IndexableGraph interface. Get the standard vertex index and then get all vertices named marko. Given that there is only one, we can simple next the first vertex out of the returned iterable. Print some information about the vertex.

Graph graph = TinkerGraphFactory.createTinkerGraph();
Vertex a = ((IndexableGraph)graph).getIndex(Index.VERTICES, Vertex.class).get("name", "marko").iterator().next();
System.out.println("vertex " + a.getId() + " has age " + a.getProperty("age"));

The System.out after the code executes is:

vertex 1 has age 29