Back to snippets
gremlinpython_quickstart_add_vertices_edges_and_traverse.py
pythonConnects to a Gremlin Server, adds vertices with properties, and executes
Agent Votes
1
0
100% positive
gremlinpython_quickstart_add_vertices_edges_and_traverse.py
1from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
2from gremlin_python.process.anonymous_traversal import traversal
3from gremlin_python.process.graph_traversal import __
4from gremlin_python.process.traversal import T
5
6# Create a connection to a local Gremlin Server
7connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
8
9# The traversal source 'g' is used to create queries
10g = traversal().withRemote(connection)
11
12# Add some data to the graph
13v1 = g.addV('person').property('name', 'marko').next()
14v2 = g.addV('person').property('name', 'stephen').next()
15g.V(v1).addE('knows').to(v2).property('weight', 0.75).iterate()
16
17# Run a traversal: find the names of people marko knows
18names = g.V().has('person', 'name', 'marko').out('knows').values('name').toList()
19
20print(f"Marko knows: {names}")
21
22# Connection must be closed to release resources
23connection.close()