Search code examples
pythonjupyter-notebookdata-science-experiencewatson-studio

How do I connect to DashDB from a Jupyter Notebook within Watson Studio?


I'm trying to pull some data in from a DashDB database and analyze it within a Jupyter Notebook, all within the Watson Studio. Ideally we would create a Pandas Dataframe for analysis.


Solution

  • Here's how I was able to do it:

    # First import the relevant libraries
    import jaydebeapi
    from ibmdbpy import IdaDataBase
    from ibmdbpy import IdaDataFrame
    

    Create a hash with credentials:

    credentials_dashdb = {
     'host':'bluemix05.bluforcloud.com',
     'port':'50000',
     'user':'dash123456',
     'password':"""mypassword""",
     'database':'BLUDB'
    }
    

    Build the connection:

    dsn="DASHDB;Database=BLUDB;Hostname=" + credentials_dashdb["host"] + ";Port=50000;PROTOCOL=TCPIP;UID=" + credentials_dashdb["user"] + ";PWD=" + credentials_dashdb["password"]  
    idadb=IdaDataBase(dsn)
    

    Import the data:

    # See all the table names in the database
    df=idadb.show_tables(show_all = True)
    
    # Show the table names
    df.head(100)
    
    # create a pandas dataframe from the table, show the first few rows
    pandas_df = IdaDataFrame(idadb, 'MY_TABLE')
    pandas_df.head()
    

    Hope that helps someone. Big credit to Sven Hafeneger and this notebook for this solution!