Edges properties and Iterate()

31 views
Skip to first unread message

Alexandre Poirier

unread,
Apr 8, 2022, 3:17:20 AM4/8/22
to Gremlin-users

Hi,

I’m currently new to gremlin and gremlin python. I’m having no problem running the following query to add an edge (I'm storing all my queries in a list and executing them somewhere else).

for row_dict in tqdm(df_edges.to_dict(orient="records")):

            for i in range(len(row_dict["Site ID"])-1):

                new_edge = 'g.V().hasLabel("Site").has("id", "{}").outE("SENDS_TO").\

                    where(__.inV().has("id", "{}")).fold().coalesce(__.unfold(),__.addE("SENDS_TO").\

                    from_(__.V().hasLabel("Site").has("id", "{}")).to(__.V().hasLabel("Site").\

                    has("id", "{}"))).iterate()'.format(

                    row_dict["Site ID"][i],

                    row_dict["Site ID"][i+1],

                    row_dict["Site ID"][i],

                    row_dict["Site ID"][i+1]

                )

                _gremlin_insert_edges.append(new_edge)

The query above has no problem running, everything works as expected. However, when I want to add properties to those edges it doesn’t work anymore.

for row_dict in tqdm(df_edges.to_dict(orient="records")):

            for i in range(len(row_dict["Site ID"])-1):

                new_edge = 'g.V().hasLabel("Site").has("id", "{}").outE("SENDS_TO").\

                    where(__.inV().has("id", "{}")).fold().coalesce(__.unfold(),__.addE("SENDS_TO").\

                    from_(__.V().hasLabel("Site").has("id", "{}")).to(__.V().hasLabel("Site").\

                    has("id", "{}")).property("count", 0)).sack(Operator.assign).by("count").sack(sum).\

                    by(__.constant(1)).property("count", __.sack()).iterate()'.format(

                    row_dict["Site ID"][i],

                    row_dict["Site ID"][i+1],

                    row_dict["Site ID"][i],

                    row_dict["Site ID"][i+1]

                )

                _gremlin_insert_edges.append(new_edge)

 

The problem seems to be related to when I add a property to my edges. It doesn’t seem like it’s a syntax error. Here’s how the code works, it’s adding a property called count and it’s setting it to 0 if it’s the first time this edge was created. However, if another edge is about to be created between the same two sites, it will increment the count property by one instead of creating a new edge. Let me know if I’m wrong and if there is a way to improve this query.

The error I get is extremely long, but it seems to be a problem with the sum, see below a snippet.

in finalize_message

    message = json.dumps(message)

TypeError: <built-in function sum> is not JSON serializable

ERROR:asyncio:Error on reading from the event loop self pipe

loop: <ProactorEventLoop running=True closed=False debug=False>

 

Does anyone know how to fix this? Thank you.

Reply all
Reply to author
Forward
0 new messages