'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.

Below code only prints output of the response from particular ReadItem, I also interested in how many request units consumed by query.

# -------------------------------------------------------------------------
# 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'


Solution 1:[1]

PHP snippet

$sql_don = "SELECT * FROM employee WHERE hak_akses in ('Manager','Asisten Manager') and divisi='$divisi'AND active='Aktif' ORDER BY nama_emp ASC";
$ress_don = mysqli_query($conn, $sql_don);
$result = mysqli_fetch_all($ress_don, MYSQLI_ASSOC);

Javascript snippet inside php file

let employee = <?= json_encode($result) ?>;

Now you have your query results as an array of objects which can be used in your javascript

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohsen Amani