删除并重新创建具有不同分区键的 Cosmos DB 容器...使用相同的分区键创建

Delete and re-create Cosmos DB containers with different partition key ... Creates with same partition key

我在 Cosmos DB 中有一个笔记本,我正在尝试删除和创建多个容器。

我们有各种各样的环境,无法访问 Live,所以我需要这个脚本来完成所有工作。

我有以下容器(和分区键)

ContA(第 1 部分)

续(第 2 部分)

续集(第 3 部分)

我知道我不能更改分区键,所以我想删除容器并使用新的分区键创建以下容器

ContA(第 4 部分)

续(第 4 部分)

续集(第 4 部分)

我正在使用以下代码来执行此操作

from azure.cosmos.partition_key import PartitionKey

# Create new database
database = cosmos_client.get_database_client("MyCosmosDB")

# FIRST SCRIPT
# Get a list of all current containers and if there are any, 
# iterate through and delete them all
containers = list(database.list_containers())
if containers:
    for container in containers:        
        print("Deleting the " + container['id'] + " container...")
        try:                   
            database.delete_container(container['id'])           
        except errors.CosmosHttpResponseError as e:
            raise
else:
    print("Ther are no containers in container to delete in this database")

# SECOND SCRIPT
# Create containers
ContAContainer = database.create_container(id='ContA', partition_key=PartitionKey(path='/part4'))
print("Creating ContA container...\n")
ContBContainer = database.create_container(id='ContB', partition_key=PartitionKey(path='/part4'))
print("Creating ContB container...\n")
ContCContainer = database.create_container(id='ContC', partition_key=PartitionKey(path='/part4'))
print("Creating ContC container...\n")

问题: 如果我 运行 # FIRST SCRIPT 和# SECOND SCRIPT 分开,一切都很好。 如果我 运行 此脚本的第一部分(删除),则可以很好地删除容器。

如果我 运行 此脚本的第二部分(创建),则使用第 4 部分分区键可以很好地创建它们。

但是当旧容器就位时,当我像上面那样 运行 将它们放在一起时,容器会保留其原始分区键...因此 ContA 仍然具有 part1 作为分区键,而不是 part4 中定义的# 第二个脚本。

任何人都可以告诉我这里发生了什么,以及我如何从这个脚本创建具有正确分区键的容器。

我试过你的代码。

but with the old containers in place, When I run them together like above, the containers keep their original partition keys... so ContA still has part1 as it's partition key, not part4 as defined in the # SECOND SCRIPT.

这似乎是一个显示错误。在我执行笔记本后,我点击 Refresh tree 按钮,它仍然如您所述显示原始分区键(“/part1”,“/part2”,“/part3”)。但是当我刷新我的浏览器页面时,分区键将是“/part4”。