Introduction
Apache CouchDB is a NoSQL document-based database server that stores data in JSON format. This guide explains how to access CouchDB in Python.
By default, CouchDB ships with an HTTP API for performing database operations. However, you may want to combine the power of the CouchDB database with the rich Python functions to process the data further. This approach is applicable when designing a backend Application Programming Interface (API) without directly exposing end-users to the CouchDB server.
This guide takes you through implementing the Apache CouchDB server with Python on Ubuntu 20.04 server.
Prerequisites
Before you begin:
Deploy an Ubuntu 20.04 server.
Create a non-root sudo user.
Install Apache CouchDb database.
1. Set Up a CouchDb Database
The first step in creating this sample application is initializing the CouchDB database. SSH to your server and use the Linux curl command to execute the following database operations. Remember to replace EXAMPLE_PASSWORD with the correct CouchDB admin password:
Create a sample my_company database.
$ curl -X PUT http://admin:EXAMPLE_PASSWORD@127.0.0.1:5984/my_company
Output.
{"ok":true}
Insert some documents into the my_company database to ensure the database is ready to accept new documents. The following documents contain data for products. That is product_ids, product_names, and the retail_prices.
$ curl -H 'Content-Type: application/json' -X PUT http://admin:EXAMPLE_PASSWORD@127.0.0.1:5984/my_company/"1" -d '{"product_name": "RIPPED JEANS" , "retail_price" : 32.50}'
$ curl -H 'Content-Type: application/json' -X PUT http://admin:EXAMPLE_PASSWORD@127.0.0.1:5984/my_company/"2" -d '{"product_name": "HIGHT WAIST JEANS" , "retail_price" : 17.25}'
$ curl -H 'Content-Type: application/json' -X PUT http://admin:EXAMPLE_PASSWORD@127.0.0.1:5984/my_company/"3" -d '{"product_name": "STRAIGHT CUT JEANS" , "retail_price" : 49.80}'
$ curl -H 'Content-Type: application/json' -X PUT http://admin:EXAMPLE_PASSWORD@127.0.0.1:5984/my_company/"4" -d '{"product_name": "COTTON BLEND JEANS" , "retail_price" : 42.35}'
$ curl -H 'Content-Type: application/json' -X PUT http://admin:EXAMPLE_PASSWORD@127.0.0.1:5984/my_company/"5" -d '{"product_name": "HIGH-WAIST JEANS" , "retail_price" : 34.80}'
Output.
{"ok":true,"id":"1","rev":"1-f0458dc03140b0cf8de6d2d2fa46ad72"}
{"ok":true,"id":"2","rev":"1-ccf054656db9d18a0fa1361547d12297"}
{"ok":true,"id":"3","rev":"1-19b450bee38c284f57b71c12be9a18f4"}
{"ok":true,"id":"4","rev":"1-97d8cc255704ebc8de92e7a9a99577dc"}
{"ok":true,"id":"5","rev":"1-346b8fbf285fa36473cc081cc377159d"}
Query the my_company database to ensure the documents are in place.
$ curl -X GET http://admin:EXAMPLE_PASSWORD@127.0.0.1:5984/my_company/_all_docs?include_docs=true
Output.
{
"total_rows":5,
"offset":0,
"rows":[
{
"id":"1",
"key":"1",
"value":{
"rev":"1-f0458dc03140b0cf8de6d2d2fa46ad72"
},
"doc":{
"_id":"1",
"_rev":"1-f0458dc03140b0cf8de6d2d2fa46ad72",
"product_name":"RIPPED JEANS",
"retail_price":32.5
}
},
{
"id":"2",
"key":"2",
"value":{
"rev":"1-ccf054656db9d18a0fa1361547d12297"
},
"doc":{
"_id":"2",
"_rev":"1-ccf054656db9d18a0fa1361547d12297",
"product_name":"HIGHT WAIST JEANS",
"retail_price":17.25
}
},
{
"id":"3",
"key":"3",
"value":{
"rev":"1-19b450bee38c284f57b71c12be9a18f4"
},
"doc":{
"_id":"3",
"_rev":"1-19b450bee38c284f57b71c12be9a18f4",
"product_name":"STRAIGHT CUT JEANS",
"retail_price":49.8
}
},
{
"id":"4",
"key":"4",
"value":{
"rev":"1-97d8cc255704ebc8de92e7a9a99577dc"
},
"doc":{
"_id":"4",
"_rev":"1-97d8cc255704ebc8de92e7a9a99577dc",
"product_name":"COTTON BLEND JEANS",
"retail_price":42.35
}
},
{
"id":"5",
"key":"5",
"value":{
"rev":"1-346b8fbf285fa36473cc081cc377159d"
},
"doc":{
"_id":"5",
"_rev":"1-346b8fbf285fa36473cc081cc377159d",
"product_name":"HIGH-WAIST JEANS",
"retail_price":34.8
}
}
]
}
Proceed to the next step to set up a project directory and a gateway class for accessing your CouchDB server.
2. Create a Database Class
You should structure your Python source code in modules to enhance code readability and support. Always create a separate directory for the source code to avoid mixing up the application's and system's files. For this project, you require a database class module that connects to the CouchDB server. Follow the steps below to create the class:
Create a project directory.
$ mkdir project
Switch to the new project directory.
$ cd project
Open a new database_gateway.py file in a text editor.
$ nano database_gateway.py
Enter the following information into the database_gateway.py file. Replace the values of the db_username, db_password db_host, and db_port, db_name variables with the correct CouchDB database credentials.
import couchdb
class DatabaseGateway:
def db_connect(self):
try:
db_username = 'admin'
db_password = 'EXAMPLE_PASSWORD'
db_host = '127.0.0.1'
db_port = '5984'
db_name = 'my_company'
con_string = 'http://' + db_username + ':' + db_password + '@' + db_host + ':' + db_port + '/'
server = couchdb.Server(con_string)
db_con = server[db_name]
return db_con
except Exception as e:
print(e)
return [];
Save and close the database_gateway.py file.
The database_gateway.py file explained:
The import couchdb declaration imports the couchdb module for Python.
The database_gateway.py file contains a single DatabaseGateway class with a single db_connect()` method.
class DatabaseGateway:
def db_connect(self):
...
Under the db_connect() method, you're using the CouchDB server's credentials to create a database connection using the couchdb.Server(con_string) statement. The db_connect() function then returns a reusable database connection using the return db_con statement.
The database_gateway module is ready for use. You can initialize the module in other source code files as follows:
import database_gateway
dg = database_gateway.DatabaseGateway()
db_connect = dg.db_connect()
Proceed to the next step to create another module that allows you to access and manipulate products' information in the CouchDB server.