Skip to content

Updating a Graph

okram edited this page Jun 7, 2011 · 7 revisions

Many of the examples in the Gremlin documentations with respect to querying/reading the graph. However, Gremlin is not simply a query language. With Gremlin it is also possible to update the graph. Gremlin can directly access Blueprints and whatever Blueprints provides, Gremlin provides.

The Standard Blueprints API

The Blueprints API provides methods to manipulate vertices, edges, properties, etc. The Blueprints API is strict to the Java convention. Given the scripting nature of Gremlin, many shorthand methods are provided. Such methods can be found in Gremlin Methods. Below is a grab bag of update operations in Gremlin.

gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> v = g.addVertex()    
==>v[0]
gremlin> v.name = 'marko'     
==>marko
gremlin> v.age = 31           
==>31
gremlin> v.map()              
==>name=marko
==>age=31
gremlin> u = g.addVertex([name:'pierre',location:'belgium'])
==>v[1]
gremlin> u.map()
==>location=belgium
==>name=pierre
gremlin> g.addEdge(v,u,'collaborator',[since:2011])
==>e[2][0-collaborator->1]
gremlin> v.outE.since
==>2011
gremlin> g.V              
==>v[1]
==>v[0]
gremlin> g.E
==>e[2][0-collaborator->1]
gremlin> g.removeVertex(v)
==>null
gremlin> g.V
==>v[1]
gremlin> g.E

The Helper Utilities of Blueprints

Blueprints comes with a collection of helper utilities that makes it easy to do the following operations:

  • Copy properties: ElementHelper.copyProperties(v,u)
  • Remove properties: ElementHelper.removeProperties(v)
  • Rename properties: ElementHelper.renameProperty('original', 'new', g.V)
  • Typecase properties: ElementHelper.typecastProperty('age', Integer.class, g.V)
  • ..and more.

Please refer to the Blueprints API for numerous handy operations to work with indices, transactions, etc.