[英]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.