Continuing from the last time, I would like to dig a little deeper into the code. Last proceedings → https://qiita.com/komiyasa/items/ae34fd9fec46c0e01b35
The following is the whole. In this whole, set the name of the database and the name of the container. It seems that the specific contents are set in another Python file.
Program.py
from azure.cosmos import exceptions, CosmosClient, PartitionKey
import family
# Initialize the Cosmos client
endpoint = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# <create_cosmos_client>
client = CosmosClient(endpoint, key)
# </create_cosmos_client>
# Create a database
# <create_database_if_not_exists>
database_name = 'AzureSampleFamilyDatabase'
database = client.create_database_if_not_exists(id=database_name)
# </create_database_if_not_exists>
# Create a container
# Using a good partition key improves the performance of database operations.
# <create_container_if_not_exists>
container_name = 'FamilyContainer'
container = database.create_container_if_not_exists(
id=container_name,
partition_key=PartitionKey(path="/lastName"),
offer_throughput=400
)
# </create_container_if_not_exists>
# Add items to the container
family_items_to_create = [family.get_andersen_family_item(), family.get_johnson_family_item(), family.get_smith_family_item(), family.get_wakefield_family_item()]
# <create_item>
for family_item in family_items_to_create:
container.create_item(body=family_item)
# </create_item>
# Read items (key value lookups by partition key and id, aka point reads)
# <read_item>
for family in family_items_to_create:
item_response = container.read_item(item=family['id'], partition_key=family['lastName'])
request_charge = container.client_connection.last_response_headers['x-ms-request-charge']
print('Read item with id {0}. Operation consumed {1} request units'.format(item_response['id'], (request_charge)))
# </read_item>
# Query these items using the SQL query syntax.
# Specifying the partition key value in the query allows Cosmos DB to retrieve data only from the relevant partitions, which improves performance
# <query_items>
query = "SELECT * FROM c WHERE c.lastName IN ('Wakefield', 'Andersen')"
items = list(container.query_items(
query=query,
enable_cross_partition_query=True
))
request_charge = container.client_connection.last_response_headers['x-ms-request-charge']
print('Query returned {0} items. Operation consumed {1} request units'.format(len(items), request_charge))
# </query_items>
client = CosmosClient(endpoint, key)
The endpoint and Key are set here. Set the value to client by getting the Key of the deployed Cosmos DB from the Azure portal.
database_name = 'AzureSampleFamilyDatabase'
database = client.create_database_if_not_exists(id=database_name)
I'm creating a new database, with the condition that if I don't have a name on it. Here we created a database called ʻAzureSampleFamilyDatabase`.
container_name = 'FamilyContainer'
container = database.create_container_if_not_exists(
id=container_name,
partition_key=PartitionKey(path="/lastName"),
offer_throughput=400
)
Now create a container named FamilyContainer
. This is also conditional if not.
for family_item in family_items_to_create:
container.create_item(body=family_item)
Now enter the value of Family_item
into the container.
for family in family_items_to_create:
item_response = container.read_item(item=family['id'], partition_key=family['lastName'])
request_charge = container.client_connection.last_response_headers['x-ms-request-charge']
print('Read item with id {0}. Operation consumed {1} request units'.format(item_response['id'], (request_charge)))
##Query execution using SQL query syntax
query = "SELECT * FROM c WHERE c.lastName IN ('Wakefield', 'Andersen')"
items = list(container.query_items(
query=query,
enable_cross_partition_query=True
))
request_charge = container.client_connection.last_response_headers['x-ms-request-charge']
print('Query returned {0} items. Operation consumed {1} request units'.format(len(items), request_charge))
The easiest arrangement is to change the database name and container name in this code and see if the changes are reflected in the Azure portal. It's a little, but let's change it like this.
database_name = 'AzureSampleFamilyDatabase2'
database = client.create_database_if_not_exists(id=database_name)
The output result of the command is as follows.
PS C:\Users\komiyasa\Val\sample\azure-cosmos-db-python-getting-started> python .\cosmos_get_started.py
Read item with id Andersen_c23dcf0d-de55-43eb-afbe-e102ebb22dcc. Operation consumed 1 request units
Read item with id Smith_efc4369e-0c4b-4609-8dfc-6799df71f07d. Operation consumed 1 request units
Read item with id Johnson_d84a50c0-ce2e-4b53-a3e9-08104a1a1b7d. Operation consumed 1 request units
Read item with id Wakefield_a667cfc4-a266-4b63-a104-a27e636039e8. Operation consumed 1 request units
If you take a look at the Azure portal, you'll see that you have a database with a new name, with the same content in it.
This is a convenient sample code because it is very easy to use and can be deployed intuitively. Next time, I'd like to check the Delete / Update method in SQL using the Azure Python SDK.
Recommended Posts