Data Science & AI Workbench enables you to connect to a Couchbase NoSQL database from within your notebook sessions.To do so, you’ll need to do the following to install the essential elements required to connect to a Couchbase server:
Download the release RPM from Couchbase to set up the Couchbase repository:
Any packages you install from the command line are available during the current session only. If you want them to persist, add them to the project’s anaconda-project.yml file. For more information, see Project configurations.
You can then use code such as this to import the modules and connect to Couchbase from within a notebook session:
Copy
Ask AI
# Import the modulesfrom couchbase.cluster import Clusterfrom couchbase.cluster import PasswordAuthenticatorimport json"""Get credentials from Kubernetes. The credentials were set up as a dictionary. For example:{ "username": "USERNAME", "password": "PASSWORD"}"""credentials = Nonewith open('/var/run/secrets/user_credentials/couchbase_credentials') as f: credentials = json.load(f)# Verify the credentials were pulled correctlyif credentials: # Try the connection and pass in authentication details bucket = None try: cluster = Cluster(f"couchbase://{credentials.get('hostname')}") cluster.authenticate( PasswordAuthenticator( credentials.get('username'), credentials.get('password') ) ) bucket = cluster.open_bucket('beer-sample') except Exception as e: print(f'There was an error connecting to the bucket: {e}') # Confirm the bucket is connected if bucket and bucket.connected: # Get a value from the bucket by index and print out the result print(json.dumps(bucket.get('21st_amendment_brewery_cafe').value, indent=4)) # Close connection bucket._close()
Was this page helpful?
Assistant
Responses are generated using AI and may contain mistakes.