Connecting Couchbase Clustered Database to Java, PHP, Ruby, Python and NodeJS Applications

By | August 3, 2017
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Recently, we’ve 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. Believing 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 specifics of the dedicated Couchbase clients’ integration depending on the used programming language and give some pieces of advice on the further required application adjustments.

Connecting to Couchbase DB: Preliminary Configurations

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

  • If your application is powered by PHP, Ruby or Python, you need to preliminary 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 Connection for Different Programming Languages

Once you’ve got 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 – either Java, PHP, Ruby, Python or Node.js.

  • Java

To establish 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 – through 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 manual upload – click Config next to the application server with your deployed project and Upload 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 connection to Couchbase cluster.

If compiling your Java application from sources by means of 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 rebuild with such parameters, you’ll get a new application version with out-of-box ability to interact with clustered Couchbase DB so that you just need to define the appropriate connection parameters within its code.  

  • PHP

For 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 installphp couchbase connectionUpon this operation finishes, the appropriate Couchbase PHP shared libraries will be placed to the /var/www/php-couchbase/modules directory, whilst 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 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 couchbaseruby connect to databaseAs a result, three new gems will be installed –yajiconnection_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 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 couchbasepython 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 for connecting to a Couchbase cluster from your application is to import the required classes.

  • Node.js

To access 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 couchbasecouchbase node jsAfter completion, the appropriate Couchbase-related module will be stored within the /opt/repo/node_modules/couchbase application server directory, enabling connection to a cluster by following the linked guidance.

That’s it! Now you have a reliable scalable storage to serve your application data, whilst handling both these project components within a Jelastic Cloud makes such solution even more efficient. Try it yourself by getting a free trial account at one of Jelastic Hosting Services providers.

Have any questions on connecting your application to Couchbase cluster? Feel free to ask within the comments below or get in touch with our technical experts at Stackoverflow.

Related article

Auto-Scalable Couchbase Cluster in Containers: One-Click Deploy to the Cloud

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Subscribe to get the latest updates