Search code examples
pythonamazon-web-servicestinkerpopamazon-neptune

Use gremlin module for Python to add vertex to Amazon Neptune graph database


Synopsis

I am trying to use Python on an Amazon EC2 instance, in the same VPC as an Amazon Neptune instance, to add a vertex (node) to Neptune. Using the example code for Amazon Neptune, I constructed the following code sample to add a vertex (node) and then print out all vertices in the graph.

from gremlin_python import statics
from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.structure.graph import Graph

from pprint import pprint


target = 'zzzzzz-instance-1.zzzzzzzz.us-west-2.neptune.amazonaws.com'
port = '8182'

g = Graph()

remote = DriverRemoteConnection(f'wss://{target}:{port}/gremlin', 'g')
conn = g.traversal().withRemote(remote)
conn.addV('person').property(id, 5).property('firstname', 'Trevor')
print(conn.V().toList())

Actual Behavior

The invocation to conn.addV() silently fails, and the following line with the print() statement returns an empty Python list.

Expected Behavior

  1. The call to conn.addV() throws an exception, or ...
  2. The call to conn.addV() succeeds, and the print() line returns the vertex

Question: How do I use the Gremlin module for Python to add a vertex to the graph, in Amazon Neptune?


Solution

  • Your addV() query needs a Terminal Step such as next() or iterate(). Otherwise, you get the bytecode representation of the query returned. http://www.kelvinlawrence.net/book/PracticalGremlin.html#var