File: throughput_bucket_management_async.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (170 lines) | stat: -rw-r--r-- 6,808 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
from azure.cosmos.aio import CosmosClient
import azure.cosmos.exceptions as exceptions
from azure.cosmos.partition_key import PartitionKey

import uuid
import asyncio
import config

# ----------------------------------------------------------------------------------------------------------
# Prerequisites -
#
# 1. An Azure Cosmos account -
#    https://azure.microsoft.com/documentation/articles/documentdb-create-account/
#
# 2. Microsoft Azure Cosmos PyPi package -
#    https://pypi.python.org/pypi/azure-cosmos/
# ----------------------------------------------------------------------------------------------------------
# Sample - demonstrates the basic throughput bucket operations at the client, database, container and item levels.
#
# 1. Setting throughput buckets at the Client Level
#
# 2. Setting Throughput Buckets at the Item Level
#    2.1 - Read Item
#    2.2 - Create Item
#
# 3. Multi-Bucket Usage
#    3.1 - Create and Delete Item with Separate Buckets
#    3.2 - Create Client and Create Item with Separate Buckets
#    3.3 - Create, Upsert, and Delete Item with Separate Buckets
# ----------------------------------------------------------------------------------------------------------
# Note -
#
# Running this sample will create (and delete) multiple Databases and Containers on your account.
# Each time a Container is created the account will be billed for 1 hour of usage based on
# the provisioned throughput (RU/s) of that account.
# ----------------------------------------------------------------------------------------------------------

HOST = config.settings['host']
MASTER_KEY = config.settings['master_key']
DATABASE_ID = config.settings['database_id']
CONTAINER_ID = config.settings['container_id']

# Applies throughput bucket 1 to all requests from a client application
async def create_client_with_throughput_bucket(host=HOST, master_key=MASTER_KEY):
    async with CosmosClient(host, master_key, throughput_bucket=1) as client:
        pass

# Applies throughput bucket 2 for read item requests
async def container_read_item_throughput_bucket(client):
    database = client.get_database_client(DATABASE_ID)
    created_container = await database.create_container(
        str(uuid.uuid4()),
        PartitionKey(path="/pk"))
    created_document = await created_container.create_item(body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'})

    await created_container.read_item(
         item=created_document['id'],
         partition_key="mypk",
         throughput_bucket=2)

    await database.delete_container(created_container.id)

# Applies throughput bucket 3 for create item requests
async def container_create_item_throughput_bucket(client):
    database = client.get_database_client(DATABASE_ID)

    created_container = await database.create_container(
        str(uuid.uuid4()),
        PartitionKey(path="/pk"))

    await created_container.create_item(
        body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'},
        throughput_bucket=3)

    await database.delete_container(created_container.id)

# Applies throughput bucket 3 for create item requests and bucket 4 for delete item requests
async def container_create_and_delete_item_throughput_bucket(client):
    database = client.get_database_client(DATABASE_ID)

    created_container = await database.create_container(
        str(uuid.uuid4()),
        PartitionKey(path="/pk"))

    created_item = await created_container.create_item(
        body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'},
        throughput_bucket=3)

    await created_container.delete_item(
        created_item['id'],
        partition_key='mypk',
        throughput_bucket=4)

    await database.delete_container(created_container.id)

# Applies throughput bucket 1 to all requests from a client application, and bucket 2 to create item requests
async def create_client_and_item_with_throughput_bucket(host=HOST, master_key=MASTER_KEY):
    async with CosmosClient(host, master_key,
        throughput_bucket=1) as client:

        database = client.get_database_client(DATABASE_ID)

        created_container = await database.create_container(
            str(uuid.uuid4()),
            PartitionKey(path="/pk"))

        await created_container.create_item(
            body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'},
            throughput_bucket=2)

        await database.delete_container(created_container.id)

# Applies throughput bucket 3 for create item requests, bucket 4 for upsert item requests, and bucket 5 for delete item
async def container_create_upsert_and_delete_item_throughput_bucket(client):
    database = client.get_database_client(DATABASE_ID)

    created_container = await database.create_container(
        str(uuid.uuid4()),
        PartitionKey(path="/pk"))

    created_item = await created_container.create_item(
        body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'},
        throughput_bucket=3)

    # add items for partition key 1
    for i in range(1, 3):
        await created_container.upsert_item(
            dict(id="item{}".format(i), pk='mypk', throughput_bucket=4))

    await created_container.delete_item(
        created_item['id'],
        partition_key='mypk',
        throughput_bucket=5)
    database.delete_container(created_container.id)

async def run_sample():
    async with CosmosClient(HOST, {'masterKey': MASTER_KEY} ) as client:
        await client.create_database_if_not_exists(id=DATABASE_ID)
        try:
            # creates client
            await create_client_with_throughput_bucket()

            # reads an item from a container
            await container_read_item_throughput_bucket(client)

            # writes an item to a container
            await container_create_item_throughput_bucket(client)

            # creates and deletes an item to a container
            await container_create_and_delete_item_throughput_bucket(client)

            # creates a client and item with separate throughput buckets
            await create_client_and_item_with_throughput_bucket()

            # creates an item, upserts multiple items, and deletes an item all on separate throughput buckets
            await container_create_upsert_and_delete_item_throughput_bucket(client)

        except exceptions.CosmosHttpResponseError as e:
            print('\nrun_sample has caught an error. {0}'.format(e.message))

        finally:
            print("\nrun_sample done")

if __name__ == '__main__':
    asyncio.run(run_sample())