|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Load data into Cosmos DB using the MongoDB API\n", |
| 8 | + "\n", |
| 9 | + "This notebook demonstrates how to load data into Cosmos DB from Cosmic Works JSON files into the database using the MongoDB API." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import os\n", |
| 19 | + "import pymongo\n", |
| 20 | + "import requests\n", |
| 21 | + "from pymongo import UpdateOne, DeleteMany\n", |
| 22 | + "from models import Product, ProductList, Customer, CustomerList, SalesOrder, SalesOrderList\n", |
| 23 | + "from dotenv import load_dotenv" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "## Establish a connection to the database" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "load_dotenv()\n", |
| 40 | + "CONNECTION_STRING = os.environ.get(\"DB_CONNECTION_STRING\")\n", |
| 41 | + "client = pymongo.MongoClient(CONNECTION_STRING)\n", |
| 42 | + "# Create database to hold cosmic works data\n", |
| 43 | + "# MongoDB will create the database if it does not exist\n", |
| 44 | + "db = client.cosmic_works" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "# empty the collections\n", |
| 54 | + "db.products.bulk_write([DeleteMany({})])\n", |
| 55 | + "db.customers.bulk_write([DeleteMany({})])\n", |
| 56 | + "db.sales.bulk_write([DeleteMany({})])" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + "## Load products" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [ |
| 72 | + "# Add product data to database using bulkwrite and updateOne with upsert\n", |
| 73 | + "# Get cosmic works product data from github\n", |
| 74 | + "product_raw_data = \"https://cosmosdbcosmicworks.blob.core.windows.net/cosmic-works-small/product.json\"\n", |
| 75 | + "product_data = ProductList(items=[Product(**data) for data in requests.get(product_raw_data).json()])\n", |
| 76 | + "db.products.bulk_write([ UpdateOne({\"_id\": prod.id}, {\"$set\": prod.model_dump(by_alias=True)}, upsert=True) for prod in product_data.items])" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "## Load customers and sales raw data" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "customer_sales_raw_data = \"https://cosmosdbcosmicworks.blob.core.windows.net/cosmic-works-small/customer.json\"\n", |
| 93 | + "response = requests.get(customer_sales_raw_data)\n", |
| 94 | + "# override decoding\n", |
| 95 | + "response.encoding = 'utf-8-sig'\n", |
| 96 | + "response_json = response.json()\n", |
| 97 | + "# filter where type is customer\n", |
| 98 | + "customers = [cust for cust in response_json if cust[\"type\"] == \"customer\"]\n", |
| 99 | + "# filter where type is salesOrder\n", |
| 100 | + "sales_orders = [sales for sales in response_json if sales[\"type\"] == \"salesOrder\"]" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "## Load customers" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [], |
| 115 | + "source": [ |
| 116 | + "customer_data = CustomerList(items=[Customer(**data) for data in customers])\n", |
| 117 | + "db.customers.bulk_write([ UpdateOne({\"_id\": cust.id}, {\"$set\": cust.model_dump(by_alias=True)}, upsert=True) for cust in customer_data.items])" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "markdown", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "## Load sales orders" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": null, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [], |
| 132 | + "source": [ |
| 133 | + "sales_data = SalesOrderList(items=[SalesOrder(**data) for data in sales_orders])\n", |
| 134 | + "db.sales.bulk_write([ UpdateOne({\"_id\": sale.id}, {\"$set\": sale.model_dump(by_alias=True)}, upsert=True) for sale in sales_data.items])" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "markdown", |
| 139 | + "metadata": {}, |
| 140 | + "source": [ |
| 141 | + "## Clean up" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": null, |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [], |
| 149 | + "source": [ |
| 150 | + "client.close()" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
| 154 | + "metadata": { |
| 155 | + "kernelspec": { |
| 156 | + "display_name": ".venv", |
| 157 | + "language": "python", |
| 158 | + "name": "python3" |
| 159 | + }, |
| 160 | + "language_info": { |
| 161 | + "codemirror_mode": { |
| 162 | + "name": "ipython", |
| 163 | + "version": 3 |
| 164 | + }, |
| 165 | + "file_extension": ".py", |
| 166 | + "mimetype": "text/x-python", |
| 167 | + "name": "python", |
| 168 | + "nbconvert_exporter": "python", |
| 169 | + "pygments_lexer": "ipython3", |
| 170 | + "version": "3.11.5" |
| 171 | + } |
| 172 | + }, |
| 173 | + "nbformat": 4, |
| 174 | + "nbformat_minor": 2 |
| 175 | +} |
0 commit comments