How to access newly-created vertex id from gremlin client response?

0

After submitting query against Neptune DB to create a new vertex, I'm not sure how to handle the response I get from gremlin client. It says the response is of type vertex, but I haven't been able to figure out how to 'look inside' this object to get any of its properties. Please advise- I'm looking for the vertex id in particular.

from gremlin_python.driver import client, serializer
gremlinClient = client.Client(
    f"wss://{<neptuneEndpoint>}:{<neptunePort>}/gremlin",
    "g"
)

# should return id of newly-created node 
def add_vertex(node_data):
    label = node_data.get('label', 'event')
    properties = node_data.get('properties', {})

    query = f"g.addV('{label}')"
    for k, v in properties.items():
        query += f".property('{k}', '{v}')"

    response = gremlinClient.submit(query).all().result()
    return response   // please help me get the newly-created vertex id here
asked 3 months ago135 views
2 Answers
2

To access the newly-created vertex ID from the Gremlin client response in Neptune, you need to modify your code slightly. The response you receive from the Gremlin client is indeed a vertex object, but you need to extract the ID from it.

Here's how you can modify your add_vertex function to return the ID of the newly created vertex:

def add_vertex(node_data):
label = node_data.get('label', 'event')
properties = node_data.get('properties', {})

query = f"g.addV('{label}')"
for k, v in properties.items():
query += f".property('{k}', '{v}')"
query += ".id()"  # Add this to return the ID

response = gremlinClient.submit(query).all().result()

# The response is a list with a single item (the ID)
if response and len(response) > 0:
return response[0]  # Return the ID
else:
return None  # Or handle the error as appropriate

In this modified version:

  1. We've added ".id()" to the end of the Gremlin query. This tells Neptune to return the ID of the newly created vertex.

  2. The response from gremlinClient.submit(query).all().result() will now be a list containing a single item, which is the ID of the new vertex.

  3. We check if the response exists and has at least one item, then return the first (and only) item in the list, which is the ID.

This approach directly returns the ID as part of the Gremlin query, which is more efficient than returning the entire vertex and then extracting the ID from it. The ID returned will be a string that uniquely identifies the newly created vertex in your Neptune database.

Remember to handle potential errors, such as network issues or database constraints, that might prevent the vertex from being created successfully.
Sources
Gremlin query status API - Amazon Neptune
GetGremlinQueryStatus - Neptune Data API

profile picture
answered 3 months ago
AWS
SUPPORT ENGINEER
reviewed 3 months ago
profile picture
EXPERT
reviewed 3 months ago
1

As written your query will return a list of one or more vertex objects. To extract the id from one of those is as simple as doing

response = gremlinClient.submit(query).all().result()
print(response[0].id)

Or, as the GenAI produced answer suggested, you could just add .id() to the end of the query if all you want to get back is the vertex ID.

AWS
MODERATOR
answered 3 months ago
profile picture
EXPERT
reviewed 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions