The ibmdbpy library provides a Python interface for data manipulation and access to in-database algorithms in IBM dashDB and IBM DB2. It accelerates Python analytics by seamlessly pushing operations written in Python into the underlying database for execution, thereby benefiting from in-database performance-enhancing features, such as columnar storage and parallel processing.
To get started you will need credentials for a dashDB database. If you don't already have an instance of dashDB you can get one for free by following steps in this article.
The steps outlined below are included in a tutorial notebook available on the Jupyter notebook Welcome screen of your Workbench.
Lets start by loading ibmdbPy. We will also need to import JayDeBeAPI package to connect to the database using JDBC from ibmdbPy.
import jaydebeapi from ibmdbpy import IdaDataBase from ibmdbpy import IdaDataFrame
Next you will need to enter your database credentials:
#Enter the values for you database connection dsn_database = "BLUDB" # e.g. "BLUDB" dsn_hostname = "<Enter hostname>" # e.g.: "bluemix05.bluforcloud.com" dsn_port = "50000" # e.g. "50000" dsn_uid = "<Enter userID>" # e.g. "dash104434" dsn_pwd = "<Enter password" # e.g. "7dBZ3jWt9xN6$o0JiX!m"
Then create the connection:
Now lets read some data into a dataframe and display first 5 rows:
df=idadb.show_tables(show_all = True df.head(5)
The results will look like:
The tutorial notebook includes additional examples for data manipulation. To ensure expected behaviors, IdaDataBase instances need to be closed. Closing the IdaDataBase is equivalent to closing the connection: once the connection is closed, it is not possible to use the IdaDataBase instance and any IdaDataFrame instances that were opened on this connection anymore.