CosmosDb Graph: move node (and its edges) to different partition? - azure-cosmosdb

I'm using a partitioned CosmosDB graph collection.
Is there a simple way to "move" a node (and its outbound links) from one partition to another? Can this be done atomically?
I tried this: (the partition key is '/tenantId')
//create the node
g.addV('testme').property('id','id123').property('tenantId','mytenant1')
//...create more nodes and edges...
//change node's partition key
g.V('id123').has('tenantId','mytenant1').property('tenantId','mytenant2')
// ^^^ fails:
// GraphRuntimeException ExceptionMessage :
// Gremlin Query Execution Error:
// Update Vertex Properties: The partition property cannot be updated

As the error explains, the partition key value cannot be updated. It's immutable.
However, if you delete the document and add it with an updated partition key value then that will work. Keep in mind that whatever you code in order to do that should have some rollback logic in it in case the new insertion fails.

Related

Is it possible to check if a logical partition exist in Azure Cosmos DB?

Is there any Partition key not found exception when we query with partitionkey via QueryRequestOptions? or is there any other ways I can be notified that the logical partition does not exist in a query?
Is there any Partition key not found exception when we query with
partitionkey via QueryRequestOptions?
Assuming you are talking about the value of the partition key, as far as I know there is no such thing in Cosmos DB.
or is there any other ways I can be notified that the logical
partition does not exist in a query?
One possible way to find out is query your container with the partition key value in the query and try to fetch at most one document. If you don't get any documents back (i.e. get empty resultset), that would mean the logical partition does not exist in the container.

How to use the Partition Key in CosmosBD via SDK or via Select QUERY

Consider Below is my sample json.
{
"servletname": "cofaxEmail",
"servlet-class": "org.cofax.cds.EmailServlet",
"init-param": {
"mailHost": "mail1",
"mailHostOverride": "mail2"
}
i have chosen "servletname" as my primary key as i will receive it in every request plus few 1000 server names are there it could be the best PK.
My Question is, to make the partition key work for me.
Do i have to specify the partition key option seperately like below
ItemResponse<ServerDto> ServerDtoResponse = await this.container.ReadItemAsync<ServerDto>(bocServerDto.mailHost, new PartitionKey(bocServerDto.servletname));
or
Including the partition key in the select query itself , without adding seperate new PartitionKey(), like
select * from r where r.servletname='cofaxEmail' and r.mailHost='mail1';
Crux of the question is: By passing partitionKey object in where condition of select query is it enough to utilize the partition key feature?
Thanks
For any crud operation you would pass in the value for the partition key. For example, on a point read.
ItemResponse<ServerDto> ServerDtoResponse = await this.container.ReadItemAsync<ServerDto>(bocServerDto.mailHost, new PartitionKey("cofaxEmail"));
For a query, you can either pass it in the queryRequest options or use it in the query as the first filter predicate. Here is an example of using the queryRequest options.
thanks.

Insert vertex into local Cosmos DB Emulator using Gremlin Console

I am trying to add vertices (and eventually edges) to a local Cosmos DB graph using the Gremlin console. I've been following this tutorial. However, whenever I try to add a vertex, I get an error about the partition key.
My query:
g.addV('person').property('firstName', 'Thomas').property('lastName', 'Andersen').property('age', 44).property('userid', 1).property('pk', 'pk')
The error:
ActivityId : cd07f7be-d824-40fa-8137-0f2726a9c26d
ExceptionType : GraphRuntimeException
ExceptionMessage :
Gremlin Query Execution Error: Cannot add a vertex where the partition key property has value 'null'.
Source : Microsoft.Azure.Cosmos.Gremlin.Core
GremlinRequestId : cd07f7be-d824-40fa-8137-0f2726a9c26d
Context : graphcompute
Scope : graphcomp-execquery
GraphInterOpStatusCode : GraphRuntimeError
HResult : 0x80131500
Type ':help' or ':h' for help.
Display stack trace? [yN]
How can I fix my query and insert the data?
Did the same mistake of mixing the two values up. So when you add your azure database, you have to specify a partition key, I picked '/client';
Now when I do my query, I have to add this property:
.property('client', 'pk')
-- the first value has to be the key itself, and the second one 'pk', short for 'partitionKey'; Then in your document you have to add a property:
client: 'TESTCLIENTID'
But again, a lot of this is about what your 'partitioning' strategy is based on, which is something you have to decide upfront for each collection, this video from Azure explains things in more detail quite good.
I don't have a CosmosDB test environment, but there's a public sample project:
Getting started with Azure Cosmos DB: Graph API
Looks like you have to add a pk property (which most likely means "partition key", and should be configurable somehow).
You don't need to add a partition key in your g.addV i looked at what the "Quick start" tab creates for you in the portal which is the "graphdb/mycollection" database/collection. You can create your own which works fine without specifying partition key when adding a vertex... Just specify
Partition key
/_partitionKey
and check the checkbox
My partition key is larger than 100 bytes
That solved it for me anyway.
I had mixed up the partition key label and value. Reversing these fixed my issue.

Janusgraph/TinkerPop - Constraint violation - How to add or update an existing Vertex

I have defined in my schema constraints to ensure uniqueness of given vertices based one or more properties. For example:
mgmt.buildIndex('byTenandIdUnique',Vertex.class).addKey(tenantId).unique().buildCompositeIndex()
As expected now, when I try to add a Vertex that already exists, I am getting an error like below:
aiogremlin.exception.GremlinServerError: 500: Adding this property for key [tenantId] and value [ACME2_AX2] violates a uniqueness constraint [byTenandIdUnique]
I am writing a Python application to load log files, with Goblin OGM, so it is expected that the data will repeat, and I don't want multiple instances of the same Vertex, hence the constraint.
Is there a way in TinkerPop or JanusGraph to update a Vertex in case it already exists instead of throwing this exception? Or is this something that the OGM should handle or maybe the code itself by querying the graph before any transaction?
TinkerPop does not do anything to enforce a schema, so the schema restriction here is specific to JanusGraph. The behavior is as you described: if you have a unique index defined and then attempt to add another element that conflicts with an existing element, an exception is thrown.
From a JanusGraph perspective, your logic will need to account for this properly. The code below is based on a common recipe using the coalesce() step that you can read more about here.
// check for existence of a vertex with the tenantId property
// if the vertex exists, return that vertex
// else create a new vertex with the tenantId
v = g.V().property("tenantId", "ACME2_AX2").fold(). \
coalesce( __.unfold(), __.addV().property("tenantId", "ACME2_AX2") ). \
next();
I don't use Goblin, so I'm not aware of whether Goblin is capable of handling this or whether it passes that responsibility on to the app developer, but checking for existence before setting the property is still an appropriate way to handle the situation.

In Azure Cosmos DB, can we change partition key later on once we decided at the beginning

I am new to Cosmos DB and I noticed that we can set the partition key based on needs to scale effectively through code like this:
DocumentCollection myCollection = new DocumentCollection();
myCollection.Id = "coll";
myCollection.PartitionKey.Paths.Add("/deviceId");
Question is can we change the partition key later on after we created the collection and specified the partition key? As I may find out that the choice of partition key is not proper later.
Changing the partition key is not supported (see e.g. https://learn.microsoft.com/en-us/rest/api/cosmos-db/replace-a-collection). You would need to create a new collection.

Resources