Gremlin Query Language – Create Vertex/Node and Edge/Relationship

Gremlin Query Language – Create Vertex/Node and Edge/Relationship

  1. Open Gremlin Console
  2. Initial command to work/test with inmemory storage
graph = TitanFactory.build().set('storage.backend','inmemory').open()
g=graph.traversal(standard())

Example 1:

Create Vertex. Label property is optional. The below queries will return vertex references.

graph.addVertex('property1','value1','property2','value2');
OR
graph.addVertex(label,'USER','property1','value1','property2','value2');
OR
graph.addVertex(T.label,'USER','property1','value1','property2','value2');
OR
graph.addVertex(T.label,'USER').property('property1','value1').property('property2','value2');
OR
graph.addVertex('LABEL_NAME');

Example 2:

Create Edge with/without properties using system generated unique ID

g.V(123).next().addEdge('EDGE_LABEL',g.V(456).next())
OR
g.E(g.V(123).next().addEdge('EDGE_LABEL',g.V(456).next())).property('property1','value1').property('property2','value2')

Example 3:

Create Edge with/without properties using custom filter properties.

g.E(g.V().hasLabel('USER').has('property','value').next().addEdge('FRIEND',g.V().hasLabel('USER').has('property','value').next())).property('property1','value1').property('property2','value2')

Example 4:

Get vertices with only required properties

g.V().valueMap('property1','property2')

[ YOU MAY ALSO LIKE ]

Leave a Reply