Developer Tutorial: Student Record System
Learn how to create and deploy a student records database on Capella Operational and connect it to your application, using the Java SDK.
Couchbase is a schema-less JSON document database designed for high performance, scalability, and fast development. This tutorial teaches you about the key concepts behind Couchbase and how they differ from traditional SQL database systems like MySQL and Oracle.
This tutorial is designed for use with Capella Operational cloud services. If you wish to use a standalone or Docker installation of Couchbase, see the Server Developer Tutorial. |
Prerequisites
-
Before starting this tutorial, you must have a Couchbase Capella account. If you do not have one already, Create an Account.
-
Install the Java Software Development Kit (JDK) — Couchbase JVM SDKs are compatible with version 8, 11, 17, or 21.
-
The recommended version is the latest Java LTS release, which is currently JDK 21. Ensure you install the highest available patch for the LTS version.
-
-
Install Apache Maven (version 3+)
The easiest way to install and manage Java JDKs and Maven on your machine is through SDKMan. After following the instructions to install SDKMan, open a terminal window and run the commands |
Data Model
The model consists of three record types:
student |
Information about individual students, like name and date of birth. |
course |
Courses the students can take. Includes course name, faculty, and the number of credit points associated with the course. Students can take more than one course at a time. |
enrollment |
Information related to courses the students are taking. In a relational database, this is usually a link record that creates a relationship between a student and a course. |
Relational Model
In a relational model, the database contains a list of students and a list of courses. Each student can enroll in multiple courses.
A student’s enrollment record is stored in a separate table called enrollment
, which links that record to the courses they are enrolling in.
The enrollment
table highlights a challenge with the relational model, each table is based on a fixed schema that only supports a single data object type, which means you cannot store a student in the same table as their enrollment record.
Document Model
Couchbase uses a document model that stores each record as a JSON document. The document model:
-
Contains simple scalar types and complex types, like nested records and arrays
-
Lets you store complex types without decomposing them to a second table
In this tutorial, the document model stores the list of enrollment records with the student records. Each enrollment record contains a reference to the course that it relates to.
With JSON, you can change the structure of the document without having to rebuild schemas. For example, you can add a new field to store students' email addresses without migrating existing data to a new schema.
In a document database, a student’s record and their course records can look similar to this:
{
"student-id": "000001",
"name": "Hilary Smith",
"date-of-birth": "21-12-1980",
"enrollments": [
{
"course-id": "ART-HISTORY-00003",
"date-enrolled": "07-9-2021"
},
{
"course-id": "GRAPHIC-DESIGN-00001",
"date-enrolled": "15-9-2021"
}
]
}
{
"course-id": "ART-HISTORY-00003",
"course-name": "art history",
"faculty": "fine art",
"credit-points" : 100
}
{
"course-id": "GRAPHIC-DESIGN-00001",
"course-name": "graphic design",
"faculty": "media and communication",
"credit-points" : 200
}
Hilary’s enrollment is stored in the same document as her student details, which means child information is stored with its parent. This structure lets you access and retrieve all of Hilary’s details with one search and without the need for complex table joins.
You should not store a student with their course record as it can lead to data duplication and make it difficult to maintain your data.
For example, you would need to access every single student record in your cluster to change the credit-points .
|
Create and Deploy a Cluster
Every Capella account includes one free tier operational cluster. In this tutorial, you will use the free tier cluster to create and manage student records.
To create and deploy a cluster:
-
Sign in to Couchbase Capella.
-
On the Operational Clusters page, click Create Cluster.
-
Select My First Project as the project for your cluster. (If you’ve already created a project, you can select that instead.)
-
Under Cluster Option, select Free.
-
In the Cluster Name field, enter student-cluster.
-
(Optional) Provide a description of your cluster.
-
Select one of the available cloud service providers.
-
Select an available geographic region for your cluster.
If you are unsure which cloud provider and region to choose, select the default options, for example AWS and US East. -
Accept the default CIDR Block for your cluster.
-
Click Create Cluster to deploy the cluster.
The cluster may take a few minutes to deploy. When the cluster is deployed, you can implement a data model.
Buckets, Scopes, and Collections
To organize and manage your data in Couchbase, you can create buckets, scopes, and collections inside your cluster.
A bucket is equivalent to a database in a relational database management system, while scopes and collections are used to provide separation between documents of different types.
bucket |
Stores and retrieves data in the server. |
scope |
Stores collections. When you create a new bucket, Couchbase provides you with a default scope called |
collection |
Contains a set of documents. Couchbase provides you with a default collection called |
For more information, see Buckets, Scopes, and Collections.
Create a Bucket, Scope, and Collection
To continue this tutorial, you must create a bucket to hold all student data, a scope to narrow down the data into only data related to an art school, and two collections to narrow it down further into art school students and art school courses.
To create the data model from the Capella UI:
-
On the Operational tab, select student-cluster.
-
Click the Data Tools tab.
-
In the left pane, click + Create.
-
Under Bucket, select New and enter the name
student-bucket
. Keep the default 100 MiB memory quota. -
Under Scope, enter the name
art-school-scope
. -
Under Collection, enter the name
student-record-collection
for your first collection. -
Click Create.
To create the second collection, follow the above steps but use the existing student-bucket
and art-school-scope
, and then create a collection with the name course-record-collection
.
The two collections allow you to use the relational model and the document model at the same time to achieve the best design and performance possible.
The student-record-collection
contains student records, and each student record contains a list of that student’s enrollments.
Unlike the standard relational model decomposition where a link table is created between students and courses, a document model stores the enrollments as part of the student records.
The course-record-collection
, on the other hand, uses the relational model to link the enrollment records to the course records they apply to.
This allows you to retrieve other details like the full title of the course or the number of credits students receive upon completing the course.
Scopes and Schools
For this tutorial we have a single scope, |
Connecting the Java SDK
After implementing the data model, you can set up a Couchbase SDK and connect it to your cluster to write your first application. We will be using Maven here — for more information about the Java SDK installation, see the full installation page, or review the Getting Started page.
Configure the Cluster Connection
Before you can establish a connection to your cluster, you must obtain the connection string, add an allowed IP address, and configure the cluster access credentials.
To configure the connection:
-
On the Operational Clusters page, select student-cluster.
-
Go to
. -
Make a note of the Public Connection String. You will need this to connect to the cluster.
-
Add an allowed IP address.
-
From
, click the Allowed IP Addresses link to go to the Allowed IP Addresses page. -
Click Add Allowed IP.
-
On the Add Allowed IP page, select Add Current IP Address to automatically populate your public IP address.
-
Click Add Allowed IP.
-
-
Create new cluster access credentials.
-
From
, click the Cluster Access link to go to the Cluster Access page. -
Click Create Cluster Access.
-
Enter an access name, for example
administrator
, and make a note of it for later. -
Enter a password, for example
Admin@123
, and again make a note of it for later. -
Under Bucket-Level Access, select student-bucket and art-school-scope, and set the access to Read/Write.
-
Click Create Cluster Access.
-
You can now set up a Couchbase Java SDK and connect it to your cluster.
Set Up the Java SDK
To set up the Java SDK:
-
Create the following directory structure on your computer:
📂 ~ (your home directory) 📂 student 📂 src 📂 main 📂 java
-
In the
student
directory, create a new file calledpom.xml
.📂 ~ (your home directory) 📂 student 📃 pom.xml ⬅ here! 📂 src 📂 main 📂 java
-
Paste the following code block into your
pom.xml
file:<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>couchbase-java</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>16</maven.compiler.source> <maven.compiler.target>16</maven.compiler.target> <encoding>UTF-8</encoding> <project.build.sourceEncoding>${encoding}</project.build.sourceEncoding> <project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding> <project.resources.sourceEncoding>${encoding}</project.resources.sourceEncoding> <archetype.encoding>${encoding}</archetype.encoding> </properties> <!-- The `<dependencies>` section of the code block lists all the libraries required to build your application. In the case of the student record tutorial, you only need the Couchbase client SDK and Log4J. --> <dependencies> <dependency> <groupId>com.couchbase.client</groupId> <artifactId>java-client</artifactId> <version>3.8.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.9</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>2.0.9</version> </dependency> </dependencies> </project>
-
Open a terminal window and navigate to your
student
directory. -
Run the command
mvn install
to pull in all the dependencies and finish your SDK setup.
Next, connect your Java SDK to your cluster.
Connect the SDK to Your Cluster
To connect to the cluster:
-
In your
java
directory, create a new file calledConnectStudent.java
.📂 ~ (your home directory) 📂 student 📃 pom.xml 📂 src 📂 main 📂 java 📃 ConnectStudent.java ⬅ here!
-
Paste the following code block into your
ConnectStudent.java
file:import com.couchbase.client.java.Bucket; import com.couchbase.client.java.Cluster; import com.couchbase.client.java.Collection; import com.couchbase.client.java.Scope; import com.couchbase.client.java.ClusterOptions; import java.time.Duration; public class ConnectStudent { public static void main(String[] args) { String connectionString = "<<connection-string>>"; // Replace this with Connection String String username = "<<username>>"; // Replace this with username from cluster access credentials String password = "<<password>>"; // Replace this with password from cluster access credentials //Connecting to the cluster Cluster cluster = Cluster.connect(connectionString, ClusterOptions.clusterOptions(username, password) // Use the pre-configured profile below to avoid latency issues with your connection. .environment(env -> env.applyProfile("wan-development")) ); // The `cluster.bucket` retrieves the bucket you set up for the student cluster. Bucket bucket = cluster.bucket("student-bucket"); // Most of the Couchbase APIs are non-blocking. // When you call one of them, your application carries on and continues to perform other actions while the function you called executes. // When the function has finished executing, it sends a notification to the caller and the output of the call is processed. // While this usually works, in this code sample the application carries on without waiting for the bucket retrieval to complete after you make the call to `cluster.bucket`. // This means that you now have to try to retrieve the scope from a bucket object that has not been fully initialized yet. // To fix this, you can use the `waitUntilReady` call. // This call forces the application to wait until the bucket object is ready. bucket.waitUntilReady(Duration.ofSeconds(10)); // The `bucket.scope` retrieves the `art-school-scope` from the bucket. Scope scope = bucket.scope("art-school-scope"); // The `scope.collection` retrieves the student collection from the scope. Collection student_records = scope.collection("student-record-collection"); // A check to make sure the collection is connected and retrieved when you run the application. // You can see the output using maven. System.out.println("The name of this collection is " + student_records.name()); // Like with all database systems, it's good practice to disconnect from the Couchbase cluster after you have finished working with it. cluster.disconnect(); } }
-
In the
ConnectStudent.java
file, replace the<<connection-string>>
,<<username>>
, and<<password>>
placeholders with the connection string, username, and password that you noted when configuring the cluster connection.You must re-run
mvn install
in yourstudent
directory whenever you make a change to a java file to rebuild your application. -
Open a terminal window and navigate to your
student
directory. -
Run the command
mvn install
to pull in all the dependencies and rebuild your application. -
Run the following command to check that the connection works:
mvn exec:java -Dexec.mainClass="ConnectStudent" -Dexec.cleanupDaemonThreads=false
If the connection is successful, the collection name outputs in the console log.
If you come across errors in your console, see the troubleshooting section.
Create a Student Record
After connecting to the cluster, you can create a student record in the form of a JSON document inserted into the student-record-collection
.
To create a student record:
-
In your
java
directory, create a new file calledInsertStudent.java
. -
Paste the following code block into your
InsertStudent.java
file:import com.couchbase.client.java.Bucket; import com.couchbase.client.java.Cluster; import com.couchbase.client.java.Collection; import com.couchbase.client.java.Scope; import com.couchbase.client.java.json.JsonObject; import com.couchbase.client.java.ClusterOptions; import java.time.Duration; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class InsertStudent { public static void main(String[] args) { String connectionString = "<<connection-string>>"; // Replace this with Connection String String username = "<<username>>"; // Replace this with username from cluster access credentials String password = "<<password>>"; // Replace this with password from cluster access credentials Cluster cluster = Cluster.connect(connectionString, ClusterOptions.clusterOptions(username, password) .environment(env -> env.applyProfile("wan-development")) ); Bucket bucket = cluster.bucket("student-bucket"); bucket.waitUntilReady(Duration.ofSeconds(10)); Scope scope = bucket.scope("art-school-scope"); Collection student_records = scope.collection("student-record-collection"); // This `JsonObject` class creates and populates the student record. JsonObject hilary = JsonObject.create() .put("name", "Hilary Smith") .put("date-of-birth", LocalDate.of(1980, 12, 21) .format(DateTimeFormatter.ISO_DATE)); // The `upsert` function inserts or updates documents in a collection. // The first parameter is a unique ID for the document, similar to a primary key used in a relational database system. // If the `upsert` call finds a document with a matching ID in the collection, it updates the document. // If there is no matching ID, it creates a new document. student_records.upsert("000001", hilary); cluster.disconnect(); } }
-
In the
InsertStudent.java
file, replace the<<connection-string>>
,<<username>>
, and<<password>>
placeholders with the connection string, username, and password that you noted when configuring the cluster connection. -
Open a terminal window and navigate to your
student
directory. -
Run the command
mvn install
to pull in all the dependencies and rebuild your application. -
Run the following command to insert the student record into the collection:
mvn exec:java -Dexec.mainClass="InsertStudent" -Dexec.cleanupDaemonThreads=false
-
From the Capella UI, go to your student cluster and check the
student-record-collection
for the new student record you just added:-
Go to
. -
Under Get documents from, select
student-bucket
,art-school-scope
, andstudent-record-collection
. -
Click Get Documents.
-
Create Course Records
Creating course records is similar to creating student records. To create course records:
-
In your
java
directory, create a new file calledInsertCourses.java
. -
Paste the following code block into your
InsertCourses.java
file:import com.couchbase.client.java.Bucket; import com.couchbase.client.java.Cluster; import com.couchbase.client.java.Collection; import com.couchbase.client.java.Scope; import com.couchbase.client.java.json.JsonObject; import com.couchbase.client.java.ClusterOptions; import java.time.Duration; public class InsertCourses { public static void main(String[] args) { String connectionString = "<<connection-string>>"; // Replace this with Connection String String username = "<<username>>"; // Replace this with username from cluster access credentials String password = "<<password>>"; // Replace this with password from cluster access credentials Cluster cluster = Cluster.connect(connectionString, ClusterOptions.clusterOptions(username, password) .environment(env -> env.applyProfile("wan-development")) ); Bucket bucket = cluster.bucket("student-bucket"); bucket.waitUntilReady(Duration.ofSeconds(10)); Scope scope = bucket.scope("art-school-scope"); // The code here is similar to creating a student record, but it writes to a different collection. Collection course_records = scope.collection("course-record-collection"); addCourse(course_records, "ART-HISTORY-000001", "art history", "fine art", 100); addCourse(course_records, "FINE-ART-000002", "fine art", "fine art", 50); addCourse(course_records, "GRAPHIC-DESIGN-000003", "graphic design", "media and communication", 200); cluster.disconnect(); } private static void addCourse(Collection collection, String id, String name, String faculty, int creditPoints) { JsonObject course = JsonObject.create() .put("course-name", name) .put("faculty", faculty) .put("credit-points", creditPoints); collection.upsert(id, course); } }
-
In the
InsertCourses.java
file, replace the<<connection-string>>
,<<username>>
, and<<password>>
placeholders with the connection string, username, and password that you noted when configuring the cluster connection. -
Open a terminal window and navigate to your
student
directory. -
Run the command
mvn install
to pull in all the dependencies and rebuild your application. -
Run the following command to insert the student record into the collection:
mvn exec:java -Dexec.mainClass="InsertCourses" -Dexec.cleanupDaemonThreads=false
-
From the Capella UI, go to your student cluster and check the
course-record-collection
for the new course records you just added.
If you come across errors in your console, see the troubleshooting section.
Retrieve Records
You can retrieve your records using the query editor or the SDK.
Retrieving Records with the Query Editor
To retrieve the records with the Query Editor, you must first define an index.
Define an Index
Before you can retrieve your records with the query editor, you must first define an index in your bucket. The index helps your cluster find specific data when you run a query.
To define an index:
-
On the Operational Clusters page, select student-cluster.
-
Go to
. -
Select student-bucket and art-school-scope in the Context drop-down.
Using these filters, you can narrow down the scope of your queries. You do not need to add the names of your bucket and scope to your queries.
-
Enter the following query into your query editor:
CREATE PRIMARY INDEX course_idx ON `course-record-collection`
-
Click Run to create a single index called
course_idx
in yourcourse-record-collection
.
Retrieve Your Records
You can use the Query Editor to retrieve all course records at once, or narrow your search down to retrieve records based on specific criteria.
Retrieve All Course Records
To retrieve all of your course records using the query editor:
-
Enter the following query into your query editor:
SELECT crc.* FROM `course-record-collection` crc
-
Click Run to retrieve all course records.
[ { "course-name": "art history", "credit-points": 100, "faculty": "fine art" }, { "course-name": "fine art", "credit-points": 50, "faculty": "fine art" }, { "course-name": "graphic design", "credit-points": 200, "faculty": "media and communication" } ]
Retrieve Course Records with Less than 200 Credits
You can expand your query to narrow your search down further.
To retrieve only courses with less than 200 credit-points
using the query editor:
-
Enter the following query into your query editor:
SELECT crc.* FROM `course-record-collection` crc WHERE crc.`credit-points` < 200
-
Click Run to retrieve all courses with less than 200 credits.
[ { "course-name": "art history", "credit-points": 100, "faculty": "fine art" }, { "course-name": "fine art", "credit-points": 50, "faculty": "fine art" } ]
Retrieve Record IDs
The id
field is not automatically returned when you retrieve all of your course information.
The id
is part of a document’s meta structure, and to retrieve it you must adjust your SQL++ query and run it again:
-
Enter the following query into your query editor:
SELECT META().id, crc.* FROM `course-record-collection` crc WHERE crc.`credit-points` < 200
The
META()
function call returns any property contained inside the document’s metadata, including the ID. -
Click Run to retrieve course records and their IDs.
[ { "course-name": "art history", "credit-points": 100, "faculty": "fine art", "id": "ART-HISTORY-000001" }, { "course-name": "fine art", "credit-points": 50, "faculty": "fine art", "id": "FINE-ART-000002" } ]
Retrieving Records with the SDK
You can also use SQL++ queries to retrieve your records with the SDK. Unlike the query editor, you must include the name of the bucket and the scope to fully qualify the name of the collection in the SQL++ statement in your application. For example:
SELECT crc.* FROM `student-bucket`.`art-school-scope`.`course-record-collection` crc
Retrieve Your Records
You can use the SDK to retrieve all course records at once, or narrow your search down to retrieve records based on specific criteria.
Retrieve All Course Records
To retrieve all of your course records using the Java SDK:
-
In your
java
directory, create a new file calledArtSchoolRetriever.java
. -
Paste the following code block into your
ArtSchoolRetriever.java
file:import com.couchbase.client.core.error.CouchbaseException; import com.couchbase.client.java.Cluster; import com.couchbase.client.java.json.JsonObject; import com.couchbase.client.java.query.QueryResult; import com.couchbase.client.java.ClusterOptions; public class ArtSchoolRetriever { public static void main(String[] args) { String connectionString = "<<connection-string>>"; // Replace this with Connection String String username = "<<username>>"; // Replace this with username from cluster access credentials String password = "<<password>>"; // Replace this with password from cluster access credentials Cluster cluster = Cluster.connect(connectionString, ClusterOptions.clusterOptions(username, password) .environment(env -> env.applyProfile("wan-development")) ); retrieveCourses(cluster); cluster.disconnect(); } private static void retrieveCourses(Cluster cluster) { try { final QueryResult result = cluster.query("select crc.* from `student-bucket`.`art-school-scope`.`course-record-collection` crc"); for (JsonObject row : result.rowsAsObject()) { System.out.println("Found row: " + row); } } catch (CouchbaseException ex) { ex.printStackTrace(); } } }
-
In the
ArtSchoolRetriever.java
file, replace the<<connection-string>>
,<<username>>
, and<<password>>
placeholders with the connection string, username, and password that you noted when configuring the cluster connection. -
Open a terminal window and navigate to your
student
directory. -
Run the command
mvn install
to pull in all the dependencies and rebuild your application. -
Run the following command to retrieve all course records:
mvn exec:java -Dexec.mainClass="ArtSchoolRetriever" -Dexec.cleanupDaemonThreads=false
If the retrieval is successful, the course information outputs in the console log.
Retrieve Course Records with Less than 200 Credits
You can set parameters in your code to narrow your search down further.
To retrieve only courses with less than 200 credit-points
using the Java SDK:
-
In your
java
directory, create a new file calledArtSchoolRetrieverParameters.java
. -
Paste the following code block into your
ArtSchoolRetrieverParameters.java
file:import com.couchbase.client.core.error.CouchbaseException; import com.couchbase.client.java.Cluster; import com.couchbase.client.java.json.JsonObject; import com.couchbase.client.java.query.QueryOptions; import com.couchbase.client.java.query.QueryResult; import com.couchbase.client.java.ClusterOptions; public class ArtSchoolRetrieverParameters { public static void main(String[] args) { String connectionString = "<<connection-string>>"; // Replace this with Connection String String username = "<<username>>"; // Replace this with username from cluster access credentials String password = "<<password>>"; // Replace this with password from cluster access credentials Cluster cluster = Cluster.connect(connectionString, ClusterOptions.clusterOptions(username, password) .environment(env -> env.applyProfile("wan-development")) ); retrieveCourses(cluster); cluster.disconnect(); } private static void retrieveCourses(Cluster cluster) { try { // This SQL++ statement takes the parameter `$creditPopints`, // which is then substituted by the value in the second parameter when the statement is called. final QueryResult result = cluster.query("select crc.* " + "from `student-bucket`.`art-school-scope`.`course-record-collection` crc " + "where crc.`credit-points` < $creditPoints", // The second parameter in the function call, with a value that replaces `$creditPoints`. QueryOptions.queryOptions() .parameters(JsonObject.create().put("creditPoints", 200))); for (JsonObject row : result.rowsAsObject()) { System.out.println("Found row: " + row); } } catch (CouchbaseException ex) { ex.printStackTrace(); } } }
-
In the
ArtSchoolRetrieverParameters.java
file, replace the<<connection-string>>
,<<username>>
, and<<password>>
placeholders with the connection string, username, and password that you noted when configuring the cluster connection. -
Open a terminal window and navigate to your
student
directory. -
Run the command
mvn install
to pull in all the dependencies and rebuild your application. -
Run the following command to retrieve all course records:
mvn exec:java -Dexec.mainClass="ArtSchoolRetrieverParameters" -Dexec.cleanupDaemonThreads=false
If the retrieval is successful, the course information with your parameters outputs in the console log.
If you come across errors in your console, see the troubleshooting section.
Add Course Enrollments
After retrieving student and course records, you can add enrollment details to the student records using the SDK.
Add Enrollment Details
To add enrollment details to a student record:
-
In your
java
directory, create a new file calledAddEnrollments.java
. -
Paste the following code block into your
AddEnrollments.java
file:import com.couchbase.client.core.error.CouchbaseException; import com.couchbase.client.java.Bucket; import com.couchbase.client.java.Cluster; import com.couchbase.client.java.Collection; import com.couchbase.client.java.Scope; import com.couchbase.client.java.json.JsonArray; import com.couchbase.client.java.json.JsonObject; import com.couchbase.client.java.query.QueryOptions; import com.couchbase.client.java.query.QueryResult; import com.couchbase.client.java.query.QueryScanConsistency; import com.couchbase.client.java.ClusterOptions; import java.time.Duration; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class AddEnrollments { public static void main(String[] args) { String connectionString = "<<connection-string>>"; // Replace this with Connection String String username = "<<username>>"; // Replace this with username from cluster access credentials String password = "<<password>>"; // Replace this with password from cluster access credentials Cluster cluster = Cluster.connect(connectionString, ClusterOptions.clusterOptions(username, password) .environment(env -> env.applyProfile("wan-development")) ); // Retrieves the student bucket you set up. Bucket bucket = cluster.bucket("student-bucket"); // Forces the application to wait until the bucket is ready. bucket.waitUntilReady(Duration.ofSeconds(10)); // Retrieves the `art-school-scope` collection from the scope. Scope scope = bucket.scope("art-school-scope"); Collection student_records = scope.collection("student-record-collection"); // Retrieves Hilary's student record, the `graphic design` course record, and the `art history` course record. // Each method uses a SQL++ call to retrieve a single record from each collection. JsonObject hilary = retrieveStudent(cluster,"Hilary Smith"); JsonObject graphic_design = retrieveCourse(cluster, "graphic design"); JsonObject art_history = retrieveCourse(cluster, "art history"); // Couchbase does not have a native date type, so the common practice is to store dates as strings. String currentDate = LocalDate.now().format(DateTimeFormatter.ISO_DATE); // Stores the `enrollments` inside the student record as an array. // `JsonArray.create()` creates an empty list structure. JsonArray enrollments = JsonArray.create(); // Adds two JSON elements to the `enrollments` array: the course that the enrollment relates to, and the date that the student enrolled in the course. // To avoid repeating data all over the cluster, you store a reference to the course instead of the entire course record itself in this field. // This means that you do not have to search through every single record if the course details change. enrollments.add(JsonObject.create() .put("course-id", graphic_design.getString("id")) .put("date-enrolled", currentDate)); enrollments.add(JsonObject.create() .put("course-id", art_history.getString("id")) .put("date-enrolled", currentDate)); // Adds the `enrollments` array to Hilary's student record. hilary.put("enrollments", enrollments); // Commits the changes to the collection. // The `upsert` function call takes the key of the record you want to insert or update and the record itself as parameters. // If the `upsert` call finds a document with a matching ID in the collection, it updates the document. // If there is no matching ID, it creates a new document. student_records.upsert(hilary.getString("id"), hilary); cluster.disconnect(); } private static JsonObject retrieveStudent(Cluster cluster, String name) throws CouchbaseException { QueryOptions studentQueryOptions = QueryOptions.queryOptions(); studentQueryOptions.parameters(JsonObject.create().put("name", name)); studentQueryOptions.scanConsistency(QueryScanConsistency.REQUEST_PLUS); final QueryResult result = cluster.query("select META().id, src.* " + "from `student-bucket`.`art-school-scope`.`student-record-collection` src " + "where src.`name` = $name", studentQueryOptions); return result.rowsAsObject().get(0); } private static JsonObject retrieveCourse(Cluster cluster, String course) throws CouchbaseException { QueryOptions courseQueryOptions = QueryOptions.queryOptions(); courseQueryOptions.parameters(JsonObject.create().put("courseName", course)); courseQueryOptions.scanConsistency(QueryScanConsistency.REQUEST_PLUS); final QueryResult result = cluster.query("select META().id, crc.* " + "from `student-bucket`.`art-school-scope`.`course-record-collection` crc " + "where crc.`course-name` = $courseName", courseQueryOptions); return result.rowsAsObject().get(0); } }
Because this is a tutorial, you do not need to add an error check to make sure that your collection has returned an item. In a live application, error checks must be made to prevent errors and keep the application running. -
In the
AddEnrollments.java
file, replace the<<connection-string>>
,<<username>>
, and<<password>>
placeholders with the connection string, username, and password that you noted when configuring the cluster connection. -
Open a terminal window and navigate to your
student
directory. -
Run the command
mvn install
to pull in all the dependencies and rebuild your application. -
Run the following command to insert the student record into the collection:
mvn exec:java -Dexec.mainClass="AddEnrollments" -Dexec.cleanupDaemonThreads=false
-
From the Capella UI, go to your student cluster.
-
Go to the
student-record-collection
and click the document ID to see the new information you just added to Hilary’s student record.
If you come across errors in your console, see the troubleshooting section.
Next Steps
Now that you have finished following the Student Record System tutorial, you can explore more of Capella Operational by checking out the rest of the developer documentation, or look at working with the Data Service or SQL++ Queries from the SDK.
Troubleshooting
Below are some tutorial-specific tips for troubleshooting — you can find more detailed troubleshooting for connecting to Capella in our Troubleshooting Cloud Connections page.
Authentication Error
If you get an authentication error when running Maven commands in your console, confirm that the username and password in your Java file matches the username and password you used when setting up the Capella Operational cluster in your browser.
If they do not match, you can edit the Java file to add the correct username or password and try compiling and running the Maven command again.