Performing Key/Value Operations

Introduction

In this lab you will gain experience working with Key/Value operations using a variety of different SDKs. You can either choose to explore each of the languages and the associated SDK or you can just explore the language of your choice.

What you will gain experience with:

  • Connecting to a Couchbase Database

  • Performing Key/Value operations

  • Handling errors

Estimated time to complete: 30 minutes

Instructions

If you do not currently have an active lab session running, launch it now. Your starting point will be here

Once you have a session, locate the set of code examples for the Key/Value operations. Key/Value code examples

Exploring the connection

Locate the first few Lab 3 code examples in the navigation menu at the top of the page. Start by looking these code examples. Specifically, focus on the way in which the connection occurs in obtaining a Cluster reference. Some things to think about as you examine the examples include:

  • What information is being used to connect to the database?

  • Given that the connection to the Capella database is secure, how does this connection get made using the various SDK languages?

Exploring Get operations

Now, look specifically at the get operation in various languages.

  • Which set of bucket data is being accessed?

  • How is the result processed from the get() operation?

  • What happens if a document for the specified key doesn’t exist?

Run the examples (or at least one of your choice). Observe the output produced. Observe the correlation between the code executed and the output displayed.

Exploring Insert operations

Next, examine the code examples for performing an insert operation. There are a couple of tasks being performed here

  1. Insert a document - This attempts to insert a new document, assuming there is not currently a current document

  2. Fetch an element of the document inserted - Performs a sub-document fetch using a lookup-in operation

Take some time to think about the assumptions that exist for each of these operations. What happens if the assumption is not true? How are these 'edge' conditions handled?

Run one or more of the code examples. You may find after running the the insert code once that you will get an error message Document already exists.

There are two ways to address this.

  • Write a line of code before the insert operation to delete (remove) the entry. Re-run the code and verify the insert operation works.

  • Open the Web console, locate the couchmusic2 bucket and access the list of documents. Find the entry with the key country::YY and delete it.

Lab Summary

In this lab, you had a chance to explore some examples of performing key/value operations using different languages. Some of the points you should have gained experience with include:

  • How to connect to the Couchbase database, including obtaining bucket and collection reference

  • How each language supports operations, such as get, insert and sub-document fetch

  • How to handle edge conditions, such as trying to perform a get operation when the key doesn’t exist

Note that the examples for connecting to a secure database would typically require the use of a shared certificate. You saw how it is possible to obtain such a certificate from the Capella database. This certificate can be provided via the SDK to ensure that only clients possessing this certificate may exchange secure data between the client and the database.

You can see a more complete example of how to do this for the various languages at the following links. Click on the Couchbase Capella Sample link to see the full example for connecting securely to a Capella database.

Congratulations! You have completed this lab.

results matching ""

    No results matching ""