[DOCKER] Tips for developing apps with Azure Cosmos DB in Python

About this article

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

Use Azure Cosmos DB Python SDK on Docker

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.

スクリーンショット 2020-12-19 23.41.13.png

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.

スクリーンショット 2020-12-19 23.11.37.png

%%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.

in conclusion

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.

Reference information

--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

Tips for developing apps with Azure Cosmos DB in Python
Tips for dealing with binaries in Python
VS Code settings for developing in Python with completion
Efficiently develop Azure Python apps with CI/CD
Access Azure Cosmos DB with Spring Boot
Tips for using python + caffe with TSUBAME
Process multiple lists with for in Python
Tips for making small tools in python
~ Tips for Python beginners from Pythonista with love ① ~
Tips for hitting the ATND API in Python
~ Tips for Python beginners from Pythonista with love ② ~
[Introduction for beginners] Working with MySQL in Python
Settings for getting started with MongoDB in python
Specific sample code for working with SQLite3 in Python
Tips for speeding up python code correctly with numba
Storage I / O notes in Python with Azure Functions
Azure Cosmos DB starting in 2020 --Azure Synapse Link Demo (Part. 1)
Settings when developing App Engine / Python apps with VS Code
Scraping with selenium in Python
Working with LibreOffice in Python
Debugging with pdb in Python
Create a child account for connect with Stripe in Python
[For beginners] Summary of standard input in Python (with explanation)
Search for strings in Python
Working with sounds in Python
Scraping with Selenium in Python
Building a development environment for Android apps-creating Android apps in Python
(For myself) Flask_8 (Add / Edit / Delete in database with python)
Techniques for sorting in Python
Tips for coding short and easy to read in Python
Scraping with Tor in Python
[Tips] Handle Athena with Python
How to specify Cache-Control for blob storage in Azure Storage in Python
Tweet with image in Python
Combined with permutations in Python
Problems and solutions when asked for MySQL db in Python 3
[Python + Selenium] Tips for scraping
~ Tips for beginners to Python ③ ~
About "for _ in range ():" in python
How to configure CORS settings for Azure storage service in Python
The story of making a standard driver for db with python.
Dockerfile with the necessary libraries for natural language processing in python
Number recognition in images with Python
Check for memory leaks in Python
Azure Functions: Try Durable Functions for Python
Testing with random numbers in Python
Check for external commands in python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
LaTeX, Python tips in master's thesis
[Memo] Links for developing with Flask
CSS parsing with cssutils in Python
Tips for running Go with docker
Monitor Python web apps with Prometheus
Getting Started with Python for PHPer-Classes
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
[TouchDesigner] Tips for for statements using python
Tips for calling Python from C