Skip to content

Commit b9cbee6

Browse files
Merge pull request #264651 from wmwxwa/patch-8
Update functions-reference-python.md
2 parents 677b73b + 1c4fc1e commit b9cbee6

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

articles/azure-functions/functions-reference-python.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,65 @@ The main project folder, *<project_root>*, can contain the following files:
212212

213213
When you deploy your project to a function app in Azure, the entire contents of the main project folder, *<project_root>*, should be included in the package, but not the folder itself, which means that *host.json* should be in the package root. We recommend that you maintain your tests in a folder along with other functions (in this example, *tests/*). For more information, see [Unit testing](#unit-testing).
214214

215+
## Connect to a database
216+
217+
[Azure Cosmos DB](../cosmos-db/introduction.md) is a fully managed NoSQL and relational database for modern app development including AI, digital commerce, Internet of Things, booking management, and other types of solutions. It offers single-digit millisecond response times, automatic and instant scalability, and guaranteed speed at any scale. Its various APIs can accommodate all your operational data models, including relational, document, vector, key-value, graph, and table.
218+
219+
To connect to Cosmos DB, first [create an account, database, and container](../cosmos-db/nosql/quickstart-portal.md). Then you may connect Functions to Cosmos DB using [trigger and bindings](functions-bindings-cosmosdb-v2.md), like this [example](functions-add-output-binding-cosmos-db-vs-code.md). You may also use the Python library for Cosmos DB, like so:
220+
221+
```python
222+
pip install azure-cosmos
223+
224+
from azure.cosmos import CosmosClient, exceptions
225+
from azure.cosmos.partition_key import PartitionKey
226+
227+
# Replace these values with your Cosmos DB connection information
228+
endpoint = "https://azure-cosmos-nosql.documents.azure.com:443/"
229+
key = "master_key"
230+
database_id = "cosmicwerx"
231+
container_id = "cosmicontainer"
232+
partition_key = "/partition_key"
233+
234+
# Set the total throughput (RU/s) for the database and container
235+
database_throughput = 1000
236+
237+
# Initialize the Cosmos client
238+
client = CosmosClient(endpoint, key)
239+
240+
# Create or get a reference to a database
241+
try:
242+
database = client.create_database_if_not_exists(id=database_id)
243+
print(f'Database "{database_id}" created or retrieved successfully.')
244+
245+
except exceptions.CosmosResourceExistsError:
246+
database = client.get_database_client(database_id)
247+
print('Database with id \'{0}\' was found'.format(database_id))
248+
249+
# Create or get a reference to a container
250+
try:
251+
container = database.create_container(id=container_id, partition_key=PartitionKey(path='/partitionKey'))
252+
print('Container with id \'{0}\' created'.format(container_id))
253+
254+
except exceptions.CosmosResourceExistsError:
255+
container = database.get_container_client(container_id)
256+
print('Container with id \'{0}\' was found'.format(container_id))
257+
258+
# Sample document data
259+
sample_document = {
260+
"id": "1",
261+
"name": "Doe Smith",
262+
"city": "New York",
263+
"partition_key": "NY"
264+
}
265+
266+
# Insert a document
267+
container.create_item(body=sample_document)
268+
269+
# Query for documents
270+
query = "SELECT * FROM c where c.id = 1"
271+
items = list(container.query_items(query, enable_cross_partition_query=True))
272+
```
273+
215274
::: zone pivot="python-mode-decorators"
216275
## Blueprints
217276

0 commit comments

Comments
 (0)