Retrieve Records

  • Capella Operational
  • tutorial
    +
    Retrieve records or documents from your collections using SQL++, Couchbase’s SQL-based query language. You can retrieve your records using the query editor or the SDK.

    Prerequisites

    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:

    1. On the Operational Clusters page, click the name of your free tier operational cluster.

    2. Go to Data Tools  Query.

    3. Set your bucket and scope context to the student-bucket and art-school-scope.

    4. Enter the following query into your query editor:

      create primary index course_idx on `course-record-collection`
    5. Click Run to create a single index called course_idx in your course-record-collection.

    Retrieve Your Records

    You can use the query editor to retrieve all course records at once, or narrow your search to retrieve records based on specific criteria.

    Retrieve All Course Records

    To retrieve all of your course records:

    1. Enter the following query into your query editor:

      select crc.* from `course-record-collection` crc
    2. 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 further. To retrieve only courses with less than 200 credit-points:

    1. Enter the following query into your query editor:

      select crc.* from `course-record-collection` crc where crc.`credit-points` < 200
    2. 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:

    1. 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.

    2. 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"
        }
      ]

    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 to retrieve records based on specific criteria.

    Retrieve All Course Records

    To retrieve all of your course records using the Java SDK:

    1. In your java directory, create a new file called ArtSchoolRetriever.java.

    2. 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();
              }
          }
      }
    3. In the ArtSchoolRetriever.java file, replace the <<connection-string>>, <<username>>, and <<password>> placeholders with your cluster’s public connection string, and the username and password from your cluster access credentials.

    4. Open a terminal window and navigate to your student directory.

    5. Run the command mvn install to pull in all the dependencies and rebuild your application.

    6. 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 further. To retrieve only courses with less than 200 credit-points using the Java SDK:

    1. In your java directory, create a new file called ArtSchoolRetrieverParameters.java.

    2. 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();
              }
          }
      }
    3. In the ArtSchoolRetrieverParameters.java file, replace the <<connection-string>>, <<username>>, and <<password>> placeholders with your cluster’s public connection string, and the username and password from your cluster access credentials.

    4. Open a terminal window and navigate to your student directory.

    5. Run the command mvn install to pull in all the dependencies and rebuild your application.

    6. 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 Troubleshooting the Developer Tutorial.

    Next Steps

    After retrieving student and course records, you can add enrollment details to the student records using the SDK.