簡體   English   中英

如何獲取 Cosmos DB 中存在的數據庫及其狀態的吞吐量值?

[英]How can I get throughput value for the databases present in the Cosmos DB and their state?

我想獲取數據庫的詳細信息,或者我可以說 Cosmos DB 中存在的所有數據庫的吞吐量值,還想檢查數據庫是否處於活動狀態?

是否有相同的 API?如果沒有,我如何獲得數據庫的吞吐量和狀態?

我已經瀏覽了 Cosmos DB 的文檔,但沒有找到任何幫助。

I want to get the throughput value for all the databases present in the Cosmos DB.

當您使用Python SDK時,您可以通過 request_charge 獲取它,

Python SDK 中的 CosmosClient 對象(請參閱此快速入門了解其用法)公開了一個 last_response_headers 字典,該字典映射了底層 HTTP API 為上次執行的操作返回的所有標頭。 請求費用在 x-ms-request-charge 密鑰下可用。

response = client.ReadItem('dbs/database/colls/container/docs/itemId', { 'partitionKey': 'partitionKey' })
request_charge = client.last_response_headers['x-ms-request-charge']

Python Azure SDK (>=4.0.0) 示例

from azure.cosmos import CosmosClient

class AzureHelper(object):

    def __init__(self, url, key):
        self.url = url
        self.key = key
        self._client = None
        self._keyspace = None

    def connect(self):
        self._client = CosmosClient(self.url, self.key)

    def get_list_keyspaces(self):
        result = []
        for db in self._client.list_databases():
            result.append(db['id'])
        return result

    def switch_keyspace(self, keyspace):
        self._keyspace = self._client.get_database_client(keyspace)

    def get_list_tables(self):
        result = []
        for table in self._keyspace.list_containers():
            result.append(table['id'])
        return result

    def get_throughput(self, table):
        container = self._keyspace.get_container_client(table)
        offer = container.read_offer()
        throughput = offer.properties['content']['offerThroughput']
        return throughput

if __name__ == '__main__':
    url = '<Cosmos DB URL, like https://testdb.cosmos.azure.com:443>'
    key = '<Primary Password>'
    az = AzureHelper(url, key)
    az.connect()
    for keyspace in az.get_list_keyspaces():
        az.switch_keyspace(keyspace)
        for table in az.get_list_tables():
            throughput = az.get_throughput(table)
            print(f'keyspace {keyspace}, table {table}, throughput {throughput}')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM