Create Student and Course Records
- Capella Operational
- tutorial
Learn how to use the SDK to create student and course records.
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:
shmvn 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:
shmvn 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 page.