2

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

Sumchans
  • 3,088
  • 6
  • 32
  • 59
  • Looks like you enabled hierarchical partition keys via command line (the Preview flag). Now please edit to show your code for setting up a container with hierarchical partition key. – David Makogon Aug 17 '23 at 12:30
  • @DavidMakogon I have added the screenshot of the new container creation screen. – Sumchans Aug 17 '23 at 15:29

1 Answers1

1

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");

Though I imagine you can write off any prospect of interacting with these documents via data explorer until this is updated.

enter image description here

Maybe it would be possible to just clone the cosmos-explorer repo and build it locally to connect to the emulator endpoint to get access to the latest and greatest features but this isn't something I explored doing.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845