참고: This post uses the the Couchbase Analytics Data Definition Language as of the version 5.5 preview release. For updates and information on breaking changes in newer versions, please refer to Changes to the Couchbase Analytics Service.
The Couchbase Analytics Service, sometimes referred to as CBAS, is a great thing for Couchbase and your NoSQL data needs because it allows you to create and run potentially complex queries against massive amounts of data efficiently using a familiar SQL dialect.
We’re going to see how to use this analytics service, new as of Couchbase 5.5, with the Node.js SDK for 카우치베이스.
For this example, we’re going to be working with the travel-sample example Bucket that can be optionally installed with Couchbase Server. It isn’t considered a huge dataset, but it will work with this example.
Configuring a Couchbase Analytics Dataset
Within the 분석 tab of the Couchbase Administrative Dashboard, execute the following query:
|
1 |
CREATE BUCKET 여행 WITH { “name”:“travel-sample” }; |
The above query will create a 여행 Analytics Bucket based of the Couchbase travel-sample Bucket.
With the bucket created, we can create a shadow dataset to work with. Execute the following queries:
|
1 2 |
CREATE SHADOW DATASET airlines ON 여행 WHERE `type` = “airline”; CREATE SHADOW DATASET airports ON 여행 WHERE `type` = “airport”; |
The above queries will create two shadow datasets based on data that exists in the travel-sample bucket. While the shadow datasets are created, they need to be initialized:
|
1 |
CONNECT BUCKET 여행; |
Once initialized, the Analytics service will begin making its copy of the documents and actively monitor for changes. We can test the data by executing a query like the following:
|
1 |
SELECT * FROM airports; |
The above query will find all documents from our shadow dataset. In other words, we’ll get all documents that have a type property of airline.
This is all great, but as of now, we’ve only used the Couchbase Administrative Dashboard. We want to be able to run queries against our Analytics dataset from Node.js.
Querying CBAS from a Node.js Application
Assuming you have Node.js installed, create a new project directory and execute the following command:
|
1 |
npm init –y |
The above command will create a new package.json file at your CLI path. In order to communicate with Couchbase, we’re going to need the SDK. It can be installed by executing the following from the CLI:
|
1 |
npm install 카우치베이스 —save |
Now we can start developing our application. Create an app.js file in your project with the following:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
상수 카우치베이스 = require(“couchbase”); 변수 cluster = 새로운 카우치베이스.클러스터(“couchbase://localhost”); cluster.authenticate(“travel”, “123456”); cluster.enableCbas([“localhost:8095”]); 변수 statement = “SELECT * FROM airports”; 변수 질의 = 카우치베이스.CbasQuery.fromString(statement); cluster.질의(질의, (error, result) =< { 만약(error) { throw error; } 콘솔.로그(result); }); |
Not too much is happening in the above code, but we’ll break it down anyways.
|
1 2 3 |
변수 cluster = 새로운 카우치베이스.클러스터(“couchbase://localhost”); cluster.authenticate(“travel”, “123456”); cluster.enableCbas([“localhost:8095”]); |
Before we can start executing queries, we need to connect to the cluster and enable interactions with the Analytics service. We also need to authenticate to the cluster with a user that has permission to use Analytics. In this example, Analytics is active on the node that we’re connecting to.
Notice that we’re not connecting to a Bucket. Had we wanted to use N1QL, we’d open a Bucket, but we’re not using N1QL even if it might look similar.
|
1 2 3 4 5 6 7 8 9 |
변수 statement = “SELECT * FROM airports”; 변수 질의 = 카우치베이스.CbasQuery.fromString(statement); cluster.질의(질의, (error, result) =< { 만약(error) { throw error; } 콘솔.로그(result); }); |
Using a simple Analytics query, we can parse it and execute it. The results of the query will be returned and printed to the logs.
Not too bad right?
Since Analytics uses SQL++, you can make your queries much more complicated using other operators such as WHERE, subqueries, and operations on collections.
결론
You just saw how to create a simple Node.js application that uses the Couchbase Analytics Service (CBAS) rather than N1QL. Analytics and SQL++ is not a replacement to N1QL, but instead a compliment for much larger or complicated datasets, where as N1QL shines on smaller datasets.
To learn more about Analytics or the Couchbase Node.js SDK, visit the 카우치베이스 개발자 포털. Learn about other new features available in the Couchbase Server 5.5 release.

댓글 남기기
댓글을 달기 위해서는 로그인해야합니다.