This is Request! Tips for developing on Azure using Python![PR] Microsoft Japan Advent Calendar 2020 This is the article on the 19th day.
This article summarizes what you can do with ** Python ** in Azure Cosmos DB
.
Azure Cosmos DB Python SDK
To connect to Azure Cosmos DB built with the SQL API with Python, you would use the ** Azure Cosmos DB Python SDK **. This library is published on PyPI, so please check it once before using it.
You can complete the package installation with the following command.
pip install azure-cosmos
You can also use Docker Compose to install and use the Azure Cosmos DB Python SDK inside a Docker container.
Create a docker-compose.yaml
similar to the following and start the container with docker-compose up -d
.
docker-compose.yaml
version: '3'
services:
app:
image: python:3.9
container_name: python3
tty: true
volumes:
- ./src:/home/src
- ./pip-lib:/pip-lib
working_dir: "/home/src"
environment:
PYTHONUSERBASE: /pip-lib
Create requirements.txt
under the src
folder. In this file, we will describe the library information to be used according to the specifications of ** pip freeze **.
For Azure Cosmos DB Python SDK:
/src/requirements.txt
azure-cosmos==4.2.0
After creating the requirements.txt
file, run the following command to install the Azure Cosmos DB Python SDK.
docker-compose run --rm app pip install -r requirements.txt --user
The command should install the library specified in requirements.txt in the pip-lib
folder.
You should also be able to verify that it is installed by running pip list
as follows:
docker-compose run --rm app pip list
By doing this, as ** Infrastructure as a Code ** (IaC), the development environment can be managed and unified with Docker, so it is recommended. All you have to do now is actually start developing in Python. PyPI and Microsoft Docs Tutorial have descriptions on how to connect and read / write data, so we will use this as a reference when developing Python applications.
Azure Cosmos DB Jupyter Notebooks
At Microsoft Build held in May 2020, it became possible to incorporate ** Jupyter Notebooks environment ** into Cosmos DB's data explorer, making it easier to perform data analysis and so on. You must enable the Notebooks feature when you create your Azure Cosmos DB account.
As of December 19, 2020, the Jupyter Notebooks feature is a preview feature, so there are restrictions on the regions that can be enabled.
--Support for Jupyter Notebook built into Azure Cosmos DB (Preview)
The nice thing about this Jupyter Notebooks feature is that you can do it in the same way as you're used to with Jupyter Notebooks, but you can also run SQL inside cells in your Jupyter Notebook **.
In addition, Jupyter Notebooks of Azure Cosmos DB includes version 4 of the aforementioned Azure Cosmos DB Python SDK
together, so you can easily start using it without building a development environment.
%%sql --database <Database name> --container <Container name>
SELECT c.Action, c.Price as ItemRevenue, c.Country, c.Item FROM c
In this way, you can use SQL in the cell to refer to the data in Azure Cosmos DB, which makes it easier to check the actual data when coding in Python such as machine learning.
Of course, you can also have Pandas DataFrame
output the SQL execution results.
%%sql --database <Database name> --container <Container name> --output df_cosmos
SELECT c.Action, c.Price as ItemRevenue, c.Country, c.Item FROM c
df_cosmos.head(10)
In this way, by adding --output
to the argument of %% sql
, the execution result of the SQL query can be directly submitted to Pandas DataFrame on the same Jupyter Notebook.
Azure Cosmos DB is a NoSQL database service. It is important not only to actually store the data, but also to utilize it for various purposes. By all means, take advantage of the Python SDK and Jupyter Notebooks features to feel free to start using data in Python for the first time.
--Quick Start: Build a Python Application Using an Azure Cosmos DB SQL API Account (https://docs.microsoft.com/ja-jp/azure/cosmos-db/create-sql-api-python) --Azure Cosmos DB Python SDK for SQL API: Release Notes and Resources --Support for Jupyter Notebook built into Azure Cosmos DB (Preview) --Enable notebooks for Azure Cosmos DB account (preview) (https://docs.microsoft.com/ja-jp/azure/cosmos-db/cosmosdb-jupyter-notebooks) --Use built-in notebook commands and features in Python notebooks in Azure Cosmos DB (preview) -How to enable pip install to update the library without rebuilding docker's python container
Recommended Posts