2

I am trying to build a knowledge graph with Azure CosmosDB with Gremlin API. While trying to create vertex, I came across an unexpected error. I am trying to add 'person' as a node. Below is my simple code:

g.addV('author').property('id','A5048784797').property('name','{name}').property('pk','pk')

The variable {name} comes from pandas dataframe and one of the name is: 'T. J. O'Brien'

The single apostrophe in O'Brien results into following error:

Gremlin query syntax error: Unexpected token: ')'; in input: '('author').property('id','A5048784797').property('name','T. J. O'Brien)'. @ line 1, column 77

Client submission:

from gremlin_python.driver import client

client = client.Client(
    url=f"{COSMOS_GREMLIN_ENDPOINT}",
    traversal_source="g",
    username="*****",
    password=f"{COSMOS_GREMLIN_KEY}",
    message_serializer=serializer.GraphSONSerializersV2d0(),
)

So my question, is how can we handle this situation in gremlin. I have pandas dataframe with a column containing list of tuple of (id, name, institute,....). I expect this situation to appear more frequently. I would really appreciate your insights in this. Thank you

1 Answer 1

-1

It looks like I found a solution. I am creating a list of nodes to be sent to the gremlin client as:

['g.addV(a1).properties(p1)',
 'g.addV(a2).properties(p2)',
 .....
]

In order to create this I would do something like:

vertex=[]
for node in node_list():
    vertex.append(f"""g.addV('author').property('name',"{name}").property('pk','pk')""") 

The trick is to use f""" """ triple quotes. and use double quotes " " for name variable

The resulting codes looks like:

['g.addV(\'author\').property(\'name'\, "T. J. O'Brien")..... ]
Sign up to request clarification or add additional context in comments.

2 Comments

Perhaps this is specific to CosmosDB's implementation of Gremlin, but in general having to escape the single quotes like this should not be necessary (unless you are using something like curl to send queries). It's generally fine with Gremlin to use double quotes for the outer string and single quotes for the keys and values within the query.
It seems like that. But, I haven't figured out that. The way i am sending the query is through a client as shown in the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.