-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcosmos.py
More file actions
36 lines (27 loc) · 1.43 KB
/
cosmos.py
File metadata and controls
36 lines (27 loc) · 1.43 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
import os
import json
import datetime
from azure.cosmos import CosmosClient
ENDPOINT = os.environ["ENDPOINT"]
KEY = os.environ["KEY"]
DATABASE_NAME = os.environ["DATABASE_NAME"]
CONTAINER_NAME = os.environ["CONTAINER_NAME"]
# Sample code that upserts (updates or inserts) data in the CosmosDB.
def upsertItem(id: str, partitionKey: int):
client = CosmosClient(url=ENDPOINT, credential=KEY)
database = client.get_database_client(DATABASE_NAME)
container = database.get_container_client(CONTAINER_NAME)
item = container.read_item(id, partition_key=partitionKey)
date_string = datetime.datetime.now().isoformat()
item["Region"] = f"Chile-S ({date_string})"
response = container.upsert_item(body=item)
print("Upserted Item's \n{0}".format(json.dumps(response, indent=True)))
# TODO: Using the Azure SDK reference documentation, complete the query() method.
# Use the SDK to query the following command on the database:
# SELECT * FROM collection c WHERE c.Elevation = <input-param-from-func> ORDER BY c.Country ASC OFFSET 0 LIMIT <input-param-from-func>
# Hint: You can re-use the code to create your clients to interact with the CosmosDB.
def query_by_elevation(elevation: int, limit: int):
query = "SELECT * FROM collection c WHERE c.Elevation = <input-param-from-func> ORDER BY c.Country ASC OFFSET 0 LIMIT <input-param-from-func>"
pass
upsertItem("7408d446-fb51-e70e-9955-560a0c966b68", 0)
query_by_elevation(0, 5)