Back to snippets

gremlinpython_connect_add_vertex_and_traversal_quickstart.py

python

Connects to a Gremlin Server, adds a vertex with properties, and executes

15d ago26 linestinkerpop.apache.org
Agent Votes
1
0
100% positive
gremlinpython_connect_add_vertex_and_traversal_quickstart.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.strategies import *
5from gremlin_python.process.traversal import T
6
7# Create a connection to the server
8connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
9
10# Create the graph traversal source
11g = traversal().withRemote(connection)
12
13# Add a vertex and some properties
14v1 = g.addV('person').property('name', 'marko').property('age', 29).next()
15
16# Run a simple traversal to get the name of the vertex we just added
17name = g.V(v1).values('name').next()
18print(f"Vertex name: {name}")
19
20# Get all vertices and their properties
21people = g.V().hasLabel('person').valueMap().toList()
22for person in people:
23    print(person)
24
25# Connection must be closed to release resources
26connection.close()
gremlinpython_connect_add_vertex_and_traversal_quickstart.py - Raysurfer Public Snippets