This example shows how to implement a simple data model for financial transactions, generate data using fakeit, query data using a Spring Boot JAVA application and optimize N1QL queries using GSI indexes and prepared statements.
docker-compose is used to start everything with minimum of efforts.
First, you need to build the application, which generates a docker image using jib :
./mvnw clean install
Then, go to docker
directory, and start everything :
docker-compose up --build
If you have your local instance of Couchbase or you don't want to use Docker, you can do the following :
Fakeit is used to describe a simple financial transactions data model, and populate Couchbase with generated data.
To start generating data, make sure that you have npm installed, and Couchbase started, then execute the following :
npm install
npm start
The application is a Java REST service implemented using Spring Boot, with Swagger-UI to test requests, and Couchmove to automatically create necessary N1QL indexes and prepared statements for Couchbase.
The Couchbase repositories are implemented using stock java SDK instead of Spring Data Couchbase to demonstrate how it works.
To build and start the application :
./mvnw
When everything is up, open your browser to http://localhost:8080/swagger-ui.html
For example, go to transaction-resource/getByBates, click on Try it out, then enter the fromDate and toDate in ISO-8601 instant format (2018-11-01T00:00:00.00Z)
In addition to indexes, Couchmove also create some prepared queries that we can test using cbc-n1qlback.
Some queries are also automatically copied to Couchbase Docker instance from docker/couchbase/queries
Go to docker
directory, then execute the following to open an interactive bash shell
docker-compose exec couchbase bash
To compare prepared vs simple queries, execute the following :
cbc-n1qlback -U couchbase://localhost/finance -P password -t 5 -f simple_queries.txt
cbc-n1qlback -U couchbase://localhost/finance -P password -t 5 -f prepared_queries.txt