Technical Thoughts musings on technology


Back

Couchbase with Golang

Written by Ryan Moore on 19 May 2014

In this post I want to talk about using Couchbase with Go. To start off with we'll use the Couchbase driver on Github.

The couchbase driver is pretty easy to use. Just connect to the database, get your pool and bucket, and then start setting and getting data. The following code example assumes that you have couchbase installed and running locally as localhost. For directions on how to install couchbase please reference the documentation on their website.

package main

import (
    "fmt"
    "log"

    "github.com/couchbaselabs/go-couchbase"
)

type User struct {
    Name string `json:"name"`
    Id   string `json:"id"`
}

func main() {
    connection, err := couchbase.Connect("http://localhost:8091")
    if err != nil {
        log.Fatalf("Failed to connect to couchbase (%s)\n", err)
    }

    pool, err := connection.GetPool("default")
    if err != nil {
        log.Fatalf("Failed to get pool from couchbase (%s)\n", err)
    }

    bucket, err := pool.GetBucket("default")
    if err != nil {
        log.Fatalf("Failed to get bucket from couchbase (%s)\n", err)
    }

    user := User{"Frank", "1"}

    added, err := bucket.Add(user.Id, 0, user)
    if err != nil {
        log.Fatalf("Failed to write data to the cluster (%s)\n", err)
    }

    if !added {
        log.Fatalf("A Document with the same id of (%s) already exists.\n", user.Id)
    }

    user = User{}

    err = bucket.Get("1", &user)
    if err != nil {
        log.Fatalf("Failed to get data from the cluster (%s)\n", err)
    }
    fmt.Printf("Got back a user with a name of (%s) and id (%s)\n", user.Name, user.Id)

}

The code above doesn't do too many surprising things but there are some things that stood out to me when first using the Couchbase API. The first thing that I found interesting was the GetPool method. If you read a lot of the Couchbase documentation Pools are not really mentioned much, it generally appears as Cluster. After doing some digging, it appears that the Couchbase team put the concept of a Pool into the codebase, but has not yet implemented features to support multiple pools. Currently you should always use the Default pool.

The other thing that threw me off was in the Add method. In the code above you'll notice the 0 in bucket.Add(user.Id, 0, user). That represents when that document should expire. Since we do not want the document to expire we pass in 0.

The Couchbase API is very easy to use in Go due to the automatic marshalling support that is built in. That's what all the `json:"name"` stuff is about in the User struct.

The code above and more can be found in this Github repo.

In the next post we'll explore using the Couchbase SDK in a Golang webapp.

comments powered by Disqus