Populate a Cloudant database using Python

less than 1 minute read

Originally published on: https://dev.to/ibmdeveloper/ibm-developer-tips-week-2-46la

In just a few lines I was able to use the Cloudant Python SDK to quickly seed a database with 1400 entries from a YAML file.

Pro-tip: Make sure you’re using the Replay429Adapter in your client or you’ll surely hit a Rate Limit Error

import yaml
from cloudant import Cloudant
from cloudant.adapters import Replay429Adapter


username = "foo"
password = "bar"
url = "baz"
client = Cloudant(username, password, url=url, connect=True,
                  adapter=Replay429Adapter(retries=10, initialBackoff=0.01))

client.connect()
db = client.create_database("repos", throw_on_exists=False)

with open("repos.yaml", 'r') as stream:
    data_loaded = yaml.safe_load(stream)

    for entry in data_loaded['repos']:
        db.create_document(entry)

Read more about the Cloudant DBaaS on IBM Cloud.

Updated: