|
1 | | -# Load data into Azure Cosmos DB API for NoSQL |
| 1 | +# Load data into Azure Cosmos DB for NoSQL |
2 | 2 |
|
3 | | -The previous lab demonstrated how to add data to a container individually. This lab will demonstrate how to load data using bulk operations into multiple containers. This data will be used in subsequent labs to explain further the capabilities of Azure Cosmos DB API about AI. |
4 | | - |
5 | | -When loading data, bulk operations are preferred over adding each document individually. Bulk operations involve performing multiple database operations as a batch rather than executing them simultaneously. This approach is more efficient and provides several benefits: |
6 | | - |
7 | | -1. Performance: By issuing load operations in bulk, the lab can significantly reduce the overhead of network round-trips and database operations. This results in faster data loading and improved overall performance. |
8 | | - |
9 | | -2. Scalability: Bulk operations allow the lab to handle large volumes of data efficiently. They can quickly process and load a substantial amount of customer, product, and sales data, enabling them to scale their operations as needed. |
10 | | - |
11 | | -3. Atomicity: Bulk operations ensure that all database changes within a batch are treated as a single transaction. The entire batch can be rolled back if any document fails to load, maintaining data integrity and consistency. |
12 | | - |
13 | | -4. Simplified code logic: By using bulk operations, the lab can simplify its code logic and reduce the number of database queries. This results in cleaner, more manageable code and reduces the likelihood of errors or inconsistencies. |
| 3 | +To support future labs and exercises, the Cosmic Works data must be loaded into Azure Cosmos DB for NoSQL containers. This lab will demonstrate how to load the Cosmic Works Customer, Product, and Sales data into Azure Cosmos DB for NoSQL containers. |
14 | 4 |
|
15 | 5 | ## Lab - Load data into Azure Cosmos DB for NoSQL containers |
16 | 6 |
|
17 | | -This lab will load the Cosmic Works Customer, Product, and Sales data into Azure Cosmos DB for NoSQL containers using bulk operations. Both the Azure Cosmos DB Emulator and Azure Cosmos DB account in Azure are supported for completion of this lab. |
| 7 | +This lab will load the Cosmic Works Customer, Product, and Sales data into Azure Cosmos DB for NoSQL containers using bulk operations. |
18 | 8 |
|
19 | 9 | >**Note**: It is highly recommended to use a [virtual environment](https://python.land/virtual-environments/virtualenv) for all labs. |
20 | 10 |
|
21 | 11 | Please visit the lab repository to complete [this lab](../Labs/lab_2_load_data.ipynb). |
22 | | - |
23 | | -This lab demonstrates the use of bulk operations to load product, customer, and sales data into Azure Cosmos DB for NoSQL containers. As an example, the following code snippet inserts product data using the `bulk_write` method allowing for upsert functionality using the `UpdateOne` method: |
24 | | - |
25 | | -```python |
26 | | -# Add product data to database using bulkwrite and updateOne with upsert |
27 | | -# Get cosmic works product data from github |
28 | | -product_raw_data = "https://cosmosdbcosmicworks.blob.core.windows.net/cosmic-works-small/product.json" |
29 | | -product_data = ProductList(items=[Product(**data) for data in requests.get(product_raw_data).json()]) |
30 | | -db.products.bulk_write([ UpdateOne({"_id": prod.id}, {"$set": prod.model_dump(by_alias=True)}, upsert=True) for prod in product_data.items]) |
31 | | -``` |
0 commit comments