Вот скрипт:
Код: Выделить всё
from dotenv import load_dotenv
from azure.cosmos import CosmosClient
import os
import uuid
def update():
load_dotenv()
# Initialize the Cosmos DB client
endpoint = os.getenv("COSMOSDB_ENDPOINT")
key = os.getenv("COSMOSDB_KEY")
client = CosmosClient(endpoint, key)
# Specify your database and container (collection) names
database_name = os.getenv("COSMOSDB_DB")
container_name = os.getenv("COSMOSDB_CONTAINER")
# Retrieve an item by its ID
container = client.get_database_client(database_name).get_container_client(container_name)
id = str(uuid.uuid4())
url = "https://google.com/"
container.create_item(
{
"id": id,
"status": "pending", # => started, analyzing, finished
"url": url,
"categories": ["7149b375-8cb2-4180-ae03-27fd0da409d0"],
"doctype": "url"
}
)
query_text = f"SELECT * FROM c WHERE c.id='{id}'"
query_items_response = container.query_items(query=query_text, enable_cross_partition_query=True)
data = list(query_items_response)
print(len(data)) # returns 1
for item in data:
print(f"Updating {item['url']}")
item["categories"] = []
item["doctype"] = "pdfDocument"
container.upsert_item(item)
updated_query_items_response = container.query_items(query=query_text, enable_cross_partition_query=True)
updated_data = list(updated_query_items_response)
print(updated_data)
print(len(updated_data)) # returns 0
# Confirm created data using CosmosDB data explorer.
if __name__ == "__main__":
update()
Однако на самом деле он был удален. Даже после некоторого ожидания я не могу получить документ ни через код, ни в обозревателе данных Cosmos DB.
И я обнаружил странную проблему.
Если Я меняю item["doctype"] = "pdfDocument" на item["doctype"] = "site" или другие, все работает как положено.
Это довольно странно.
Подробнее здесь: https://stackoverflow.com/questions/792 ... s-document
Мобильная версия