简体   繁体   English

如何获取 Python SDK API 中 cosmos 查询消耗了多少请求单位

[英]How to get how much requests units consumed by cosmos query in Python SDK API

How to check how many Request units consumed for each requests in Azure comsos DB query using python sdk.如何使用 python sdk 检查 Azure comsos DB 查询中每个请求消耗了多少请求单位。

Below code only prints output of the response from particular ReadItem, I also interested in how many request units consumed by query.下面的代码仅打印来自特定 ReadItem 的响应的 output,我也对查询消耗了多少请求单元感兴趣。

# -------------------------------------------------------------------------
# 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()

I tried below options我尝试了以下选项

 request_charge = client.last_response_headers['x-ms-request-charge']

But I am getting below error但是我遇到了以下错误

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'

You need to access container.client_connection not the client,您需要访问 container.client_connection而不是客户端,

request_charge = container.client_connection.last_response_headers['x-ms-request-charge']

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

相关问题 Azure Cosmos DB Python SDK:如何读取更改提要? - Azure Cosmos DB Python SDK : How to read change feed? 如何使用 Python 中的请求包通过 API 密钥获取 API 答案 - How to get API answer with an API key using the requests package in Python 如何通过 Requests 和 Python 查询和登录 Bill.com API? - How to query and login into the Bill.com API via Requests and Python? Python 品脱单位如何使用货币单位? - How are currency units used with Python Pint units? 如何使用Cassandra API将Python的cosmos_client连接到Cosmos数据库实例? - How to connect Python's cosmos_client to Cosmos DB instance using Cassandra API? 如何使用 Python 更新 Cosmos 表 API 中实体的属性 - How to update a property in an Entity in Cosmos table API using Python 如何在 Python 中格式化对 Blogger API 的获取请求? - How do I format get requests to Blogger API in Python? 如何使用 Instagram Private Api 获得超过 100 个单位? - How to get more than 100 units using Instagram Private Api? 如何通过facebook-sdk python api获取用户帖子? - How to get user posts through facebook-sdk python api? 如何使用 python 的库 cosmpy 获取 cosmos 钱包余额? - How to get cosmos wallet balance with python's library cosmpy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM