Watson Studio

 View Only
  • 1.  Using Cloudant database from Jupyter Notebooks

    Posted Mon May 17, 2021 05:05 PM
    Hello,
    I want to record our data processing results in our cloudant database from watson studio using spark & python environment.
    I found the way to make a connection with the cloudant database from jupyter notebook in this link "https://medium.com/@glynn_bird/using-cloudant-from-jupyter-notebooks-acf36428d65c"
    But I didn't find the password in my Cloudant account's admin credentials.
    How could I connect to the cloudant from watson studio without using the passward?
    Thanks

    ------------------------------
    Siham Si hadj mohand
    ------------------------------


  • 2.  RE: Using Cloudant database from Jupyter Notebooks

    Posted Wed May 19, 2021 02:39 AM
    Edited by Massimo Loaldi Wed May 19, 2021 02:44 AM
    Hello Siham, you need to use the service credentials that you can generate by entering Cloudant.
    You will need to reference the username and apikey afterwards. Pls see the code below, it is an example of how to connect to Cloudant and create a pandas data frame.
    Hope this helps.
    Massimo


    import pandas as pd
    !pip install cloudant from cloudant.client import Cloudant

    from cloudant.client import Cloudant
    client = Cloudant.iam(
        "..........................", #username
        "..........................", #apikey 
        connect=True
    )
    
    impo=[]
    
    Connect = client['<name of the db in cloudant>']
    
    for document in Connect:
        impo.append(document)
    
    abl = pd.DataFrame(impo)
    abl.tail()
     
    .... And here a function to create new records in cloudant

    # =============================================================================
    # DB data writing (recording the new record in Cloudant)
    # =============================================================================
    
    def db_data_writing(credential:dict, db_name:str, files:dict):
        """
        DB data writing
        This function reach the online database and write the input files as a new document
        Parameters
        ----------
        credential: dict
                    credential to access the online Cloudant database instance
        db_name: str
                    name of the database on which write data
        files: dict
                    json containing data to write on db
        Returns
        -------
        bool
            exit status
        """
        # Feed credential and establish a connection
        client = Cloudant.iam(credential["username"],credential["apikey"],connect=True)
        # Select the database instance on which write results
        db = client[db_name]
        # write and save document
        db.create_document(files).save()
        # shutdown the connection
        client.disconnect()


    ------------------------------
    Massimo Loaldi
    ------------------------------



  • 3.  RE: Using Cloudant database from Jupyter Notebooks

    Posted Wed May 26, 2021 08:16 AM
    Hello Massino,
    Thank you for your help, It works!
    But I have another problem, I got the error below from writing some input files as a new document:
    "HTTPError: 413 Client Error: Request Entity Too Large document_too_large "
    How could I fix this error?

    Thank you

    ------------------------------
    Siham Si hadj mohand
    ------------------------------