-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
45 lines (35 loc) · 1.28 KB
/
Copy pathsample.py
File metadata and controls
45 lines (35 loc) · 1.28 KB
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
import os
import json
from azure.cosmos import CosmosClient
from azure.identity import DefaultAzureCredential
endpoint = os.environ["COSMOS_ENDPOINT"]
database_name = "sample"
container_name = "sample-container"
# Using ClientSecretCredential
credential = DefaultAzureCredential() # DefaultAzureCredential will use the VM managed identity to connect to Azure Cosmos DB
client = CosmosClient(url=endpoint, credential=credential)
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)
new_item = {
"id": "Product1",
"categoryId": "61dba35b-4f02-45c5-b648-c6badc0cbd79",
"categoryName": "eletronics",
"name": "Surface Laptop",
"quantity": 12,
"sale": True,
}
container.upsert_item(new_item)
existing_item = container.read_item(
item="Product1",
partition_key="Product1",
)
print("Point read\t", existing_item)
QUERY = "SELECT * FROM p WHERE p.categoryName = @categoryName"
CATEGORYNAME = "eletronics"
params = [dict(name="@categoryName", value=CATEGORYNAME)]
results = container.query_items(
query=QUERY, parameters=params, enable_cross_partition_query=True
)
items = [item for item in results]
output = json.dumps(items, indent=True)
print("Result list\t", output)