DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Hello World with Couchbase and Java

Hello World with Couchbase and Java

Don Pinto user avatar by
Don Pinto
·
Dec. 21, 12 · Interview
Like (0)
Save
Tweet
Share
6.30K Views

Join the DZone community and get the full member experience.

Join For Free

My favorite technical book of all time has been the C Programming Language by Kernighan and Ritchie affectionately referred to as K&R. They popularized a term that will ever be in a developer parlance - Hello World.

#include <stdio.h>
main()
{
    printf("Hello World\n");
}

This innocous looking program has helped launch the careers of many a programmer and has been the basis of the wild success of many an API. Dennis Ritchie who passed away recently, or as some would refer to it as returned from main(), was also inspirational in creating the Unix Operating System.

So, without further ado, here's the Hello World for Couchbase.

import java.net.URI;
import java.util.List;
import java.util.ArrayList;
import com.couchbase.client.CouchbaseClient;

public class HelloCouchbase {
  public static void main(String args[]) {
    try {
      URI local = new URI("http://localhost:8091/pools");
      List<URI> baseURIs = new ArrayList<URI>();
      baseURIs.add(local);

      CouchbaseClient c = new CouchbaseClient(baseURIs, "default", "");
      c.set("key", 0, "Hello World");
      System.out.println(c.get("key"));
    } catch (Exception e) {
      System.err.println("Error connecting to Couchbase: "
          + e.getMessage());
     System.exit(0);
    }
  }
}

Admittedly a much longer program.

What the program does is very simply to get the value of a key that was just set. However, in a distributed system, there are no guarantees due to the inherent dynamic nature of the system. The eight fallacies of distributed computing goes into greater details on this. From a programmer's perspective, the Java client libraries for Couchbase abstract this dyamic nature of a cluster.  They provide a basic set of operations such as get and set which is available in either synchronous or asynchronous forms. It's possible to use these simple operations in conjunction with operations that help implement atomicity such as check and set(cas) to implement high performant and scalable systems for the real world.

Any more and I am deviating from the philosophy of the Hello World.

I will be contributing more towards using Couchbase with the Java client libraries, but in the meantime, here are a few useful links.



Java (programming language)

Published at DZone with permission of Don Pinto, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Development Trends 2023
  • Top Authentication Trends to Watch Out for in 2023
  • Understanding gRPC Concepts, Use Cases, and Best Practices
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: