Announcing General Availability Of Couchbase Ruby SDK 3.0 !

Couchbase
Couchbase

--

By: Arun Vijayraghavan

“ We are delighted to announce the General Availability of Couchbase Ruby SDK 3.0 ! “

Useful Links

Introduction

The Couchbase Ruby SDK allows you to connect to a Couchbase cluster from Ruby using simplified and high-performance API which is an extension of native Ruby. Our SDK is written with the future of server features in mind, providing support for capabilities like Scopes and Collections which will be the new way of managing data.

Compatibility

Couchbase Ruby SDK 3.0 is

  • Compatible with any MRI Ruby version greater than and including 2.5.0.
  • Fully supported for Couchbase server version greater than and including 6.0.0.

More information on compatibility can be found here.

Installation

You can install Ruby by following the steps as mentioned in the official Ruby Website.

Once Ruby is installed, Installing the version of Ruby SDK for Couchbase is very simple. On a terminal window simply type the following command,

gem install couchbase

We also provide gem repositories with precompiled extensions. Use them in case the C/C++ compiler cannot be installed on the box to build the extension during gem install. See this page for more instructions: Couchbase Ruby Release Notes and Archives

Connecting to Couchbase Cluster

A connection to a Couchbase Server cluster is represented by a Cluster object. A Cluster provides access to Buckets, Scopes, and Collections, as well as various Couchbase services and management interfaces.

Connecting to Couchbase Cluster is very simple, all you would need to do is import couchbase library and call the connect method on the cluster object by passing in the credentials using Cluster Options as you see below.

require "couchbase" # import couchbase library
include Couchbase
options = Cluster::ClusterOptions.new
options.authenticate("Administrator", "password")
cluster = Cluster.connect("couchbase://localhost", options)

Features

This new gem ships with a lot of new and enhanced features which are simple and less verbose.

Below you will find highlights of some of the features illustrated with examples that use Couchbase Server version 6.5.1 in Developer preview mode with `travel-sample` and `beer-sample` buckets added.

For a comprehensive set of features refer to our documentation.

Key Value Operation

Key Value operation, also known as Data Service offers the simplest and quickest way to retrieve or mutate data where the document key is known.

The example uses travel-samples default collection to,

  • Create a new document (update if a document already exists) with the key “foo” .
  • Retrieve the document.
  • Remove / Delete the document.
require 'couchbase'
include Couchbase
options = Cluster::ClusterOptions.new
options.authenticate("Administrator", "password")
cluster = Cluster.connect("couchbase://localhost", options)
bucket = cluster.bucket("travel-sample")
collection = bucket.default_collection
res = collection.upsert("foo", {"bar" => 42})
res = collection.get("foo")
puts res.content
res = collection.remove("foo")
puts res
cluster.disconnect

Sub-Document Operations

Sub-document operations can be used to efficiently access parts of documents using sub-document paths.They may be quicker and more network-efficient than full-document operations such as upsert, replace and get because they only transmit the accessed sections of the document over the network. These operations are also atomic, allowing safe modifications to documents with built-in concurrency control.

The example below uses sub document paths fax, email to mutate a document with the key “customer123”.

require 'couchbase'
include Couchbase
options = Cluster::ClusterOptions.new
options.authenticate("Administrator", "password")
cluster = Cluster.connect("couchbase://localhost", options)
bucket = cluster.bucket("default")
collection = bucket.default_collection
document = {
name: "Douglas Reynholm",
email: "douglas@reynholmindustries.com"
}
collection.upsert("customer123", document)
res = collection.mutate_in("customer123", [
MutateInSpec.upsert("fax", "311-555-0151"),
MutateInSpec.replace("email", "dougr96@hotmail.com"),
])

Query

You can query for documents in Couchbase using the N1QL query language, a language based on SQL, but designed for structured and flexible JSON documents. Querying can solve typical programming tasks such as finding a user profile by email address, facebook login, or user ID.

The example below uses named parameters to retrieve 10 documents of type “hotel” from `travel-sample` bucket.

require 'couchbase'include Couchbaseoptions = Cluster::ClusterOptions.new
options.authenticate("Administrator", "password")
cluster = Cluster.connect("couchbase://localhost", options)
options = Cluster::QueryOptions.new
options.named_parameters({type: "hotel"})
res = cluster.query("SELECT * FROM `travel-sample` WHERE type = $type LIMIT 10", options)
res.rows.each do |row|
puts "#{row["travel-sample"]["country"]}. #{row["travel-sample"]["name"]}"
end

Analytics

Couchbase’s analytics service provides the ability to run complex ad-hoc queries to gather insights on operational data without impeding operational workloads. The query syntax is essentially the same as N1QL, allowing you to leverage your SQL knowledge to run analytical queries on JSON data.

The example below uses positional parameters to retrieve `count of airports` in France from airports datasetcreated on `travel-sample` bucket.

require 'couchbase'include Couchbaseoptions = Cluster::ClusterOptions.new
options.authenticate("Administrator", "password")
cluster = Cluster.connect("couchbase://localhost", options)
options = Cluster::AnalyticsOptions.new
options.positional_parameters(["France"])
result = cluster.analytics_query('SELECT COUNT(*) AS airport_count FROM airports WHERE country = ?',options)
puts "Airports in France: #{res.rows.first["airport_count"]}"

Conclusion

Hope you are already excited and are ready to build your next application using this new gem !

If you have any questions or feedback, drop by our developer community. We’d love to hear from you. You can also check out the SDK over at GitHub.

Happy Programming !!!

Arun Vijayraghavan is the Principal Product Manager for SDK and Connectors at Couchbase. As a customer obsessed product leader, he strives to shape the future of products, making critical decisions between performance, features, and time-to-market. He has a demonstrated ability and an established track record of over 20 years of providing strategic guidance to companies to launch developer platforms and new products in order to pull together the single vision of maximizing business value of the product. Arun holds dual master degree’s in Physics and Information Technology.

--

--

Couchbase
Couchbase

The world’s best open source database for building scalable, high performance web, mobile & IoT applications. www.couchbase.com