모범 사례 및 튜토리얼

Using Couchbase Analytics with Node.js and JavaScript

참고: 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:

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:

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:

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:

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:

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:

Now we can start developing our application. Create an app.js file in your project with the following:

Not too much is happening in the above code, but we’ll break it down anyways.

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.

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.

이 기사 공유하기

작가

닉 라보이(Nic Raboy)는 모던 웹 및 모바일 개발 기술의 옹호자입니다. 그는 자바, 자바스크립트, 골랑(Golang)을 비롯해 앵귤러, 네이티브스크립트, 아파치 코르도바 같은 다양한 프레임워크 경험을 가지고 있습니다. 닉은 웹 및 모바일 개발을 더 쉽게 이해할 수 있도록 돕는 자신의 개발 경험에 대해 글을 씁니다.

댓글 남기기

카우치베이스 카펠라를 시작할 준비가 되셨나요?

개발 시작하기

NoSQL을 탐색하고, 리소스를 찾아보고, 튜토리얼을 시작하려면 개발자 포털을 확인하세요.

카펠라 프리 사용하기

단 몇 번의 클릭으로 카우치베이스(Couchbase)를 직접 체험해 보세요. Capella DBaaS는 시작하기 가장 쉽고 빠른 방법입니다.

연락해

Couchbase 제품군에 대해 더 알고 싶으신가요? 저희가 도와드리겠습니다.