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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Using Golang to Fetch Couchbase Documents With Ease

Using Golang to Fetch Couchbase Documents With Ease

Looking up Couchbase documents by key will pretty much always be faster than other alternatives. See how you can use Go to find what you need.

Nic Raboy user avatar by
Nic Raboy
·
Jan. 04, 17 · Tutorial
Like (2)
Save
Tweet
Share
4.49K Views

Join the DZone community and get the full member experience.

Join For Free

Couchbase and the various server SDKs offer many different ways to query for data. You could write N1QL queries, view queries, or you could even look up documents by their key. Of the three possible ways to obtain data, doing lookups based on defined keys will always be faster than executing a query that uses an index.

What happens when you have multiple document keys and you need to obtain all the corresponding documents? The first thing to come to mind might be to loop through these keys, performing a lookup operation in each iteration of the loop. That works, but at the price of a new network request per lookup. Is there a better way? Absolutely!

What you can do is a bulk operation against the database. I've seen this question come up numerous times and I even wrote a tutorial on how to accomplish such a task in Node.js. However, what if you're not a JavaScript developer? This time we're going to see how to perform a batch request using the Go programming language.

Take the following Golang application for example:

package main import ( "fmt" "github.com/couchbase/gocb" ) type Person struct {
    Firstname string `json:"firstname,omitempty"`
    Lastname  string `json:"lastname,omitempty"`
    Website   string `json:"website,omitempty"`
    Blog      string `json:"blog,omitempty"`
}

var bucket *gocb.Bucket func main() {
    cluster, _ := gocb.Connect("couchbase://localhost")
    bucket, _ = cluster.OpenBucket("example", "")

    var items []gocb.BulkOp

    items = append(items, &gocb.GetOp{Key: "nraboy", Value: &Person{}})
    items = append(items, &gocb.GetOp{Key: "agupta", Value: &Person{}})

    error := bucket.Do(items)

    if error != nil {
        fmt.Println("ERROR: ", error)
    }

    for i := 0; i < len(items); i++ {
        person, _ := items[i].(*gocb.GetOp).Value.(*Person)
        if *person == (Person{}) {
            fmt.Printf("`%+v` does not exist in the database\n", items[i].(*gocb.GetOp).Key)
        } else {
            fmt.Printf("Key: %+v, Value: %+v\n", items[i].(*gocb.GetOp).Key, person)
        }
    }
}


In the above application, we have a Person structure that represents each of our Couchbase documents. After establishing a connection to the Couchbase cluster and opening a particular bucket, we create a slice of bulk operations called items.

Each key that we wish to look up in our request will go into this items slice, and the resulting value will be stored in it as well.

Using the Do function, we can execute the bulk operation, keeping an eye out for errors. In this case, errors are not missing keys, they are execution errors. After executing the request, we can loop through each item in the items slice. If the value of a particular item equals that of an initialized, but empty Person object, it means that the key was not found on the server. Otherwise, we're just going to print out the key and the found document.

If you're interested in the topic of performance, Kirk Kirkconnel wrote a blog post that explains the differences. It can be found here. You can also read about batching operations in the Couchbase documentation.

Document Database Golang Ease (programming language) Fetch (FTP client)

Published at DZone with permission of Nic Raboy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Rust vs Go: Which Is Better?
  • Using GPT-3 in Our Applications
  • Choosing the Right Framework for Your Project
  • Master Spring Boot 3 With GraalVM Native Image

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: