Skip to content

Commit 83a6482

Browse files
committed
Update screenshots, first cosmos db lab, indexing section
1 parent 1ce0f49 commit 83a6482

File tree

12 files changed

+141
-132
lines changed

12 files changed

+141
-132
lines changed
0 Bytes
Loading
0 Bytes
Loading
0 Bytes
Loading
0 Bytes
Loading

02_Overview_Cosmos_DB/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ The focus for this developer guide is [Azure Cosmos DB for NoSQL](https://learn.
1616

1717
Azure Cosmos DB offers three capacity modes: provisioned throughput, serverless and autoscale modes. creating an Azure Cosmos DB account, it's essential to evaluate the workload's characteristics in order to choose the appropriate mode to optimize both performance and cost efficiency.
1818

19+
[**Serverless mode**](https://learn.microsoft.com/en-us/azure/cosmos-db/serverless) offers a more flexible and pay-as-you-go approach, where only the Request Units consumed are billed. This is particularly advantageous for applications with sporadic or unpredictable usage patterns, as it eliminates the need to provision resources upfront.
20+
1921
[**Provisioned throughput mode**](https://learn.microsoft.com/azure/cosmos-db/set-throughput) allocates a fixed amount of resources, measured in [Request Units per second (RUs/s)](https://learn.microsoft.com/azure/cosmos-db/request-units), which is ideal for applications with predictable and steady workloads. This ensures consistent performance and can be more cost-effective when there is a constant or high demand for database operations. RU/s can be set at both the database and container levels, allowing for fine-grained control over resource allocation.
2022

21-
[**Serverless mode**](https://learn.microsoft.com/en-us/azure/cosmos-db/serverless) offers a more flexible and pay-as-you-go approach, where only the Request Units consumed are billed. This is particularly advantageous for applications with sporadic or unpredictable usage patterns, as it eliminates the need to provision resources upfront.
23+
[**Autoscale mode**](https://learn.microsoft.com/azure/cosmos-db/provision-throughput-autoscale) builds upon the provisioned throughput mode but allows for the database or container automatically and instantly scale up or down resources based on demand, ensuring that the application can handle varying workloads efficiently. When configuring autoscale, a maximum (Tmax) value threshold is set for a predictable maximum cost. This mode is suitable for applications with fluctuating usage patterns or infrequently used applications.
2224

23-
[**Autoscale mode**](https://learn.microsoft.com/azure/cosmos-db/provision-throughput-autoscale) builds upon the provisioned throughput mode but allows for the database or container automatically and instantly scales up or down resources based on demand, ensuring that the application can handle varying workloads efficiently. When configuring autoscale, a maximum (Tmax) value threshold is set for a predictable maximum cost. This mode is suitable for applications with fluctuating usage patterns or infrequently used applications.
25+
[**Dynamic scaling**](https://learn.microsoft.com/en-us/azure/cosmos-db/autoscale-per-partition-region) allows for the automatic and independent scaling of non-uniform workloads across regions and partitions according to usage patterns. For instance, in a disaster recovery configuration with two regions, the primary region may experience high traffic while the secondary region can scale down to idle, thereby saving costs. This approach is also highly effective for multi-regional applications, where traffic patterns fluctuate based on the time of day in each region.

07_Create_First_Cosmos_DB_Project/README.md

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,66 @@ The following concepts are covered in detail in this lab:
4545
The `azure-cosmos` library is used to create a Cosmos DB API for NoSQL database client. The client enables both DDL (data definition language) and DML (data manipulation language) operations.
4646

4747
```python
48-
client = cosmos_client.CosmosClient(HOST, {'masterKey': MASTER_KEY} )
48+
# Initialize the Cosmos DB client
49+
client = CosmosClient.from_connection_string(CONNECTION_STRING)
4950
```
5051

5152
### Creating a database
5253

5354
The `create_database` method is used to create a database. If the database already exists, an exception is thrown, therefore verify the database already exists before creating it.
5455

5556
```python
56-
client.create_database(id=id)
57+
db: DatabaseProxy = client.create_database(database_name)
5758
```
5859

5960
### Creating a container
6061

61-
In progress
62+
The `create_container_if_not_exists` method is used to create a container. If the container already exists, the method will retrieve the existing container.
6263

63-
### Creating a document
64+
```python
65+
container: ContainerProxy = db.create_container_if_not_exists(
66+
id="product",
67+
partition_key={"paths": ["/categoryId"], "kind": "Hash"}
68+
)
69+
```
6470

65-
In progress
71+
### Creating or Updating a document (Upsert)
6672

67-
### Reading a document
73+
One method of creating a document is using the `create_item` method. This method takes a single document and inserts it into the database, if the item already exists in the container, and exception is thrown. Alternatively, the `upsert_item` method can also be used to insert a document into the database and in this case, if the document already exists, it will be updated.
74+
75+
```python
76+
# Create a document
77+
container.upsert_item(product_dict)
78+
```
6879

69-
In progress
80+
### Reading documents
7081

71-
### Updating a document
82+
The `read_item` method can be used to retrieve a single document if both the `id` value and `partition_key` value are known. Otherwise, the `query_items` method can be used to retrieve a list of documents using a [SQL-like query](https://learn.microsoft.com/azure/cosmos-db/nosql/tutorial-query).
7283

73-
In progress
84+
```python
85+
items = container.query_items(query="SELECT * FROM prod", enable_cross_partition_query=True)
86+
```
7487

7588
### Deleting a document
7689

77-
In progress
90+
The `delete_item` method is used to delete a document from the container.
91+
92+
```python
93+
container.delete_item(item=product.id, partition_key=product.category_id)
94+
```
95+
96+
## Cosmos DB indexing
97+
98+
Azure Cosmos DB automatically indexes all properties for all items in a container. However, the creation of additional indexes can improve performance and add functionality such as spatial querying and vector search.
99+
100+
The following indexes are supported by Azure Cosmos DB:
101+
102+
The **Range Index** supports efficient execution of queries involving numerical and string data types. It is optimized for inequality comparisons (<, <=, >, >=) and sorting operations. Range indexes are particularly useful for time-series data, financial applications, and any scenario that requires filtering or sorting over a numeric range or alphabetically ordered strings.
103+
104+
The **Spatial Index** excels with geospatial data types such as points, lines, and polygons. Spatial queries include operations such as finding intersections, conducting proximity searches, and handling bounding-box queries. Spatial indexes are crucial for applications that require geographic information system (GIS) capabilities, location-based services, and asset tracking.
105+
106+
The **Composite Index** combines multiple properties into a single entry, optimizing complex queries that use multiple properties for filtering and sorting. They significantly improve the performance of multidimensional queries by reducing the number of request units (RUs) consumed during these operations.
78107

79-
### Querying documents
108+
The **Vector Index** is specialized for high-dimensional vector data. Use cases include similarity searches, recommendation systems, and any other application requiring efficient handling of high-dimensional vectors. This index type optimizes the storage and retrieval of vectors typically utilized in AI application patterns such as RAG (Retrieval Augmented Generation).
80109

81-
In progress
110+
Learn more about indexing in the [Azure documentation](https://learn.microsoft.com/azure/cosmos-db/index-overview)
0 Bytes
Loading

Labs/.env.EXAMPLE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
DB_CONNECTION_STRING="AccountEndpoint=https://<cosmos-account-name>.documents.azure.com:443/;AccountKey=<cosmos-account-key>;"
1+
COSMOS_DB_CONNECTION_STRING="AccountEndpoint=https://<cosmos-account-name>.documents.azure.com:443/;AccountKey=<cosmos-account-key>;"
22
AOAI_ENDPOINT = "https://<resource>.openai.azure.com/"
33
AOAI_KEY = "<key>"

Labs/lab_0_explore_and_use_models.ipynb

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@
1515
"\n",
1616
"When integrating Azure OpenAI service in a solution written in Python, the OpenAI Python client library is used. This library is maintained by OpenAI, and is compatible with the Azure OpenAI service.\n",
1717
"\n",
18-
"Install the latest `openai` client library:"
19-
]
20-
},
21-
{
22-
"cell_type": "code",
23-
"execution_count": null,
24-
"metadata": {},
25-
"outputs": [],
26-
"source": [
27-
"! pip install openai"
28-
]
29-
},
30-
{
31-
"cell_type": "markdown",
32-
"metadata": {},
33-
"source": [
3418
"When using the OpenAI client library, the Azure OpenAI `key` and `endpoint` for the service are needed. In this case, ensure the Azure OpenAI `key` and `endpoint` is located in a `.env` file in the root of this project, you will need to create this file. The `.env` file should contain the following values (replace the value with your own `key` and `endpoint`):\n",
3519
"\n",
3620
"```\n",
@@ -71,14 +55,14 @@
7155
},
7256
{
7357
"cell_type": "code",
74-
"execution_count": 8,
58+
"execution_count": 5,
7559
"metadata": {},
7660
"outputs": [],
7761
"source": [
7862
"chatClient = AzureOpenAI(\n",
7963
" azure_endpoint=os.getenv(\"AOAI_ENDPOINT\"), \n",
8064
" api_key=os.getenv(\"AOAI_KEY\"), \n",
81-
" api_version=\"2023-05-15\"\n",
65+
" api_version=\"2024-06-01\"\n",
8266
")"
8367
]
8468
},
@@ -139,7 +123,7 @@
139123
"name": "python",
140124
"nbconvert_exporter": "python",
141125
"pygments_lexer": "ipython3",
142-
"version": "3.12.3"
126+
"version": "3.11.5"
143127
}
144128
},
145129
"nbformat": 4,

0 commit comments

Comments
 (0)