Graph db with Gremlin property syntax error

53 views Asked by At

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

There are 1 answers

2
stackword_0 On

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")..... ]