카우치베이스 아키텍처

Couchbase N1QL을 활용한 데이터 구조 및 쿼리 (JSON용 SQL)

In the Data Structures for NoSQL Applications post, we used simplified JSON data access through native collections, maps, and more. This post demonstrates querying that data using higher-level N1QL queries, the SQL-based language for JSON.

chart of couchbase data structures and examples
Summary of Couchbase Data Structure types and JSON samples.

Developers can focus on managing basic data structures and data types using programming languages. Meanwhile, DBAs and analysts can access the same data using a query language. Couchbase does this using NoSQL data indexing methods and N1QL querying services.

Search queries with Couchbase full-text search engine are also possible and will be covered in a future post.

What is NoSQL database indexing? 

Indexing examines the data and determines how to find those elements again. Indexing can be applied to JSON documents, keys/fields, or values in fields. Keys in JSON documents act like column names in tabular database indexing systems.

The database manages these collections of elements and values as indexes that point back to the source. The database backend updates indexing as documents change while storing data. Meanwhile, DBAs can optimize indexing for specific use cases such as high-volume writes or queries.

Using indexes, N1QL queries can quickly find field values that match without scanning through the original data. This method also applies to the common data structures, collections, and scopes introduced in Couchbase 7.0.

Querying Couchbase data structures with N1QL

The Couchbase web console is an easy way to view data structure documents in the database. Notice how data structure documents are often much simpler than more complex JSON documents.

data structure documents listed in the bucket preview screen
List of data structure documents in the bucket

To start writing N1QL queries, indexing your data structure data is required. At a minimum, a bucket-wide primary index is needed for basic operations.

For documents using the new collections features from Couchbase 7.0, they must also be indexed.

Primary indexes struggle with large datasets but are excellent for exploring small amounts of data quickly. Use Global Secondary Indexes (GSI) when moving toward production in a big data project.

Listing all data documents and IDs

However, more targeted indexing is not always possible as data structures may not have named keys. For example, a counter is just an ID and an integer value with no field name. But, if you have a map, you can create an index that targets an internal key.

A basic query for document ID can return all documents and show the field names being used.

query results showing data structure documents

CurrentScore is a basic counter, whereas gameList is a list/collection, etc.

Querying scopes/collections for data structures

If using scopes and collections, add them to the from clause.

Retrieve a specific value from a named data structure object by adding it to the where clause.

JSON query results showing a document ID and counter value

When specifying field names (and no ID) the query returns matching values across all documents.

JSON N1QL query showing records containing specific field names

The query returns objects with the specific fields and their values. In the above results, only one of the data structures had the contact field. Two others had a 이름 field, but several blank objects show there were no matching fields.

To be scalable, all query applications should also use global secondary indexes (GSI) for specific fields.

이것 Database Indexing Best Practices article covers more computer science scenarios used in big data software engineering.

Bringing it all together

As you can see, querying documents and related subcomponents is very simple using Couchbase. Strategic use of sophisticated indexing methods provides even more ways to access the data your applications are creating.

Couchbase dramatically simplifies system architectures, allowing developers to get started without a lot of heavy lifting. These references will help you get started quickly.

이 기사 공유하기

작가

카우치베이스(Couchbase)의 시니어 제품 마케팅 매니저로서 제품에 대한 대중의 인지도를 높이는 동시에, 가치 있는 콘텐츠로 영업 현장 팀을 지원하고 있습니다.

댓글 남기기

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

개발 시작하기

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

카펠라 프리 사용하기

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

연락해

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