Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: Java 1.0
Community Wiki and Resources
Wiki: Java Client Library
Download Client Library
JavaDoc
Java Client Library
SDK Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
1 Getting Started
Chapter Sections
Chapters

1.4. A More Substantial Program

Please download the Sample Code if you're interested in making a more substantial program that you can run. The program will create a user specified number of threads, that each try to create (or read from Couchbase) 100 random numbers. The code creates a CouchbaseClient object instance for each thread, and then proceeds to perform a gets() operation looking for specific keys. The gets() operation will return null if the key has not been set. In this case the thread will create the value itself and set it into Couchbase and it will incur a 100 millisecond penalty for doing so. This simulates an expensive database operation. You can find the full source code for the small application attached to the end of this article.

Let's discuss a few parts of the program, so you can understand the fundamentals of connecting to Couchbase servers, testing for the existence of particular key-value pairs, and setting a value to a key. These few operations will give you more of an idea of how to begin.

Listing 2. Connecting to a set of Couchbase servers:

URI server = new URI(addresses);

    ArrayList<URI> serverList = new ArrayList<URI>();

    serverList.add(server);
    CouchbaseClient client = new CouchbaseClient(
        serverList, "default", "");

You can see, from these lines that you'll need to obtain an instance of a CouchbaseClient. There are numerous ways to construct one, but a constructor that is quite useful involved the ArrayList of URIs.

http://host-or-ip:port/pools

The port you will be connecting to will be the port 8091 which is effectively a proxy that knows about all of the other servers in the cluster and will provide quick protocol access. So in the case of this cluster, providing an addresses string as follows, worked very well:

String addresses = "10.0.0.33:8091/pools"

Listing 3 is an abridged excerpt that shows the creation of an IntegerTranscoder, which is a useful class for converting objects in Couchbase back to integers when needed. This is for convenience and reduces type casting. You can then see that a the gets() method is called. This returns a CASValue<T> of type integer which is useful for checking and setting a value. If the value is null it means that Couchbase hasn't been given a value for this key. The code then sets a value. Otherwise, we can get its value and do something with it.

Listing 3. Check And Set operations

IntegerTranscoder intTranscoder = new IntegerTranscoder();

    CASValue<Integer> value = client.gets(key,
        intTranscoder);

    if (value == null) {
        // The value doesn't exist in Couchbase
        client.set(key, 15, rand.nextInt(), intTranscoder);

        // Simulate the value taking time to create.
        Thread.sleep(100);

        created++;

    } else {

        int v = value.getValue();

    }

Setting values in Couchbase are done asynchronously, and the application does not have to wait for these to be completed. Sometimes, though, you may want to ensure that Couchbase has been sent some values, and you can do this by calling client.waitForQueues() and giving it a timeout for waiting for this to occur, as shown in Listing 4.

Listing 4. Waiting for the data to be set into Couchbase.

client.waitForQueues(1, TimeUnit.MINUTES);