Cosmos DB Connector for Python

Connect to your Azure Cosmos DB databases to read, query, create, update, and delete documents and more!

In this article you will learn how to quickly and efficiently integrate Cosmos DB data in Python. We will use high-performance Cosmos DB Connector to easily connect to Cosmos DB and then access the data inside Python.

Let's follow the steps below to see how we can accomplish that!

Download Documentation

Create ODBC Data Source (DSN) based on ZappySys API Driver

Step-by-step instructions

To get data from Cosmos DB using Python we first need to create a DSN (Data Source) which will access data from Cosmos DB. We will later be able to read data using Python. Perform these steps:

  1. Download and install ODBC PowerPack.

  2. Open ODBC Data Sources (x64):

    Open ODBC Data Source
  3. Create a User data source (User DSN) based on ZappySys API Driver

    ZappySys API Driver
    Create new User DSN for ZappySys API Driver
    • Create and use User DSN if the client application is run under a User Account. This is an ideal option in design-time, when developing a solution, e.g. in Visual Studio 2019. Use it for both type of applications - 64-bit and 32-bit.
    • Create and use System DSN if the client application is launched under a System Account, e.g. as a Windows Service. Usually, this is an ideal option to use in a production environment. Use ODBC Data Source Administrator (32-bit), instead of 64-bit version, if Windows Service is a 32-bit application.
  4. When the Configuration window appears give your data source a name if you haven't done that already, then select "Cosmos DB" from the list of Popular Connectors. If "Cosmos DB" is not present in the list, then click "Search Online" and download it. Then set the path to the location where you downloaded it. Finally, click Continue >> to proceed with configuring the DSN:

    CosmosDbDSN
    Cosmos DB
    ODBC DSN Template Selection
  5. Now it's time to configure the Connection Manager. Select Authentication Type, e.g. Token Authentication. Then select API Base URL (in most cases, the default one is the right one). More info is available in the Authentication section.

    Steps how to get and use Cosmos DB credentials : API Key [Http]
    Connecting to your Azure Cosmos DB data requires you to authenticate your REST API access. Follow the instructions below:
    1. Go to your Azure portal homepage: https://portal.azure.com/.
    2. In the search bar at the top of the homepage, enter Azure Cosmos DB. In the dropdown that appears, select Azure Cosmos DB.
    3. Click on the name of the database account you want to connect to (also copy and paste the name of the database account for later use).
    4. On the next page where you can see all of the database account information, look along the left side and select Keys: Use API key to get Cosmos DB data via REST API in Azure
    5. On the Keys page, you will have two tabs: Read-write Keys and Read-only Keys. If you are going to write data to your database, you need to remain on the Read-write Keys tab. If you are only going to read data from your database, you should select the Read-only Keys tab.
    6. On the Keys page, copy the PRIMARY KEY value and paste it somewhere for later use (the SECONDARY KEY value may also be copied and used).
    7. Now go to SSIS package or ODBC data source and use this PRIMARY KEY in API Key authentication configuration.
    8. Enter the primary or secondary key you recorded in step 6 into the Primary or Secondary Key field.
    9. Then enter the database account you recorded in step 3 into the Database Account field.
    10. Next, enter or select the default database you want to connect to using the Defualt Database field.
    11. Continue by entering or selecting the default table (i.e. container/collection) you want to connect to using the Default Table (Container/Collection) field.
    12. Select the Test Connection button at the bottom of the window to verify proper connectivity with your Azure Devops account.
    13. If the connection test succeeds, select OK.
    14. Done! Now you are ready to use Asana Connector!

    Fill in all required parameters and set optional parameters if needed:

    CosmosDbDSN
    Cosmos DB
    API Key [Http]
    https://[$Account$].documents.azure.com
    Required Parameters
    Primary or Secondary Key Fill-in the parameter...
    Account Name (Case-Sensitive) Fill-in the parameter...
    Database Name (keep blank to use default) Case-Sensitive Fill-in the parameter...
    API Version Fill-in the parameter...
    Optional Parameters
    Default Table (needed to invoke #DirectSQL)
    ODBC DSN HTTP Connection Configuration

  6. Once the data source connection has been configured, it's time to configure the SQL query. Select the Preview tab and then click Query Builder button to configure the SQL query:

    ZappySys API Driver - Cosmos DB
    Connect to your Azure Cosmos DB databases to read, query, create, update, and delete documents and more!
    CosmosDbDSN
    Open Query Builder in API ODBC Driver to read and write data to REST API
  7. Start by selecting the Table or Endpoint you are interested in and then configure the parameters. This will generate a query that we will use in Python to retrieve data from Cosmos DB. Hit OK button to use this query in the next step.

    #DirectSQL SELECT * FROM root where root.id !=null order by root._ts desc
    Configure table/endpoint parameters in ODBC data source based on API Driver
    Some parameters configured in this window will be passed to the Cosmos DB API, e.g. filtering parameters. It means that filtering will be done on the server side (instead of the client side), enabling you to get only the meaningful data much faster.
  8. Now hit Preview Data button to preview the data using the generated SQL query. If you are satisfied with the result, use this query in Python:

    ZappySys API Driver - Cosmos DB
    Connect to your Azure Cosmos DB databases to read, query, create, update, and delete documents and more!
    CosmosDbDSN
    #DirectSQL SELECT * FROM root where root.id !=null order by root._ts desc
    API ODBC Driver-based data source data preview
    You can also access data quickly from the tables dropdown by selecting <Select table>.
    A WHERE clause, LIMIT keyword will be performed on the client side, meaning that the whole result set will be retrieved from the Cosmos DB API first, and only then the filtering will be applied to the data. If possible, it is recommended to use parameters in Query Builder to filter the data on the server side (in Cosmos DB servers).
  9. Click OK to finish creating the data source.

Video Tutorial

Read data in Python

Using ODBC DSN

  1. Python code to get the data:

    CosmosDbDSN')
    Python code to get the data from ZappySys DSN

  2. When you run the code it will make the API call and read the data:
    Python - Extracted data from ZappySys DSN

  3. Here is Python program's code in text format:

    
        import pyodbc
        conn = pyodbc.connect('DSN=CosmosDbDSN')
    
        cursor = conn.cursor()
    
        #execute query to fetch data from API service
        cursor.execute("SELECT id,title FROM products")
    
        row = cursor.fetchone()
        while row:
            print(row)
            row = cursor.fetchone()
    
        ##For loop example
        #for row in cursor:
        #    print(row)
    

Using a full ODBC connection string

If you want to avoid being dependent on a DSN and creating multiple DSNs for each platform (x86, x64), then you can use a fully qualified connection string. Simply go to your DSN and copy the Connection String:

  1. Open ODBC data source configuration and click Copy settings:
    ZappySys API Driver - Configuration [Version: 2.0.1.10418]
    ZappySys API Driver - Cosmos DB
    Connect to your Azure Cosmos DB databases to read, query, create, update, and delete documents and more!
    CosmosDbDSN
    Copy connection string for ODBC application
  2. The window opens, telling us the connection string was successfully copied to the clipboard: Successful connection string copying for ODBC application
  3. Then in your Python code use Connection String when initializing OdbcConnection object, for example:

    conn = pyodbc.connect('DRIVER={ZappySys API Driver};ServiceUrl=https://yourservices.provider.com/api/xxxx....;AuthName=Http;')

How to install `pyodbc` in the Python?

You would need to install pyodbc in Python if you intend to establish connections to databases that support ODBC (Open Database Connectivity). This module facilitates communication between Python applications and various database management systems, enabling you to perform operations such as querying, retrieving data, and managing databases. Here's how you can install pyodbc in Python:

Installation Steps:

  1. Ensure you have Python installed on your system. If not, download it from the official Python website and follow the installation instructions.

  2. Open your terminal or command prompt.

  3. Use the following command to install pyodbc using pip, the Python package installer:

    python -m pip install "pyodbc"

    Make sure you have a stable internet connection and the necessary permissions to install Python packages.


    Python - pip install pyodbc

Reasons to Install:

  • If pyodbc is not installed, your Python script will generate the following error:
    "ModuleNotFoundError: No module named 'pyodbc'"
    .
  • Database Connectivity: pyodbc allows Python to connect to various databases that support ODBC, such as Microsoft SQL Server, PostgreSQL, MySQL, and more.

  • Data Operations: It facilitates the execution of SQL queries, retrieval of data, and other database operations from within Python scripts.

  • Cross-Platform Support: pyodbc is designed to work across different operating systems, including Windows, macOS, and various Linux distributions.

  • Simplicity and Efficiency: The module provides an intuitive interface for managing database transactions and connections, simplifying the process of working with databases in Python.

By installing pyodbc, you can seamlessly integrate your Python applications with a wide range of ODBC-supported databases, enabling efficient and effective data management and analysis.

Actions supported by Cosmos DB Connector

Learn how to perform common Cosmos DB actions directly in Python with these how-to guides:

Conclusion

In this article we showed you how to connect to Cosmos DB in Python and integrate data without any coding, saving you time and effort. It's worth noting that ZappySys API Driver allows you to connect not only to Cosmos DB, but to any Java application that supports JDBC (just use a different JDBC driver and configure it appropriately).

We encourage you to download Cosmos DB Connector for Python and see how easy it is to use it for yourself or your team.

If you have any questions, feel free to contact ZappySys support team. You can also open a live chat immediately by clicking on the chat icon below.

Download Cosmos DB Connector for Python Documentation

More integrations

Other connectors for Python

All
Big Data & NoSQL
Database
CRM & ERP
Marketing
Collaboration
Cloud Storage
Reporting
Commerce
API & Files

Other application integration scenarios for Cosmos DB

All
Data Integration
Database
BI & Reporting
Productivity
Programming Languages
Automation & Scripting
ODBC applications

  • How to connect Cosmos DB in Python?

  • How to get Cosmos DB data in Python?

  • How to read Cosmos DB data in Python?

  • How to load Cosmos DB data in Python?

  • How to import Cosmos DB data in Python?

  • How to pull Cosmos DB data in Python?

  • How to push data to Cosmos DB in Python?

  • How to write data to Cosmos DB in Python?

  • How to POST data to Cosmos DB in Python?

  • Call Cosmos DB API in Python

  • Consume Cosmos DB API in Python

  • Cosmos DB Python Automate

  • Cosmos DB Python Integration

  • Integration Cosmos DB in Python

  • Consume real-time Cosmos DB data in Python

  • Consume real-time Cosmos DB API data in Python

  • Cosmos DB ODBC Driver | ODBC Driver for Cosmos DB | ODBC Cosmos DB Driver | SSIS Cosmos DB Source | SSIS Cosmos DB Destination

  • Connect Cosmos DB in Python

  • Load Cosmos DB in Python

  • Load Cosmos DB data in Python

  • Read Cosmos DB data in Python

  • Cosmos DB API Call in Python