Skip to content

Batch query using Neptune Gremlin client

0

In my Neptune cluster, I have vertices with a timestamp field. I would like to batch-query for vertices in the following way: I have a list of timestamps, and for each timestamp, I'd like to get one vertex with that timestamp.

I tried the below code

def get_node_batch_by_timestamps(iso_timestamps):
    clauses = [f"g.V().has('{NEPTUNE_PARTITION_KEY}', '{ts}').limit(1)" for ts in iso_timestamps]
    gremlin_script = f"""
        g.union(
            {','.join(clauses)}
        )
    """
    print(f"gremlin_script: {gremlin_script}")
        
    gremlinClient = get_gremlin_client()
    try:
        response = gremlinClient.submit(gremlin_script).all().result()
        print(f"batch query response: {response}")
        return response
    finally:
        gremlinClient.close()

However, I am getting "Exception processing a script on request" error.

Please let me know how I should adjust my query.

asked a year ago426 views

2 Answers
0

You can try removing the g from your individual clause queries. As of TinkerPop 3.5.0, you cannot use a GraphTraversalSource object in a child traversal within a query. Since you're just using strings, you should build your child traversals starting with V().

However, I don't really think what you're doing here is going to perform very well in Neptune. I just tried doing something similar on a 1.4.5.0 cluster and the query was non-optimal.

Instead, you could try doing something like:

g.V().has('{NEPTUNE_PARTITION_KEY}',within({ts1},{ts2},...{tsn})).
    group().by('{NEPTUNE_PARTITION_KEY}').
    unfold().
    select(values).local(unfold().limit(1))

This would work so long as the number of items returned for each timestamp is small. Roughly how many vertices do you have per timestamp?

AWS

answered a year ago

  • Thanks for your answer. At the moment, I have between 1-10 vertices per timestamp. Though I plan to increase this number to the thousands.

-2

Thank you for sharing your code and the issue you're experiencing. I can suggest a few adjustments to improve your batch query approach for Neptune using the Gremlin client.

First, it's important to note that Neptune uses a different query processing engine than standard TinkerPop implementations. As a result, some Groovy-style scripting features are not supported. Your current approach of building a complex script with multiple clauses might be causing the issue.

Instead, I recommend using the Gremlin Language Variant (GLV) client to create a batch programmatically. Here's an adjusted version of your code that should work better:

def get_node_batch_by_timestamps(iso_timestamps):
    gremlinClient = get_gremlin_client()
    try:
        g = gremlinClient.traversal()
        traversal = g.V()
        
        for ts in iso_timestamps:
            traversal = traversal.union(
                __.has(NEPTUNE_PARTITION_KEY, ts).limit(1)
            )
        
        response = traversal.toList()
        print(f"batch query response: {response}")
        return response
    finally:
        gremlinClient.close()

This approach builds a single traversal using the GLV, which is more efficient and less prone to errors. It uses the union step to combine multiple sub-traversals, each looking for a vertex with a specific timestamp and limiting the result to one vertex per timestamp.

A few key points about this approach:

  1. It creates a single traversal object and builds upon it, which is more efficient than sending multiple separate queries.
  2. It uses the union step to combine multiple sub-traversals, allowing for parallel execution of the queries for each timestamp.
  3. The toList() method at the end executes the traversal and returns the results.

This method should be more reliable and efficient, especially when dealing with a large number of timestamps. It avoids the need to construct a complex Gremlin script as a string, which can be error-prone and less performant.

Remember to ensure that your get_gremlin_client() function is properly configured to connect to your Neptune cluster. Also, make sure you're using the latest version of the Gremlin client library compatible with your Neptune version to take advantage of the most recent improvements and bug fixes.

If you're still encountering issues after trying this approach, it would be helpful to check the specific error message and Neptune server logs for more detailed information about the exception.

Sources
Creating Efficient Multithreaded Gremlin Writes - Amazon Neptune
Gremlin standards compliance in Amazon Neptune - Amazon Neptune

answered a year 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.