Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Connecting Couchbase Clustered DBs to Java, PHP, Ruby, Python, and Node.js Apps

DZone's Guide to

Connecting Couchbase Clustered DBs to Java, PHP, Ruby, Python, and Node.js Apps

Here is how to connect your clustered Couchbase database to your app no matter what language you use, assuming it's Ruby, Python, Java, PHP, or Node.js.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

Recently, we presented you a new clustered Couchbase DB solution, designed to deliver an auto-scalable and highly available storage for your application data in a matter of click. On the belief that you’ve given it a try, so your cluster is up and running by now, today, we’ll examine how to interconnect it with your project for an effective utilization. In particular, we’ll consider the specifics of the dedicated Couchbase clients’ integration depending on the programming language used and give some pieces of advice on the further required application adjustments.

Connecting to Couchbase DB: Preliminary Configurations

In order to set up a proper interaction between a Couchbase cluster and your project, consider the following points:

  • If your application is powered by PHP, Ruby, or Python, you need to preliminarily deploy the Packages Installer add-on to a used application server by following the guidance within the linked repository readme.

Once it’s applied, refer to the Install package option within this add-on’s options menu and paste the following command (as a single string) into the appropriate field to integrate the packages required for your application to access and operate with Couchbase cluster:

autoconf gcc
http://rpms.famillecollet.com/enterprise/7/remi/x86_64//libcouchbase-2.7.5-1.el7.remi.x86_64.rpm
http://rpms.famillecollet.com/enterprise/7/remi/x86_64//libcouchbase-devel-2.7.5-1.el7.remi.x86_64.rpm


  • Some steps within the instructions below could require connecting to the appropriate application server container via Jelastic SSH Gate. To accomplish this, the following operations should be executed:

Now, let’s proceed to the connection establishment itself.

Couchbase Connections for Different Programming Languages

Once you’re acquainted with the recommendations above and accomplished the appropriate actions, refer to one of the sections below according to the programming language that runs your application – Java, PHP, Ruby, Python, or Node.js.

Java

To establish a connection between your Couchbase cluster and Java application, you need to provide the appropriate dedicated Couchbase Java Client JAR libraries within the /lib directory on your application server container. Herewith, this can be accomplished in two different ways – by uploading the required files manually (for the already deployed application) or rebuilding it from the sources with the appropriate client being set as a dependency – we’ll consider both these approaches.

So, for a manual upload – click Config next to the application server with your deployed project and Upload the extracted files from the above-linked package to its webapps/{app_context}/WEB-INF/lib directory.java couchbase connection mavenFinally, Restart the server with the corresponding icon next to it for the newly added JARs to be automatically detected and run. Once it’s up again, modify your application source code to establish a connection to the Couchbase cluster.

If compiling your Java application from sources by means of a Maven build node, you can simply designate the Couchbase Java Client as a required dependency within the project pom.xml file like in the code snippet below.

<dependencies>
   <dependency>
       <groupId>com.couchbase.client</groupId>
       <artifactId>java-client</artifactId>
       <version>2.0.3</version>
   </dependency>
</dependencies>


After rebuilding with your parameters, you’ll get a new application version with the out-of-the-box ability to interact with a clustered Couchbase DB so that you just need to define the appropriate connection parameters within its code.

PHP

For a connection from PHP, make sure you’ve integrated the required components through the Packages Installer add-on (like it was described at the beginning of this section) and run the following commands to install a dedicated client.

git clone git://github.com/couchbase/php-couchbase.git
cd php-couchbase
phpize
./configure –with-couchbase
make
make install


php couchbase connectionOnce this operation finishes, the appropriate Couchbase PHP shared libraries will be placed to the /var/www/php-couchbase/modules directory, while dedicated shared extensions – to /usr/lib64/php/modules.

To actually connect to the cluster from your PHP application, instantiate a new CouchbaseCluster object within the code.

Ruby

To access a Couchbase cluster from inside a Ruby project, make sure you’ve integrated the required components through the Packages Installer add-on (like it was described at the beginning of this section) and connect to a destination container via SSH to install the Couchbase Ruby Client

gem install couchbase



ruby connect to databaseAs a result, three new gems will be installed – yaji, connection_pool, and couchbase. Now just point to the Couchbase client within your application source code to establish a connection.

Python

To bind your newly created Couchbase cluster to a Python-powered application, make sure you’ve integrated the required components through the Packages Installer add-on (like it was described at the beginning of this section) and run the following command to download the Couchbase Python Client to your app server.

pip install couchbase



python connect to couchbaseWhen the installation is finished, you’ll see a destination directory where the required Couchbase libraries are stored – this implies your server can operate with this DB system now. Thus, the only thing that left to do to connect to a Couchbase cluster from your application is to import the required classes.

Node.js

To access a Couchbase cluster from a Node.js app, access the appropriate app server container via SSH Gate and install the dedicated native module by running the next command.

npm install couchbase



couchbase node jsAfter completion, the appropriate Couchbase-related module will be stored within the /opt/repo/node_modules/couchbase application server directory, enabling a connection to a cluster by following the linked guidance.

That’s it! Now you have a reliable, scalable storage to serve your application data.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
integration ,couchbase ,database integration ,java ,php ,node.js ,ruby ,python ,tutorial

Published at DZone with permission of Tetiana Markova, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}