Skip to content

Instantly share code, notes, and snippets.

@oldarmyc
Created December 20, 2018 00:27
Show Gist options
  • Save oldarmyc/8c43f09a1dbd48b60efba41971972caa to your computer and use it in GitHub Desktop.
Save oldarmyc/8c43f09a1dbd48b60efba41971972caa to your computer and use it in GitHub Desktop.
Couchbase example
"""
To get started will have to install a few packages to get things running
# Download the release RPM from couchbase that sets up the couchbase repositories
wget http://packages.couchbase.com/releases/couchbase-release/couchbase-release-1.0-4-x86_64.rpm
# Install the RPM using yum
sudo yum localinstall couchbase-release-1.0-4-x86_64.rpm -y
# Install the needed packages required for the pip package install
sudo yum install libcouchbase-devel libcouchbase2-bin gcc gcc-c++ -y
# Install the pip package
pip install couchbase
"""
# Import the modules
from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
import json
# Get credentials from kubernetes. The credentials were setup as a dictionary
credentials = None
with open('/var/run/secrets/user_credentials/couchbase_credentials') as f:
credentials = json.load(f)
# Check and make sure the credentials were pulled correctly
if 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}')
# Check if 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment