This is a tutorial to use IBM Cloud Functions to respond to a new document in Cloudant and write the document into a table in Db2 Warehouse on Cloud. This tutorial will demonstrate how to:
- Create the
cloudant-to-db2Cloudant database - Create a Db2 Warehouse on Cloud table to house your data
- Create a IBM Cloud Functions sequence
- Add a public Cloudant action to read documents
- Create an action to extract the Cloudant JSON document and insert that data into a table in Db2 Warehouse on Cloud
- Trigger a function in response to changes in a Cloudant database
- Enable the sequence to be invoked by the Cloudant trigger
- Test the triggered sequence by creating a Cloudant document and viewing the data in a Db2 table
N.B. This is an example only intended as a starting point for using IBM Cloud Functions to extract data from Cloudant and insert into Db2 Warehouse on Cloud. It is not intended as a drop-in replacement for the deprecated Cloudant warehouse integration. In particular this example does not perform upsert or handle document deletion.
Before you begin you will need:
- IBM Bluemix CLI and IBM Cloud Functions CLI installed on your machine
- An IBM Cloudant database instance
- An IBM Db2 Warehouse on Cloud instance
-
Log in to the Cloudant dashboard and create the
cloudant-to-db2Cloudant database
-
Create a Db2 table to house your data
-
Manage>Opento get the Db2 Warehouse on Cloud dashboard, then chooseRun SQL -
For
cloudant-to-db2database, create a table called CLOUDANT_TO_DB2 withvarcharcolumns for id, rev, diet and latin_name by running this SQL statement:CREATE TABLE CLOUDANT_TO_DB2 (id VARCHAR(1024), rev VARCHAR(1024), diet VARCHAR(1024), latin_name VARCHAR(1024))
- Add service credentials to your Db2 Warehouse on Cloud instance
(
Service credentialsthen pressNew credential)
-
-
Create a sequence that includes a Cloudant action to read documents
- Log in to your IBM Bluemix account and select the
Actionstab on IBM Cloud Functions - Press the
Createbutton - Press
Create Sequence - Choose a name for the sequence e.g.
cloudantToDb2 - Choose whatever package you want to store it in (we'll use the default package)
- Create a Cloudant action for the sequence:
- Select
Use Publicand click onCloudant - Choose the
read-documentaction from the drop down - Select new binding and choose a name for the package binding e.g.
myCloudant - Choose the Cloudant instance from the dropdown or enter details for another Cloudant account
- Add
cloudant-to-db2to theDatabasefield
- Select
- Press the
Createbutton
- Log in to your IBM Bluemix account and select the
-
Create an action to write a Cloudant document to Db2
- IBM Cloud > Functions > Actions
- Click on the
cloudantToDb2sequence - Click
Add
- Choose a name for the action e.g.
writeToDb2 - Choose whatever package you want to store it in (we'll use the default package)
- Choose the Node.js 8 runtime
- Press the
Create & Addbutton
- Press the
Savebutton - Click on the
writeToDb2action
- In a terminal window, bind the service credentials to the
writeToDb2action by running the bluemix CLI command:bx wsk service bind dashdb writeToDb2
You should see a success output likeService credentials 'Credentials-1' from service 'Db2 Warehouse-er' bound to action 'writeToDb2'Note: If you have multiple Db2 Warehouse on Cloud service credentials you need to identify the correct credential to bind with additional CLI args - Replace the
mainfunction in thewriteToDb2action code with this sample code:
const ibmdb = require('ibm_db') function main(message) { let db2, stmnt, result; // Open the connection to Db2 Warehouse on Cloud using the bound credentials db2 = ibmdb.openSync(message.__bx_creds.dashDB.dsn); // SQL insert statement to insert a row into the CLOUDANT_TO_DB2 table stmnt = db2.prepareSync('INSERT INTO CLOUDANT_TO_DB2 VALUES (?,?,?,?);'); db2.beginTransactionSync(); // begin Db2 transaction // Execute the SQL insert statement, substituting the appropriate values from the passed in // document that was read from Cloudant stmnt.execute([message._id, message._rev, message.diet, message.latin_name], function(err, result, outparams){ // Commit or rollback if(err) { db2.rollbackTransactionSync(); } else { db2.commitTransactionSync(); } // Close the connection db2.closeSync(); return result; }); }
-
Create a Cloudant trigger
- Select the IBM Cloud
Functionscatalog category, then select theTriggerstab - Select
Create Trigger - Choose Trigger type
Cloudant - Choose a name for the trigger e.g.
cdtExample - Choose or create whatever package you want to store it in (we'll use the default package)
- Create or Select a Cloudant instance, or enter details for another Cloudant account
- Add
cloudant-to-db2to theDatabasefield - Press the
Createbutton
- Select the IBM Cloud
-
Connect the sequence to the Cloudant trigger
- Write a document into your IBM Cloudant database
- Login to your Cloudant Dashboard and open the
cloudant-to-db2database - Select
Create Documentand paste the JSON below:
{ "_id": "frog", "diet": "insectivore", "latin_name": "Rana temporaria" }- Press
Create Documentagain to create the document
- Login to your Cloudant Dashboard and open the
- View the IBM Cloud Functions Monitor dashboard
- View the data in the Db2 Warehouse on Cloud table
- In the Bluemix dashboard, go to your Db2 Warehouse on Cloud service
- On the Manage tab, click the Open button:

- In the Db2 console, click on the Explore tab and select the schema that matches your username
- Select the
CLOUDANT_TO_DB2table under the selected schema and click View Data:
- You should now see the data for the
frogdocument:
- IBM Cloud Docs - Cloudant NoSQL DB
- IBM Cloud Functions Cloudant trigger
- IBM Cloud Functions Cloudant data processing
- IBM Cloud Functions catalog
- New Node.js 8 runtime for IBM Cloud Functions
- Simplify binding your IBM Cloud services to serverless Functions
- IBM Db2 bindings for Node.js
- IBM Db2 Warehouse on Cloud - Node.js Hello World Sample
- IBM Cloud Functions Node.js Runtime
- Access Db2 from IBM Cloud Functions the Easy Way (Node.js)




