通过python将_item更新到cosmosdb

问题描述 投票:0回答:1

我有一个代码:

import logging
import json
import os
from azure.cosmos import CosmosClient
import azure.functions as func

url = os.environ["ACCOUNT_URI"]
key = os.environ["ACCOUNT_KEY"]
client1 = CosmosClient(url, key)
client = CosmosClient.from_connection_string(os.environ["CosmosDBConnStr"])

database_name = ""
container_name = ""

database = client.get_database_client(database_name)
container = database.get_container_client(container_name)

logging.info(f"container: {url}")
def main(myblob: func.InputStream, doc: func.Out[func.Document]):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n")
    #reading file from blob
    contract_data=myblob.read()  
    
    try:
        logging.info(f"contract data: {contract_data}")
        contract_json = json.loads(contract_data)       
        version = contract_json.get("version")
        name = contract_json.get("name")
        title = contract_json.get("title")
        logging.info(f"contract json: {contract_json}")
        query = "SELECT c.version,c.name,c.title,c.Theme,c.description,c['data owner'],c.confidentiality,c.table1 FROM c "
      
       
        items = list(container.query_items(
            query=query,
            enable_cross_partition_query=True
        ))
        logging.info(f"item: {items[0]}")
        for item in items:
            if item["name"] == name and item["version"] == version:
                if item["title"] == title:
                    logging.info(f"Skipping, item already exists: {item}")
                    return  # Skip saving the document
                
                container.upsert_item(body=contract_json,pre_trigger_include = 
                None,post_trigger_include= None)
                return

        doc.set(func.Document.from_json(contract_data))
                       
    except Exception as e:
            logging.info(f"Error: {e}")

我在 cosmosdb 中添加了一个文档,我想替换具有不同标题的同一文档。但我无法做到这一点,我收到代码:BadRequest 消息:消息:{“错误”:[“指定的输入之一无效”]} 这是我的查询中的项目示例:

{'version': 'V1', 'name': 'demo_contract2', 'title': 'title122', 'Theme': 'Theme12', 'description': 'test data contract management2', 'data owner': '[email protected]', 'confidentiality': 'open', 'table1': {'description:': 'testen', 'attribute_1': {'type': 'int', 'description:': 'testen', 'identifiability': 'identifiable'}}}

这是我文件中的contract_json 示例:

{'version': 'V1', 'name': 'demo_contrac1', 'title': 'title1', 'Theme': 'Theme1', 'description': 'test data contract management2', 'data owner': '[email protected]', 'confidentiality': 'open', 'table1': {'description:': 'testen', 'attribute_1': {'type': 'int', 'description:': 'testen', 'identifiability': 'identifiable'}}}

他们很匹配。我应该如何在代码中调节 upsert_item 函数?

python azure-functions azure-cosmosdb upsert
1个回答
0
投票

您在更新插入内容中缺少

id
。文档的标识由
id
和分区键值的组合定义,如果您不指定
id
,则 Upsert 将始终表现为创建操作。

因为您是通过查询获取项目,所以只需添加

id
:

query = "SELECT c.id, c.version,c.name,c.title,c.Theme,c.description,c['data owner'],c.confidentiality,c.table1 FROM c "
     

您现在可以从

id
获取
item["id"]
以便在需要时使用。 Upsert 操作的主体应包含
id
,如果它与现有文档匹配,则该文档将被更新。

© www.soinside.com 2019 - 2024. All rights reserved.