Scopes and Collections aren’t just for organizing your data – now they can be used for your data analytics as well.
As part of the Couchbase 7.0 release, support for Scopes and Collections has been extended to the Analytics Service as well.
Scopes and Collections are logical containers within a Couchbase Bucket. A Collection is a data container within a Bucket. A Scope is a mechanism for grouping multiple Collections within a Bucket. Collections allow users to group related data – much like tables in relational databases.
Let’s take a closer look at how your team can use Scopes and Collections as part of your enterprise data analysis. But first, let’s clarify some terminology.
Collections Terminology in Couchbase 7.0
The Couchbase Analytics Service includes the following key concepts that are used in the 6.x and 7.0 releases:
Dataverses: Dataverse is short for data universe, a top-level organizing concept and a namespace for other metadata entities (such as links, Buckets, datasets, indexes, etc). There is one Default dataverse already created (and it can’t be deleted).
Datasets: Datasets are containers that hold JSON objects linked to underlying Collections. As you can imagine, the new Collections feature fits well with Analytics datasets. In fact, datasets are synonymous with Collections.
How Collections Work in Couchbase Analytics
Before digging into how Collections work in Couchbase Analytics, let’s take a step back and look at how Collections work in the Data Service.
Consider a travel industry dataset that involves airline and hotel bookings. Let’s say we have a Bucket named travel
. A Bucket is the equivalent of a database in the relational model. Within that travel
Bucket, we have a Scope called inventory
(similar to a schema in RDBMS) and within that inventory
Scope, we have four Collections: airlines
, airports
, routes
and hotels
(these would be tables in RDBMS).
I’ve illustrated this example in Diagram A below:
Next up, Diagram B below shows how Collections in the Data Service appear in the Analytics Service:
There are multiple ways to use your Collections with the Couchbase Analytics Service. In our example, let’s say we want to enable the Hotel Collection. In the Analytics workbench, we have a few options using these Analytics Data Definition Languages (DDLs):
- If you’re familiar with the Analytics Service, leverage any of the following statements (supported in 6.x and 7.0) to create the Hotel Collection from above:
- Create a dataverse/namespace using the following:
- CREATE DATAVERSE `travel-sample`.inventory;
- Create the Hotel Collection using the following statement:
- CREATE DATASET `travel-sample`.inventory.hotel ON `travel-sample`.inventory.hotel;
- Create a dataverse/namespace using the following:
-
ALTER COLLECTION `travel-sample`.inventory.hotel ENABLE ANALYTICS;
- The
ENABLE
option creates an Analytics namespace or dataverse called
if one does not exist already.travel-sample
.inventory - The Analytics Collection or dataset in the above namespace is
hotel
.
- The
-
CREATE ANALYTICS COLLECTION `travel-sample`.inventory.hotel ON `travel-sample`.inventory.hotel;
- This statement assumes that the dataverse
is already in place. If not, you can create one as mentioned in option #1 above. Also, we support a new DDL (below) which creates a dataverse using Scope nomenclature:travel-sample
.inventory- CREATE ANALYTICS SCOPE `travel-sample`.inventory;
- Once the
CREATE ANALYTICS
Collection statement above is executed, the Hotel Collection is created as well.
- This statement assumes that the dataverse
Conclusion
Collections are easy to use with the Analytics Service.
They align with N1QL naming conventions to refer to Buckets, Scopes and Collections so your queries are interchangeable between the Query and Analytics engines. To ensure backward compatibility with 6.x releases, the Analytics Service continues to support DDL statements to create and manage dataverses and datasets.
If you want to learn more about the Couchbase Server 7.0 release, check out What’s New and/or the 7.0 release notes.
I hope you’re excited about this new functionality of Scopes and Collections working with Couchbase Analytics. I look forward to hearing your feedback on Couchbase Forums.
Download Couchbase 7 today