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
|
# -------------------------------------------------------------------------
# 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
import uuid
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
def create_client_with_throughput_bucket(host=HOST, master_key=MASTER_KEY):
cosmos_client.CosmosClient(host, master_key,
throughput_bucket=1)
# Applies throughput bucket 2 for read item requests
def container_read_item_throughput_bucket(client):
database = client.get_database_client(DATABASE_ID)
created_container = database.create_container(
str(uuid.uuid4()),
PartitionKey(path="/pk"))
created_document = created_container.create_item(body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'})
created_container.read_item(
item=created_document['id'],
partition_key="mypk",
throughput_bucket=2)
database.delete_container(created_container.id)
# Applies throughput bucket 3 for create item requests
def container_create_item_throughput_bucket(client):
database = client.get_database_client(DATABASE_ID)
created_container = database.create_container(
str(uuid.uuid4()),
PartitionKey(path="/pk"))
created_container.create_item(
body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'},
throughput_bucket=3)
database.delete_container(created_container.id)
# Applies throughput bucket 3 for create item requests and bucket 4 for delete item requests
def container_create_and_delete_item_throughput_bucket(client):
database = client.get_database_client(DATABASE_ID)
created_container = database.create_container(
str(uuid.uuid4()),
PartitionKey(path="/pk"))
created_item = created_container.create_item(
body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'},
throughput_bucket=3)
created_container.delete_item(
created_item['id'],
partition_key='mypk',
throughput_bucket=4)
database.delete_container(created_container.id)
# Applies throughput bucket 1 to all requests from a client application, and bucket 2 to create item requests
def create_client_and_item_with_throughput_bucket(host=HOST, master_key=MASTER_KEY):
client = cosmos_client.CosmosClient(host, master_key,
throughput_bucket=1)
database = client.get_database_client(DATABASE_ID)
created_container = database.create_container(
str(uuid.uuid4()),
PartitionKey(path="/pk"))
created_container.create_item(
body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'},
throughput_bucket=2)
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
def container_create_upsert_and_delete_item_throughput_bucket(client):
database = client.get_database_client(DATABASE_ID)
created_container = database.create_container(
str(uuid.uuid4()),
PartitionKey(path="/pk"))
created_item = 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):
created_container.upsert_item(
dict(id="item{}".format(i), pk='mypk', throughput_bucket=4))
created_container.delete_item(
created_item['id'],
partition_key='mypk',
throughput_bucket=5)
database.delete_container(created_container.id)
def run_sample():
client = cosmos_client.CosmosClient(HOST, {'masterKey': MASTER_KEY} )
client.create_database_if_not_exists(id=DATABASE_ID)
try:
# creates client
create_client_with_throughput_bucket()
# reads an item from a container
container_read_item_throughput_bucket(client)
# writes an item to a container
container_create_item_throughput_bucket(client)
# creates and deletes an item to a container
container_create_and_delete_item_throughput_bucket(client)
# creates a client and item with separate throughput buckets
create_client_and_item_with_throughput_bucket()
# creates an item, upserts multiple items, and deletes an item all on separate throughput buckets
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__':
run_sample()
|