자바

Spring Data Couchbase 2 is out! Quick getting started with Spring Initializr

4 분 읽기

Good News everyone, Spring Data Couchbase 2 was released last week and is now available on the awesome Spring Initializr. This awesome website let you start a Spring/Couchbase project very quickly. Just select the dependencies you want for your project, hit the Generate Project button and you’ll end up with an archive containing a project ready to be imported. This wizard is also available in the Spring Tool Suite.

1/ Go to https://start.spring.io/

2/ Right now it’s only available with Spring Boot 1.4.0-SNAPSHOT so make sure you start by selecting the right SpringBoot version.

3/ Select Couchbase

4/ Add any other dependencies you might require for your project

5/ Generate the project

Now you have an archive ready to be imported in your editor of choice. As I am doing Spring stuff I tend to stick with their default tool suite. It’s basically Eclipse pre-configured with the right set of plugins.

6/ I have stuck with Maven in the generation wizard so I can simply import it as an existing maven project.

To activate the default config, you need to add the following in your application.properties file:

spring.data.couchbase.bucket.name=default

Or you can create a @Configuration Bean that extends AbstractCouchbaseConfiguration. Now we’re ready to start. Here’s a quick inventory of what the Spring Boot Couchbase auto-config gives you.

Properties

You can define several properties to configure your access to Couchbase, here are the defaults:

# the name of the bucket to connect to, it's mandatory to enable the auto-config
spring.data.couchbase.bucket.name=default
# the password of the bucket to connect to
spring.data.couchbase.bucket.password=
# adress of the Couchbase Cluster nodes
spring.data.couchbase.bootstrap-hosts=127.0.0.1
# Automatic index creation based only annotations (@ViewIndexed, @N1qlPrimaryIndexed, @N1qlSecondaryIndexed)
spring.data.couchbase.auto-index=false
# Default level of consistency (read-your-own-writes|eventually-consistent|strongly-consistent|update-after)
spring.data.couchbase.consistency=read-your-own-writes
  • read-your-own-writes is stale=false and ScanConsistency=statement_plus
  • strongly-consistent is stale=false and ScanConsistency=request_plus
  • update-after is stale=update-after and ScanConsistency=not_bounded
  • eventually-consistent is stale=true and ScanConsistency=not_bounded

Stale Level

If stale=true is set, Couchbase will not refresh the view even if it is stale. The benefit of this is an improved query latency. If stale=update_after is set, Couchbase will update the view after the stale result is returned. If stale=false is set, Couchbase will refresh the view and return you most updated results.

Scan Consistency Level

not_bounded

This is the default (for single-statement requests). No timestamp vector is used in the index scan. This is also the fastest mode, because we avoid the cost of obtaining the vector, and we also avoid any wait for the index to catch up to the vector.

request_plus

This implements strong consistency per request. Before processing the request, a current vector is obtained. The vector is used as a lower bound for the statements in the request. If there are DML statements in the request, RYOW is also applied within the request.

statement_plus

This implements strong consistency per statement. Before processing each statement, a current vector is obtained and used as a lower bound for that statement.

Index annotation

Here’s an example of repository with automatic index creation. When the repository bean is initialized, we make sure the index defined by the annotations exist and if not create them.

@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "customer")
@N1qlSecondaryIndexed(indexName = "customerSecondaryIndex")
public interface CustomerRepository extends CouchbasePagingAndSortingRepository<Customer, String> {}

Index should be used for development only. We advise turning automatic index generation off in production and make sure you create the right index in the right nodes during your deployment process.

ViewIndexed

This annotation lets you define the name of the design document and View name as well as a custom map and reduce function.

N1qlPrimaryIndexed

This annotation makes sure that the bucket associated to the current repository will have a N1QL primary index.

N1qlSecondaryIndexed

This annotation makes sure that a secondary index exists on the type of your Entity.

Available Beans

If you have enabled the auto-config, you will have access to several Beans, here’s a list of the most useful ones:

  • CouchbaseTemplate couchbaseTemplate
    • Gives you access to the low-level Spring Data Couchbase API
  • ValidatingCouchbaseEventListener validationEventListener
    • javax.validation dependant entities validator. When it is registered as Spring component its automatically invoked before entities are saved in database.
  • CustomConversions customConversions
    • Used to capture custom JSON type conversions.
  • Bucket couchbaseClient
    • Direct access to the configured Bucket

결론

This should have given you a good overview of what is available when starting a new project with spring-boot-starter-data-couchbase. For more details about Spring Data Couchbase 2, please refer to the 문서화 or checkout this blog regularly for more Spring/Couchbase goodness.

이 기사 공유하기

작가

로랑은 파리에 사는 찌질한 매니아 기질이 있는 헤비메탈 마니아입니다. 그는 주로 자바(Java)로 코드를 작성하고 AsciiDoc으로 구조화된 텍스트를 작성하며, 데이터, 리액티브 프로그래밍 및 기타 유행어 같은 주제에 대해 자주 이야기합니다. 그는 또한 Clever Cloud와 Nuxeo의 전직 개발자 애드보케이트로 활동하며 해당 커뮤니티가 더 크고 강하게 성장하도록 시간과 전문 지식을 쏟아부었습니다. 현재는 카우치베이스(Couchbase)에서 개발자 관계(Developer Relations)를 총괄하고 있습니다.

댓글 남기기

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

개발 시작하기

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

카펠라 프리 사용하기

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

연락해

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