Skip to content

Commit a468770

Browse files
authored
Merge pull request #27 from Azure-Samples/kaihuis-cosmosdb-samples
add cosmosdb samples
2 parents a9e7eef + 7698aff commit a468770

8 files changed

+858
-0
lines changed

samples/cosmosdb/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- python
5+
products:
6+
- azure
7+
description: "These code samples will show you how to manage Cosmos DB using Azure SDK for Python."
8+
urlFragment: cosmosdb
9+
---
10+
11+
# Getting started - Managing Cosmos DB using Azure Python SDK
12+
13+
These code samples will show you how to manage Cosmos DB using Azure SDK for Python.
14+
15+
## Features
16+
17+
This project framework provides examples for the following services:
18+
19+
### Cosmos DB
20+
* [] Using the Azure SDK for Python - Cosmos DB Management Library [azure-mgmt-cosmosdb](https://pypi.org/project/azure-mgmt-cosmosdb/) for the [Cosmos DB API](https://docs.microsoft.com/en-us/rest/api/cosmos-db/)
21+
22+
## Getting Started
23+
24+
### Prerequisites
25+
26+
1. Before we run the samples, we need to make sure we have setup the credentials. Follow the instructions in [register a new application using Azure portal](https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal) to obtain `subscription id`,`client id`,`client secret`, and `application id`
27+
28+
2. Store your credentials an environment variables.
29+
For example, in Linux-based OS, you can do
30+
```bash
31+
export AZURE_TENANT_ID="xxx"
32+
export AZURE_CLIENT_ID="xxx"
33+
export AZURE_CLIENT_SECRET="xxx"
34+
export SUBSCRIPTION_ID="xxx"
35+
```
36+
37+
### Installation
38+
39+
1. If you don't already have it, [install Python](https://www.python.org/downloads/).
40+
41+
This sample (and the SDK) is compatible with Python 2.7, 3.3, 3.4, 3.5 and 3.6.
42+
43+
2. General recommendation for Python development is to use a Virtual Environment.
44+
For more information, see https://docs.python.org/3/tutorial/venv.html
45+
46+
Install and initialize the virtual environment with the "venv" module on Python 3 (you must install [virtualenv](https://pypi.python.org/pypi/virtualenv) for Python 2.7):
47+
48+
```
49+
python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation
50+
cd mytestenv
51+
source bin/activate # Linux shell (Bash, ZSH, etc.) only
52+
./scripts/activate # PowerShell only
53+
./scripts/activate.bat # Windows CMD only
54+
```
55+
56+
### Quickstart
57+
58+
1. Clone the repository.
59+
60+
```
61+
git clone https://github.com/Azure-Samples/azure-samples-python-management.git
62+
```
63+
64+
2. Install the dependencies using pip.
65+
66+
```
67+
cd azure-samples-python-management/samples/cosmosdb
68+
pip install -r requirements.txt
69+
```
70+
71+
## Demo
72+
73+
A demo app is included to show how to use the project.
74+
75+
To run the complete demo, execute `python example.py`
76+
77+
To run each individual demo, point directly to the file. For example (i.e. not complete list):
78+
79+
1. `python manage_database_account.py`
80+
81+
If the script starts with `disable_***.py`, it means that it is unavailable now.
82+
83+
The sample files do not have dependency each other and each file represents an individual end-to-end scenario. Please look at the sample that contains the scenario you are interested in
84+
85+
## Resources
86+
87+
- https://github.com/Azure/azure-sdk-for-python
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
import os
8+
9+
from azure.identity import DefaultAzureCredential
10+
from azure.mgmt.cosmosdb import CosmosDBManagementClient
11+
from azure.mgmt.resource import ResourceManagementClient
12+
13+
14+
def main():
15+
16+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
17+
GROUP_NAME = "testgroupx"
18+
DATABASE_ACCOUNT = "databaseaccountxxyyzzz"
19+
KEYSPACE_NAME = "myKeyspace"
20+
TABLE_NAME = "myTable"
21+
22+
# Create client
23+
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
24+
resource_client = ResourceManagementClient(
25+
credential=DefaultAzureCredential(),
26+
subscription_id=SUBSCRIPTION_ID
27+
)
28+
cosmosdb_client = CosmosDBManagementClient(
29+
credential=DefaultAzureCredential(),
30+
subscription_id=SUBSCRIPTION_ID
31+
)
32+
33+
# Create resource group
34+
resource_client.resource_groups.create_or_update(
35+
GROUP_NAME,
36+
{"location": "eastus"}
37+
)
38+
39+
# - init depended resources -
40+
# Create database account
41+
database_account = cosmosdb_client.database_accounts.begin_create_or_update(
42+
GROUP_NAME,
43+
DATABASE_ACCOUNT,
44+
{
45+
"location": "eastus",
46+
"kind": "GlobalDocumentDB",
47+
"database_account_offer_type": "Standard",
48+
"locations": [
49+
{
50+
"location_name": "eastus",
51+
"is_zone_redundant": False,
52+
"failover_priority": "0"
53+
},
54+
],
55+
"capabilities": [
56+
{
57+
"name": "EnableCassandra"
58+
}
59+
],
60+
"api_properties": {}
61+
}
62+
).result()
63+
print("Create database account:\n{}".format(database_account))
64+
# - end -
65+
66+
# Create cassandra keyspace
67+
keysapce = cosmosdb_client.cassandra_resources.begin_create_update_cassandra_keyspace(
68+
GROUP_NAME,
69+
DATABASE_ACCOUNT,
70+
KEYSPACE_NAME,
71+
{
72+
"location": "eastus",
73+
"resource": {
74+
"id": KEYSPACE_NAME
75+
},
76+
"options": {
77+
"throughput": "2000"
78+
}
79+
}
80+
).result()
81+
print("Create cassandra keyspace:\n{}".format(keysapce))
82+
83+
# Create cassandra table
84+
table = cosmosdb_client.cassandra_resources.begin_create_update_cassandra_table(
85+
GROUP_NAME,
86+
DATABASE_ACCOUNT,
87+
KEYSPACE_NAME,
88+
TABLE_NAME,
89+
{
90+
"location": "eastus",
91+
"resource": {
92+
"id": TABLE_NAME,
93+
"default_ttl": "100",
94+
"schema": {
95+
"columns": [
96+
{
97+
"name": "columnA",
98+
"type": "Ascii"
99+
}
100+
],
101+
"partition_keys": [
102+
{
103+
"name": "columnA"
104+
}
105+
]
106+
107+
}
108+
},
109+
"options": {
110+
"throughput": "2000"
111+
}
112+
}
113+
).result()
114+
print("Create cassandra table:\n{}".format(table))
115+
116+
# Get cassandra keyspace
117+
keyspace = cosmosdb_client.cassandra_resources.get_cassandra_keyspace(
118+
GROUP_NAME,
119+
DATABASE_ACCOUNT,
120+
KEYSPACE_NAME
121+
)
122+
print("Get cassandra keyspace:\n{}".format(keyspace))
123+
124+
# Delete cassandra keyspace
125+
cosmosdb_client.cassandra_resources.begin_delete_cassandra_keyspace(
126+
GROUP_NAME,
127+
DATABASE_ACCOUNT,
128+
KEYSPACE_NAME
129+
).result()
130+
print("Delete cassandra keyspace.\n")
131+
132+
# Delete Group
133+
resource_client.resource_groups.begin_delete(
134+
GROUP_NAME
135+
).result()
136+
137+
138+
if __name__ == "__main__":
139+
main()
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
import os
8+
9+
from azure.identity import DefaultAzureCredential
10+
from azure.mgmt.cosmosdb import CosmosDBManagementClient
11+
from azure.mgmt.resource import ResourceManagementClient
12+
13+
14+
def main():
15+
16+
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
17+
GROUP_NAME = "testgroupx"
18+
DATABASE_ACCOUNT = "databaseaccountxxyyzz"
19+
20+
# Create client
21+
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
22+
resource_client = ResourceManagementClient(
23+
credential=DefaultAzureCredential(),
24+
subscription_id=SUBSCRIPTION_ID
25+
)
26+
cosmosdb_client = CosmosDBManagementClient(
27+
credential=DefaultAzureCredential(),
28+
subscription_id=SUBSCRIPTION_ID
29+
)
30+
31+
# Create resource group
32+
resource_client.resource_groups.create_or_update(
33+
GROUP_NAME,
34+
{"location": "eastus"}
35+
)
36+
37+
# Create database account
38+
database_account = cosmosdb_client.database_accounts.begin_create_or_update(
39+
GROUP_NAME,
40+
DATABASE_ACCOUNT,
41+
{
42+
"location": "eastus",
43+
"database_account_offer_type": "Standard",
44+
"locations": [
45+
{
46+
"failover_priority": "2",
47+
"location_name": "southcentralus",
48+
"is_zone_redundant": False
49+
},
50+
{
51+
"location_name": "eastus",
52+
"failover_priority": "1"
53+
},
54+
{
55+
"location_name": "westus",
56+
"failover_priority": "0"
57+
}
58+
]
59+
}
60+
).result()
61+
print("Create database account:\n{}".format(database_account))
62+
63+
# Get database account
64+
database_account = cosmosdb_client.database_accounts.get(
65+
GROUP_NAME,
66+
DATABASE_ACCOUNT
67+
)
68+
print("Get database account:\n{}".format(database_account))
69+
70+
# Update database account
71+
database_account = cosmosdb_client.database_accounts.begin_update(
72+
GROUP_NAME,
73+
DATABASE_ACCOUNT,
74+
{
75+
"tags": {
76+
"dept": "finance"
77+
}
78+
}
79+
).result()
80+
print("Update database account:\n{}".format(database_account))
81+
82+
# Delete database account
83+
database_account = cosmosdb_client.database_accounts.begin_delete(
84+
GROUP_NAME,
85+
DATABASE_ACCOUNT
86+
).result()
87+
print("Delete database account.\n")
88+
89+
# Delete Group
90+
resource_client.resource_groups.begin_delete(
91+
GROUP_NAME
92+
).result()
93+
94+
95+
if __name__ == "__main__":
96+
main()

0 commit comments

Comments
 (0)