how to add an edge between 2 vertices in different partitions - Azure Cosmos DB Gremlin

Omar Ghoche 0 Reputation points
2023-05-16T20:14:07.3233333+00:00

I am trying to add 2 vertices to different logical partitions, then i want to connect them through an edge, but couldn't find anything online! How to do it?!

vertices2partitions

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,543 questions
{count} votes

1 answer

Sort by: Most helpful
  1. GeethaThatipatri-MSFT 29,017 Reputation points Microsoft Employee
    2023-05-18T18:03:33.3933333+00:00

    Hi, @Omar Ghoche Welcome to the Microsoft Q&A platform, thanks for posting the question

    When you create your Gremlin API graph you define a partition key path. The value of the partition key for each vertex defines its logical partition. To create vertices in different partitions, you simply define each vertex with a different partition key value. You can then create an edge between the two vertices.

     For example, assuming you have a graph with partition key path /pk, the following two traversals will each create a vertex in a different logical partition:

     g.addV(‘vertex_label_1’).property(‘id’,’V1’).property(‘pk’,’pkX’)

    g.addV(‘vertex_label_2’).property(‘id’,’V2’).property(‘pk’,’pkY’)

     

    This traversal will then select the two existing vertices and create an edge between them:

     g.V(‘V1’).has(‘pk’,’pkX’).addE(‘edge_label_12’).property(‘id’,’E12’).to(g.V(‘V2’).has(‘pk’,’pkY’))

    Gremlin does not have transactions so it is possible that either of the addV steps may fail or that the sequentially executed traversals may beat the propagation delay for the new graph elements. If it’s possible that this can cause issues in your application, you can read this article on using idempotent queries to ensure the existence the graph elements:

    https://jayanta-mondal.medium.com/cosmos-db-graph-gremlin-api-how-to-executing-multiple-writes-as-a-unit-via-a-single-gremlin-2ce82d8bf365

    I hope this information helps

    Regards

    Geetha