Updating a Graph

jbmusso edited this page Jul 11, 2016 · 7 revisions

Attention: this Wiki hosts an outdated version of the TinkerPop framework and Gremlin language documentation.

Please visit the Apache TinkerPop website and latest documentation.

Many of the examples in the Gremlin documentations are 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.
To set double or float values you have to explicitly specify the datatypes as the .dot notation won’t work.

Below is a grab bag of update operations in Gremlin.

gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> v = g.addVertex()
gremlin> v.name = 'marko'
gremlin> v.age = 31
gremlin> v.height = 186.5d
gremlin> v.weight = 72.4f
gremlin> v.map()
gremlin> u = g.addVertex([name:'pierre',location:'belgium'])
gremlin> u.map()
gremlin> g.addEdge(v,u,'collaborator',[since:2011])
gremlin> v.outE.since
gremlin> g.V
gremlin> g.E
gremlin> g.removeVertex(v)
gremlin> g.V
gremlin> g.E
gremlin> g.V.as('x').linkOut('knows','x')
gremlin> g.E
gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.v(1).out.name
gremlin> g.v(1).out.sideEffect{it.name = it.name + '!'}.name
gremlin> g.v(1).out.name
gremlin> g.v(1).addEdge('coworker',g.v(2)).since = 2008
gremlin> g.v(1).outE('coworker')
gremlin> g.v(1).outE('coworker').since

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.