Adding a BLU/Analytics Warehouse Service to iPython

Written a few months back now, but may help someone at some point…

Create a BLU Warehouse in Bluemix, and bind to the Application.

cf create-service AnalyticsWarehouse Small AW-ipython
cf services
cf bind-service ipython AW-ipython

Script to run to download the DB2 Server driver, unpack it and set environment settings for the ibm_db install later.

#!/usr/bin/env bash

echo "Downloading IBM Data Server driver..."
curl "" -s -o ./clidriver.tgz
tar xzf ./clidriver.tgz -C ./

# Delete the archive
rm -rf ./clidriver.tgz
mv ./clidriver /home/vcap/app/.heroku/clidriver

# Set environment variables, so the ibm_db install works correctly
export IBM_DB_HOME="/home/vcap/clidriver"
export IBM_DB_LIB="/home/vcap/app/.heroku/clidriver/lib"
export LD_LIBRARY_PATH="/app/.heroku/clidriver/lib:$LD_LIBRARY_PATH"

Python scripts to install the ibm_db package.

import os
!pip install ibm_db
import ibm_db

Test application code to inspect the Bluemix VCAP_SERVICES variable and connect to the remote DB2 instance.

import urllib
import datetime
import json

# get service information if on Bluemix 
if 'VCAP_SERVICES' in os.environ: 
  db2info = json.loads(os.environ['VCAP_SERVICES'])['AnalyticsWarehouse'][0]
  db2cred = db2info["credentials"] 

# connect to DB2  
db2conn = ibm_db.connect("DATABASE="+db2cred['db']+";HOSTNAME="+db2cred['hostname']+";PORT="+str(db2cred['port'])+";UID="+db2cred['username']+";PWD="+db2cred['password']+";","","") 
# we have a DB2 connection
if db2conn:  
    stmt = ibm_db.exec_immediate(db2conn,"select host_name,os_name,os_version,total_cpus,configured_cpus, total_memory,os_kernel_version,os_arch_type, os_release,os_full_version from sysibmadm.env_sys_info")  

# fetch the result  
result = ibm_db.fetch_assoc(stmt)   


Other useful resources:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s