This post explains the fundamental techniques for creating, modifying, and deleting vertices and edges using Gremlin traversals. These operations form the backbone of graph mutation in Apache TinkerPop and are essential for building and evolving a graph model over time.
All examples consistently use the traversal source g, which represents the recommended and portable way to interact with a graph. Direct manipulation of the underlying graph object is intentionally avoided, as traversal based mutations are safer, more expressive, and compatible with remote execution through Gremlin Server.
1. Adding Vertices
Vertices represent entities in a graph such as people, systems, locations, or projects. Gremlin provides several flexible ways to create vertices, depending on the use case.
Before experimenting with the example, let's create a traversal object.
graph = TinkerGraph.open() g = graph.traversal()
$gremlin.sh \,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated: tinkerpop.server plugin activated: tinkerpop.utilities plugin activated: tinkerpop.sugar plugin activated: tinkerpop.tinkergraph gremlin> gremlin> graph = TinkerGraph.open() ==>tinkergraph[vertices:0 edges:0] gremlin> g = graph.traversal() ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
Basic Vertex Creation with a Label
The simplest form of vertex creation uses the addV() step with a label:
g.addV('person')
gremlin> g.addV('person') ==>v[0]
Adding a Vertex with Properties at Creation Time
In most real-world scenarios, a vertex is created along with descriptive properties. Gremlin supports this through chained property() steps:
g.addV('person'). property('name', 'Krishna'). property('age', 29). property('role', 'Engineer')
Each property() step mutates the vertex being created. This pattern keeps vertex definition concise and readable, making the intent of the traversal immediately clear.
gremlin> g.addV('person'). ......1> property('name', 'Krishna'). ......2> property('age', 29). ......3> property('role', 'Engineer') ==>v[1]
We created two vertices now, you can count the total vertices by executing following statement.
g.V().count()
gremlin> g.V().count() ==>2
Capturing a Newly Created Vertex
Often, a traversal needs to reuse a newly created vertex. For example, when creating edges immediately afterward. Calling next() materializes the vertex and returns it to the client.
ram = g.addV('person'). property('name', 'Ram'). property('age', 37). next()
gremlin> ram = g.addV('person'). ......1> property('name', 'Ram'). ......2> property('age', 37). ......3> next() ==>v[5] gremlin>
To inspect the full content of the vertex, start a traversal from the vertex reference and use elementMap() method.
gremlin> g.V(ram).valueMap() ==>[name:[Ram],age:[37]]
Creating Multiple Vertices in a Single Traversal
Gremlin allows multiple mutation steps within a single traversal.
g.addV('person').property('name', 'Lahari').property('age', '7'). addV('person').property('name', 'Thulasi').property('age', '5')
This style is particularly useful for quickly seeding test data or constructing small example graphs. Each addV() step creates a new vertex independently within the same traversal execution.
We can get the both created vertices using within method.
g.V(). hasLabel('person'). has('name', within('Lahari', 'Thulasi')). valueMap(true)
gremlin> g.addV('person').property('name', 'Lahari').property('age', '7'). ......1> addV('person').property('name', 'Thulasi').property('age', '5') ==>v[17] gremlin> gremlin> g.V(). ......1> hasLabel('person'). ......2> has('name', within('Lahari', 'Thulasi')). ......3> valueMap(true) ==>[id:17,label:person,name:[Thulasi],age:[5]] ==>[id:14,label:person,name:[Lahari],age:[7]]
Adding Multiple Properties
Multiple properties can be added or updated in a single traversal.
g.V(). hasLabel('person'). has('name', 'Lahari'). property('age', 27). property('hobbies', 'Trekking')
For example, above statement update age value to 27, and add new property hobbies to Lahari as it is not exists.
gremlin> g.V().hasLabel('person').has('name', 'Lahari').valueMap(true) ==>[id:26,label:person,name:[Lahari],age:[7]] gremlin> gremlin> g.V(). ......1> hasLabel('person'). ......2> has('name', 'Lahari'). ......3> property('age', 27). ......4> property('hobbies', 'Trekking') ==>v[26] gremlin> gremlin> g.V().hasLabel('person').has('name', 'Lahari').valueMap(true) ==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]]
In Gremlin, adding a property with an existing key replaces the old value (for single-valued properties).
Updating Based on Conditions
g.V(). hasLabel('person'). has('age', gt(25)). property('discount', 10)
This updates only vertices matching the condition, Add or update the property discount to 10 for the person age greater than 25.
gremlin> g.V().hasLabel('person').valueMap(true) ==>[id:0,label:person] ==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]] ==>[id:17,label:person,name:[Thulasi],age:[5]] ==>[id:5,label:person,name:[Ram],age:[37]] ==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]] gremlin> gremlin> g.V(). ......1> hasLabel('person'). ......2> has('age', gt(25)). ......3> property('discount', 10) ==>v[1] ==>v[5] ==>v[26] gremlin> gremlin> g.V().hasLabel('person').valueMap(true) ==>[id:0,label:person] ==>[id:1,label:person,role:[Engineer],name:[Krishna],discount:[10],age:[29]] ==>[id:17,label:person,name:[Thulasi],age:[5]] ==>[id:5,label:person,name:[Ram],discount:[10],age:[37]] ==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],discount:[10],age:[27]]
Deleting Vertex Properties
To remove a property, use properties() followed by drop().
g.V(). hasLabel('person'). has('name', 'Krishna'). properties('discount'). drop()
Above statement removes the property 'discount' to the person 'Krishna'.
gremlin> g.V().hasLabel('person').valueMap(true) ==>[id:0,label:person] ==>[id:1,label:person,role:[Engineer],name:[Krishna],discount:[10],age:[29]] ==>[id:17,label:person,name:[Thulasi],age:[5]] ==>[id:5,label:person,name:[Ram],discount:[10],age:[37]] ==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],discount:[10],age:[27]] gremlin> gremlin> gremlin> g.V(). ......1> hasLabel('person'). ......2> has('name', 'Krishna'). ......3> properties('discount'). ......4> drop() gremlin> gremlin> gremlin> g.V().hasLabel('person').valueMap(true) ==>[id:0,label:person] ==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]] ==>[id:17,label:person,name:[Thulasi],age:[5]] ==>[id:5,label:person,name:[Ram],discount:[10],age:[37]] ==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],discount:[10],age:[27]]
Following statement removes the discount property from all person vertices.
g.V(). hasLabel('person'). properties('discount'). drop()
gremlin> g.V().hasLabel('person').valueMap(true) ==>[id:0,label:person] ==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]] ==>[id:17,label:person,name:[Thulasi],age:[5]] ==>[id:5,label:person,name:[Ram],discount:[10],age:[37]] ==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],discount:[10],age:[27]] gremlin> gremlin> g.V(). ......1> hasLabel('person'). ......2> properties('discount'). ......3> drop() gremlin> gremlin> g.V().hasLabel('person').valueMap(true) ==>[id:0,label:person] ==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]] ==>[id:17,label:person,name:[Thulasi],age:[5]] ==>[id:5,label:person,name:[Ram],age:[37]] ==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]]
Deleting Multiple Properties
g.V(). hasLabel('person'). has('name', 'Krishna'). properties('role', 'age'). drop()
Above statement drop role and age properties for the person named 'Krishna'.
gremlin> g.V().hasLabel('person').valueMap(true) ==>[id:0,label:person] ==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]] ==>[id:17,label:person,name:[Thulasi],age:[5]] ==>[id:5,label:person,name:[Ram],age:[37]] ==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]] gremlin> gremlin> gremlin> g.V(). ......1> hasLabel('person'). ......2> has('name', 'Krishna'). ......3> properties('role', 'age'). ......4> drop() gremlin> gremlin> gremlin> g.V().hasLabel('person').valueMap(true) ==>[id:0,label:person] ==>[id:1,label:person,name:[Krishna]] ==>[id:17,label:person,name:[Thulasi],age:[5]] ==>[id:5,label:person,name:[Ram],age:[37]] ==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]]
2. Adding Edges
Edges represent relationships between vertices.
Adding an Edge Using to()
Let's create a project 'Jarvis'.
g.addV('project').property('name', 'Jarvis').next()
gremlin> g.addV('project').property('name', 'Jarvis').next() ==>v[0]
Let's create two person 'Abhishek', 'Narasimha'
gremlin> g.addV('person').property('name', 'Abhishek').next() ==>v[2] gremlin> g.addV('person').property('name', 'Narasimha').next() ==>v[4]
Let's print the vertices with names Jarvis, Abhishek and Narasimha.
gremlin> g.V().has('name', within('Jarvis', 'Abhishek', 'Narasimha')).valueMap(true) ==>[id:0,label:project,name:[Jarvis]] ==>[id:2,label:person,name:[Abhishek]] ==>[id:4,label:person,name:[Narasimha]]
Following statements adds and edge 'works_on' from Abhishek to Jarvis.
abhishek = g.V().hasLabel('person').has('name', 'Abhishek').next();[] jarvis = g.V().hasLabel('project').has('name', 'Jarvis').next();[] g.V(abhishek).addE('works_on').to(jarvis)
gremlin> abhishek = g.V().hasLabel('person').has('name', 'Abhishek').next();[] gremlin> jarvis = g.V().hasLabel('project').has('name', 'Jarvis').next();[] gremlin> gremlin> g.V(abhishek).addE('works_on').to(jarvis) ==>e[6][2-works_on->0]
Get all the persons who worked on the project 'jarvis'.
g.V(jarvis). in('works_on'). valueMap(true)
gremlin> g.V(jarvis). ......1> in('works_on'). ......2> valueMap(true) ==>[id:2,label:person,name:[Abhishek]]
Adding an Edge Using from()
g.V(jarvis). addE('manages'). from(narasimha) narasimha = g.V().hasLabel('person').has('name', 'Narasimha').next();[] jarvis = g.V().hasLabel('project').has('name', 'Jarvis').next();[]
gremlin> narasimha = g.V().hasLabel('person').has('name', 'Narasimha').next();[] gremlin> jarvis = g.V().hasLabel('project').has('name', 'Jarvis').next();[] gremlin> gremlin> g.V(jarvis). ......1> addE('manages'). ......2> from(narasimha) ==>e[7][4-manages->0]
Get the person who manages the project 'jarvis'
gremlin> g.V(jarvis).in('manages').valueMap(true) ==>[id:4,label:person,name:[Narasimha]]
Get the projects managed by Narasimha.
gremlin> g.V(narasimha).out('manages').valueMap(true) ==>[id:0,label:project,name:[Jarvis]]
Adding an Edge Using as() Labels
The as() step labels a vertex for later reference within the traversal.
joel = g.addV('person').property('name', 'Joel').next() alice = g.addV('project').property('name', 'alice').next() g.V().hasLabel('person').has('name', 'Joel').as('p'). V().hasLabel('project').has('name', 'alice'). addE('manages').from('p')
Here, above statements model the relation ship joel -> manages -> alice. The as() step labels a vertex for later reference within the traversal.
gremlin> joel = g.addV('person').property('name', 'Joel').next() ==>v[8] gremlin> gremlin> alice = g.addV('project').property('name', 'alice').next() ==>v[10] gremlin> gremlin> g.V().hasLabel('person').has('name', 'Joel').as('p'). ......1> V().hasLabel('project').has('name', 'alice'). ......2> addE('manages').from('p') ==>e[12][8-manages->10]
Adding Properties to an existing Edge
Edges can have properties just like vertices.
Get the Edge:
my_edge = g.V().has('person', 'name', 'Joel').outE('manages').next() g.E(my_edge).valueMap(true)
gremlin> my_edge = g.V().has('person', 'name', 'Joel').outE('manages').next() ==>e[12][8-manages->10] gremlin> gremlin> gremlin> g.E(my_edge).valueMap(true) ==>[id:12,label:manages]
Attach properties to the edge using property method.
g.E(my_edge). property('since', 2026). property('allocation', 35)
gremlin> g.E(my_edge). ......1> property('since', 2026). ......2> property('allocation', 35) ==>e[12][8-manages->10] gremlin> gremlin> g.E(my_edge).valueMap(true) ==>[id:12,label:manages,allocation:35,since:2026]
Adding Properties to an Edge At the time of Creation
You can add properties to an edge at the time of creation as well.
gopi = g.addV('person'). property('name', 'Gopi'). property('age', 41). next() galelio = g.addV('project'). property('name', 'Galelio'). next() g.V(gopi). addE('works_on'). to(galelio). property('since', 2024). property('employee_type', 'permanent')
Above statement add the properties 'since', 'employee_type' to the relation 'works_on'.
gremlin> gopi = g.addV('person'). ......1> property('name', 'Gopi'). ......2> property('age', 41). ......3> next() ==>v[13] gremlin> gremlin> galelio = g.addV('project'). ......1> property('name', 'Galelio'). ......2> next() ==>v[16] gremlin> gremlin> g.V(gopi). ......1> addE('works_on'). ......2> to(galelio). ......3> property('since', 2024). ......4> property('employee_type', 'permanent') ==>e[20][13-works_on->16] gremlin> gremlin> gremlin> g.V(gopi).outE('works_on').valueMap(true) ==>[id:20,label:works_on,employee_type:permanent,since:2024]
Updating an Edge Property
g.E(). hasLabel('manages'). has('allocation', 35). property('since', 2021)
Above statement updates the property 'since' to the value 2021
gremlin> g.E().valueMap(true) ==>[id:20,label:works_on,employee_type:permanent,since:2024] ==>[id:6,label:works_on] ==>[id:7,label:manages] ==>[id:12,label:manages,allocation:35,since:2026] gremlin> gremlin> gremlin> g.E(). ......1> hasLabel('manages'). ......2> has('allocation', 35). ......3> property('since', 2021) ==>e[12][8-manages->10] gremlin> gremlin> gremlin> g.E().valueMap(true) ==>[id:20,label:works_on,employee_type:permanent,since:2024] ==>[id:6,label:works_on] ==>[id:7,label:manages] ==>[id:12,label:manages,allocation:35,since:2021]
Deleting an Edge Properties
g.E(). properties('since'). drop()
Above snippet drops the property 'since' from all the edges.
gremlin> g.E().valueMap(true) ==>[id:20,label:works_on,employee_type:permanent,since:2024] ==>[id:6,label:works_on] ==>[id:7,label:manages] ==>[id:12,label:manages,allocation:35,since:2021] gremlin> gremlin> g.E(). ......1> properties('since'). ......2> drop() gremlin> gremlin> g.E().valueMap(true) ==>[id:20,label:works_on,employee_type:permanent] ==>[id:6,label:works_on] ==>[id:7,label:manages] ==>[id:12,label:manages,allocation:35]
Deleting the Entire Edge
g.E().hasLabel('works_on').drop()
Above statement drop all the edges with label 'works_on'.
gremlin> g.E().valueMap(true) ==>[id:20,label:works_on,employee_type:permanent] ==>[id:6,label:works_on] ==>[id:7,label:manages] ==>[id:12,label:manages,allocation:35] gremlin> gremlin> g.E().hasLabel('works_on').drop() gremlin> gremlin> g.E().valueMap(true) ==>[id:7,label:manages] ==>[id:12,label:manages,allocation:35]
Note:
· Always prefer g.addV() and g.addE() over direct graph mutations
· property() is used for create, update, and overwrite
· drop() removes properties, edges, or vertices
· as(), to(), and from() enable expressive, readable traversals
· Traversal based mutations are portable, safe, and Gremlin Server–friendly
Previous Next Home


No comments:
Post a Comment