简体   繁体   English

如何使用 Python 更新 Cosmos 表 API 中实体的属性

[英]How to update a property in an Entity in Cosmos table API using Python

I have the below table in CosmosDB.我在 CosmosDB 中有下表。

PartitionKey  Rowkey  Group  Salary
John          HR      A      100000
Mark          DOC     B      200000

I want to update the Salary property in the first entity.我想更新第一个实体中的 Salary 属性。 When I tried to update the salary property in the first entity, the complete entity is being replaced instead of updating the salary property.当我尝试更新第一个实体中的 salary 属性时,整个实体被替换而不是更新 salary 属性。

Could someone let me know how to update an property in a entity in CosmosDB table API.谁能告诉我如何更新 CosmosDB 表 API 中实体的属性。

from azure.data.tables import TableServiceClient
from datetime import datetime


my_entity = {
   "PartitionKey" : "John",
    "RowKey" : "HR",
    "Group": "A",
    "Salary": 100000
  
}
table_service_client = TableServiceClient.from_connection_string(conn_str="")
table_client = table_service_client.get_table_client(table_name="my_table")

entity = table_client.create_entity(entity=my_entity)


created = table.get_entity(partition_key=my_entity["PartitionKey"], row_key=my_entity["RowKey"])
created["Salary"] = "200"
table.update_entity(mode=UpdateMode.REPLACE, entity=created)

Please try by changing the following lines of code:请尝试更改以下代码行:

created = table.get_entity(partition_key=my_entity["PartitionKey"], row_key=my_entity["RowKey"])
created["Salary"] = "200"
table.update_entity(mode=UpdateMode.REPLACE, entity=created)

with

created = table_client.get_entity(partition_key=my_entity["PartitionKey"], row_key=my_entity["RowKey"])
created["Salary"] = "200"
table_client.update_entity(mode=UpdateMode.MERGE, entity=created)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python更新Cosmos Db中的记录? - How To Update Record in Cosmos Db using python? 更新/合并操作在Azure Cosmos DB(表API)中找不到实体 - Update/merge operation can't find Entity in Azure Cosmos DB (Table API) 如何使用Cassandra API将Python的cosmos_client连接到Cosmos数据库实例? - How to connect Python's cosmos_client to Cosmos DB instance using Cassandra API? 如何获取财产实体的维基数据 ID? 是否有可用于 python 的 API? - How to get Wikidata ID for an entity of a property ? Is there an API available for python? 使用 python 的 azure cosmos db SQL API 地理空间数据 - Geospatial data with azure cosmos db SQL API using python 如何使用 python 中的 API 更新 Strava 活动? - How to update Strava activities using API in python? 如何使用 SqlAlchemy 和 python 更新表 - How to update a table using SqlAlchemy with python 如何使用 Python 使用代理 IP 连接到 Cosmos DB - How to connect to Cosmos DB using Proxy IP using Python 如何在odoo 9中使用python更新父实体特定字段 - How to update the parent entity specific field using python in odoo 9 触发器拒绝来自 python 的 cosmos DB 更新 - Trigger to reject cosmos DB update from python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM