Not able to setup Hierarchical partition keys on the portal of Azure Cosmos db emulator

537 Views Asked by At

Has anybody been able to setup hierarchical partition keys on Azure cosmos db emulator. I tried following this post from Microsoft -

https://learn.microsoft.com/en-us/azure/cosmos-db/hierarchical-partition-keys?tabs=net-v3%2Cbicep

This is what I have done so far -

  1. Uninstalled and installed latest version of the emulator.
  2. Started the emulator from the installation directory and ran this command in powershell - .\CosmosDB.Emulator.exe /EnablePreview

I didn't find the option to setup Hierarchical partition keys.

Screenshot of the New Container creation screen -

enter image description here

1

There are 1 best solutions below

0
Martin Smith On

I just downloaded the latest version of the Emulator and started it with the /EnablePreview flag.

Unfortunately the last time the bundled Data Explorer was updated was May 2022 according to the release notes - which predates this feature being added to the data explorer code base.

But the back end stuff appears to work fine.

Creating a collection with hierarchical partition key works fine if you connect to it and do it programmatically.

e.g. Using the following code based on the examples in the docs did work

using Microsoft.Azure.Cosmos;

//well known Emulator connection string
using CosmosClient client = new(
    "https://localhost:8081",
    "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");

;
await client.CreateDatabaseIfNotExistsAsync("soquestion");
var database = client.GetDatabase("soquestion");

List<string> subpartitionKeyPaths = new List<string> {
    "/TenantId",
    "/UserId",
    "/SessionId"
};

// Create a container properties object
ContainerProperties containerProperties = new ContainerProperties(
    id: "hierPKTest",
    partitionKeyPaths: subpartitionKeyPaths
);

// Create a container that's subpartitioned by TenantId > UserId > SessionId
Container container = await database.CreateContainerIfNotExistsAsync(containerProperties, throughput: 400);

var item = new 
{
    id = "f7da01b0-090b-41d2-8416-dacae09fbb4a",
    TenantId = "Microsoft",
    UserId = "8411f20f-be3e-416a-a3e7-dcd5a3c1f28b",
    SessionId = "0000-11-0000-1111"
};

// Specify the full partition key path when creating the item
PartitionKey partitionKey = new PartitionKeyBuilder()
    .Add(item.TenantId)
    .Add(item.UserId)
    .Add(item.SessionId)
    .Build();

// Create the item in the container
var createResponse = await container.CreateItemAsync(item, partitionKey);

Console.WriteLine("Done");

At the time of giving this answer interacting with these documents via data explorer was not possible and gave an error.

enter image description here

As of the latest version (2.14.16) I can now edit these documents in the bundled data explorer. I still don't see any UI support for creating collections with hierarchical partition keys in this though so probably this still needs to be done through code.