作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使用 python sdk 检查 Azure comsos DB 查询中每个请求消耗了多少个请求单元。
下面的代码仅打印特定 ReadItem 的响应输出,我也对查询消耗了多少请求单元感兴趣。
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
import azure.cosmos.cosmos_client as cosmos_client
import azure.cosmos.exceptions as exceptions
from azure.cosmos.partition_key import PartitionKey
from config import configs
HOST = configs['host']
MASTER_KEY = configs['key']
DATABASE_ID = configs['database_id']
CONTAINER_ID = configs['container_id']
client = cosmos_client.CosmosClient(HOST, {'masterKey': MASTER_KEY} )
def read_item(container, doc_id):
id = "9fedcb0991553b94b6e79595c9c26922b3c480940fc024fe4acd7dbad122d66b"
pk= "/c/file1"
response = container.read_item(item=id, partition_key=pk)
print(response)
def run_sample():
try:
# setup database for this sample
db = client.create_database_if_not_exists(id=DATABASE_ID)
# setup container for this sample
container = db.create_container_if_not_exists(id=CONTAINER_ID, partition_key=PartitionKey(path='/file_path', kind='Hash'))
read_item(container)
except exceptions.CosmosHttpResponseError as e:
print('\nrun_sample has caught an error. {0}'.format(e.message))
finally:
print("\nrun_sample done")
if __name__ == '__main__':
run_sample()
我尝试了以下选项
request_charge = client.last_response_headers['x-ms-request-charge']
但是我遇到了以下错误
run_sample done
Traceback (most recent call last):
File "/Users/vcimalap/Library/CloudStorage/OneDrive-Microsoft/my_code/my_test_code/k8s/smb.yaml/cosmos_db/query.py", line 197, in <module>
run_sample()
File "/Users/vcimalap/Library/CloudStorage/OneDrive-Microsoft/my_code/my_test_code/k8s/smb.yaml/cosmos_db/query.py", line 175, in run_sample
read_item(container, item)
File "/Users/vcimalap/Library/CloudStorage/OneDrive-Microsoft/my_code/my_test_code/k8s/smb.yaml/cosmos_db/query.py", line 55, in read_item
request_charge = client.last_response_headers['x-ms-request-charge']
AttributeError: 'CosmosClient' object has no attribute 'last_response_headers'
最佳答案
您需要access container.client_connection不是客户端,
request_charge = container.client_connection.last_response_headers['x-ms-request-charge']
关于python - 如何在Python SDK API中获取cosmos查询消耗了多少请求单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72315838/
我是一名优秀的程序员,十分优秀!