Skip to content

couchbase-guides/java-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tags
java-sdk

Getting started with Couchbase and the Java SDK

This guide walks you through the process of using the Couchbase SDK to store a JSON document in Couchbase.

What you’ll build

You will store a JSON document representiong a Person in Couchbase Server using the Couchbase Java SDK.

What you’ll need

  • 15-30 minutes

  • A Java code editor with JDK 1.8+

  • Maven

  • Couchbase Server 4.5+ (follow the instructions to install Couchbase and create a bucket - this guide assumes you will have it running locally)

Setup

Install and launch Couchbase

With your project set up, you can install and launch Couchbase.

For whatever operating system you are using, instructions and downloads can be found at http://developer.couchbase.com/documentation/server/current/getting-started/installing.html.

After you install Couchbase, launch it. You should see a webpage opening in your default browser allowing you to setup Couchbase

Dependencies

The following code will rely on the Couchbase Java SDK so make sure you add the correct dependency:

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

Code

Create an Application class

Here you create an Application class with all the components.

src/main/java/hello/Application.java

link:complete/src/main/java/hello/Application.java[role=include]

The main() method starts by intializing a connection to the cluster by calling CouchbaseCluster.create(). By default this method will attempt a connection to a cluster on the same machine, which is to say with 127.0.0.1 as IP.

Then use the openBucket method. By default it opens the bucket named default. A bucket is where you store k/v pairs in Couchbase. A k/v pair can be called a document from the moment the value is a JSON String. To create a JSON object call the JsonObject.create() method. By default this object will be empty. As the goal here is to store a Person document you can add a firstName and lastName property to the JsonObject like so:

jo.put("firstName", "Alice");
jo.put("lastName", "Smith");

A JsonObject must be wrapped into a JsonDocument. You can create a JsonDocument once you have a key and a value like so:

JsonDocument.create("asmith", jsonObject);

The next step is to store that JsonDocument in the default bucket. The upsert method is a inserting a document if it does not exist or updates it if it exists:

bucket.upsert(document);

Run

To test if this worked, fetch the document again using bucket.get("asmith") and print the result. Once you run the application you should see something like this:

Jun 03, 2016 5:17:45 PM com.couchbase.client.core.CouchbaseCore <init>
INFO: CouchbaseEnvironment: {sslEnabled=false, sslKeystoreFile='null', sslKeystorePassword='null', queryEnabled=false, queryPort=8093, bootstrapHttpEnabled=true, bootstrapCarrierEnabled=true, bootstrapHttpDirectPort=8091, bootstrapHttpSslPort=18091, bootstrapCarrierDirectPort=11210, bootstrapCarrierSslPort=11207, ioPoolSize=8, computationPoolSize=8, responseBufferSize=16384, requestBufferSize=16384, kvServiceEndpoints=1, viewServiceEndpoints=1, queryServiceEndpoints=1, searchServiceEndpoints=1, ioPool=NioEventLoopGroup, coreScheduler=CoreScheduler, eventBus=DefaultEventBus, packageNameAndVersion=couchbase-jvm-core/1.2.5 (git: 1.2.5), dcpEnabled=false, retryStrategy=BestEffort, maxRequestLifetime=75000, retryDelay=ExponentialDelay{growBy 1.0 MICROSECONDS, powers of 2; lower=100, upper=100000}, reconnectDelay=ExponentialDelay{growBy 1.0 MILLISECONDS, powers of 2; lower=32, upper=4096}, observeIntervalDelay=ExponentialDelay{growBy 1.0 MICROSECONDS, powers of 2; lower=10, upper=100000}, keepAliveInterval=30000, autoreleaseAfter=2000, bufferPoolingEnabled=true, tcpNodelayEnabled=true, mutationTokensEnabled=false, socketConnectTimeout=1000, dcpConnectionBufferSize=20971520, dcpConnectionBufferAckThreshold=0.2, queryTimeout=75000, viewTimeout=75000, kvTimeout=2500, connectTimeout=5000, disconnectTimeout=25000, dnsSrvEnabled=false}
Jun 03, 2016 5:17:45 PM com.couchbase.client.core.node.CouchbaseNode signalConnected
INFO: Connected to Node localhost
Jun 03, 2016 5:17:45 PM com.couchbase.client.core.config.DefaultConfigurationProvider$8 call
INFO: Opened bucket default
{"firstName":"Alice","lastName":"Smith"}

The most important line is the last one showing the document has been stored successfuly. The previous line are logs showing the Couchbase SDK connection to the cluster.

Summary

Congratulations! You set up a Couchbase server and wrote a simple application that stores a document in Couchbase.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages