upsert_item到cosmosdb通过python

i7uq4tfw  于 2023-06-04  发布在  Python
关注(0)|答案(1)|浏览(169)

我有一个这样的代码:

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 = "dmdb"
container_name = "DataContract"

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消息:Message:{“Errors”:[“One of the specified inputs is invalid”]}这是我的查询中的一个示例:

{'version': 'V1', 'name': 'demo_contract2', 'title': 'title122', 'Theme': 'Theme12', 'description': 'test data contract management2', 'data owner': 'j.jansen@amsterdam.nl', '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': 'j.jansen@amsterdam.nl', 'confidentiality': 'open', 'table1': {'description:': 'testen', 'attribute_1': {'type': 'int', 'description:': 'testen', 'identifiability': 'identifiable'}}}

他们是匹配的。我应该如何在我的代码中调整我的upsert_item函数?

mnemlml8

mnemlml81#

您在Upsert内容中缺少id。文档的标识由id和Partition Key值的组合定义,如果您不指定id,则Upsert将始终表现为Create操作。
因为您是通过查询获取项目的,所以只需添加id

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

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

相关问题