Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AV-68634] Capella Dev Tutorial - Add Course Records and Other Docs #322

Open
wants to merge 4 commits into
base: AV-68634-tutorial-feature-branch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions modules/tutorials/examples/AddEnrollments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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 endpoint = "<<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(endpoint, 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);

}
}
48 changes: 48 additions & 0 deletions modules/tutorials/examples/ArtSchoolRetriever.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 endpoint = "<<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(endpoint, 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();
}
}
}
37 changes: 37 additions & 0 deletions modules/tutorials/examples/ArtSchoolRetrieverAll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 endpoint = "<<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(endpoint, 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();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 0 additions & 140 deletions modules/tutorials/pages/java-tutorial/.adding-course-enrollments.adoc

This file was deleted.

Loading