1
+ import os
2
+ #from azure.cosmos import CosmosClient, exceptions, PartitionKey
3
+ from azure .cosmos .aio import CosmosClient
4
+ import json
5
+ import asyncio
6
+
7
+ URL = "<Cosmos DB URL>"
8
+ KEY = "<Cosmos DB Key>"
9
+ client = CosmosClient (URL , credential = KEY )
10
+ DATABASE_NAME = 'ToDoList'
11
+ database = client .get_database_client (DATABASE_NAME )
12
+ CONTAINER_NAME = 'Items1'
13
+ container = database .get_container_client (CONTAINER_NAME )
14
+
15
+ '''
16
+ ## Create a database
17
+ try:
18
+ database = client.create_database(DATABASE_NAME)
19
+ except exceptions.CosmosResourceExistsError:
20
+ database = client.get_database_client(DATABASE_NAME)
21
+
22
+
23
+ ## Create a container
24
+ try:
25
+ container = database.create_container(id=CONTAINER_NAME, partition_key=PartitionKey(path='/id', kind='Hash'))
26
+ except exceptions.CosmosResourceExistsError:
27
+ container = database.get_container_client(CONTAINER_NAME)
28
+ except exceptions.CosmosHttpResponseError:
29
+ raise
30
+
31
+ ## Create an analytical store enabled container
32
+ try:
33
+ container = database.create_container(id=CONTAINER_NAME, partition_key=PartitionKey(path='/id', kind='Hash'),default_ttl=-1)
34
+ except exceptions.CosmosResourceExistsError:
35
+ container = database.get_container_client(CONTAINER_NAME)
36
+ except exceptions.CosmosHttpResponseError:
37
+ raise
38
+
39
+
40
+ ## Create records
41
+ for i in range(1, 10):
42
+ container.upsert_item({
43
+ 'id': 'item{0}'.format(i),
44
+ 'TYkey' : '1',
45
+ 'productName': 'Widget',
46
+ 'productModel': 'Model {0}'.format(i)
47
+ }
48
+ )
49
+
50
+
51
+
52
+ ## Delete data
53
+ for item in container.query_items(query='SELECT * FROM products p WHERE p.productModel = "Model 2"', enable_cross_partition_query=True):
54
+ print(item)
55
+ container.delete_item(item, partition_key=item['id'])
56
+
57
+ ## Query the database
58
+ for item in container.query_items(query='SELECT * FROM mycontainer r WHERE r.id="item3"',enable_cross_partition_query=True):
59
+ print(json.dumps(item, indent=True))
60
+
61
+
62
+
63
+ ## Using the asynchronous client
64
+ async def create_products():
65
+ async with CosmosClient(URL, credential=KEY) as client: # the with statement will automatically initialize and close the async client
66
+ database = client.get_database_client(DATABASE_NAME)
67
+ container = database.get_container_client(CONTAINER_NAME)
68
+ for i in range(20):
69
+ await container.upsert_item({
70
+ 'id': 'item{0}'.format(i),
71
+ 'productName': 'Widget',
72
+ 'productModel': 'Model {0}'.format(i)
73
+ }
74
+ )
75
+ async def main():
76
+ await create_products()
77
+
78
+ # Run the event loop to execute the asynchronous function
79
+ asyncio.run(main())
80
+
81
+ '''
82
+ ## Queries with the asynchronous client
83
+ async def create_lists ():
84
+ results = container .query_items (query = 'SELECT * FROM products p WHERE p.productModel = "Model 2"' )
85
+ item_list = []
86
+ async for item in results :
87
+ item_list .append (item )
88
+ await client .close ()
89
+ return item_list
90
+
91
+ async def main ():
92
+ items = await create_lists ()
93
+ for item in items :
94
+ print (item )
95
+ await client .close ()
96
+
97
+ # Run the event loop to execute the asynchronous function
98
+ asyncio .run (main ())
0 commit comments