Professional Documents
Culture Documents
2
Exercise 3. IBM Cloud with IBM Cloudant
EXempty
Overview
NoSQL databases are built from the ground up to scale globally, run non-stop, and handle a wide
variety of data types, such as JavaScript Object Notation (JSON), full-text, and geospatial.
Cloudant is a NoSQL database that is optimized for handling heavy workloads of concurrent reads
and writes in the cloud. These workloads are typical of large, fast-growing web, and mobile
applications (apps).
This exercise shows you how to create an instance of a Cloudant service on IBM Cloud. You use
your Cloudant service instance to discover various features of the Cloudant service and understand
the different methods that are available to use the HTTP APIs by using IBM Cloud Identity and
Access Management. You also learn how to use Cloudant HTTP APIs to apply create, read,
update, and delete operations by creating indexes and by using Cloudant Query on Cloudant
documents.
Objectives
After completing this exercise, you should be able to perform the following tasks:
• Create an instance of the Cloudant service on IBM Cloud.
• Create service credentials by using IBM Cloud Identity and Access Management (IAM).
• Access the Cloudant documentation.
• Explore the features of the Cloudant dashboard.
• Create, read, update, and delete Cloudant documents by using HTTP APIs.
• Verify the data that is stored in the database by using the Cloudant dashboard.
• Create indexes and query Cloudant documents by using HTTP APIs.
Introduction
Cloudant is built on Apache CouchDB and contributes to the open source project.
IBM Cloudant is a NoSQL database as a service (DBaaS) that frees developers from worrying
about managing the database and enables them to focus on the application.
Cloudant is designed to ensure that the flow of data between an application and its database
remains uninterrupted and performs to users’ satisfaction. Cloudant can run across many servers in
EXempty
a database cluster, resulting in high availability (HA) and fault tolerance. The data replication
technology also enables developers to put data closer to where their applications need it most.
Your app’s data persistence layer can be durable and highly available with IBM Cloudant. Your data
is securely hosted and globally managed by big data experts 24x7.
This exercise demonstrates how you can create a Cloudant database service on IBM Cloud without
installing or configuring the database instance on your workstation. You use an HTTP API client
such as Postman to create, read, update, and delete Cloudant documents. You learn how to create
indexes and query data by using Cloudant API endpoints.
Requirements
This exercise requires:
• Access to the internet and the IBM Cloud console from your workstation by using one of the
modern web browsers with the current version (such as Chrome, Firefox, or Safari).
• An IBM Cloud account (Lite, Pay-As-You-Go, or Subscription).
• Postman.
• IBM Cloud CLI that is installed in your workstation (optional).
• Any text editor, such as Notepad, Notepad++, VS code, or Sublime (optional).
EXempty
Exercise instructions
In this exercise you will complete the following tasks:
__ 1. Log in to IBM Cloud.
__ 2. Create an instance of the Cloudant service.
__ 3. Explore the Cloudant service on IBM Cloud.
__ 4. Create credentials for a Cloudant service instance.
__ 5. Explore the features of the Cloudant dashboard.
__ 6. Use an HTTP API client to access Cloudant.
__ 7. Delete your Cloudant service instance.
EXempty
Note
If your company uses the single sign-on service, you provide only your IBMid, and then you are
redirected to the single sign-on page to log in by using your company credentials.
__ 2. You are redirected to the IBM Cloud dashboard page, as shown in the following figure.
__ 2. You can now see the entire catalog. In the search field, type Cloudant.
__ 3. Select the Cloudant service, as shown in the following figure.
EXempty
__ b. Accept the default values for service name and resource group.
__ c. In Available authentication methods, select Use only IAM, as shown in the following
figure.
EXempty
Note
The Cloudant service has two authentication methods that are available: Use both legacy
credentials and IAM, which means that both IAM and Legacy credentials can be used to access
the account, and Use only IAM, which means that only IAM credentials are provided through
service binding and credential generation.
• Legacy credentials enable login to Cloudant by using HTTP Basic authentication by providing a
user name and password that is used for authentication.
• Identity and Access Management (IAM) provides a unified approach to managing user
identities, services, and access control. IAM authentication requires that an IAM API key is
exchanged for a time-limited access token before you can make a request to Cloudant. When
the access token expires, the client must obtain a new one from the IAM token service.
In this exercise, you use the IAM method as the authentication method for the Cloudant service.
EXempty
__ 5. After the service is created, you are redirected to the Resource list window, as shown in the
following figure. Open the Cloudant service instance overview by clicking the newly created
service in the Resource list window.
EXempty
Note
After you create the service, it takes a few minutes to be provisioned, as shown in the following
figure. The status is “Provision in progress” for a while. If it takes too long for the status to change to
“Provisioned”, refresh the browser.
EXempty
__ 2. After you open the Cloudant service instance, you are redirected to the overview page of the
service. In the left pane are the Manage, Service credentials, Plan, and Connections
menu options, as shown in the following figure.
__ 3. The Manage section has four tabs: Overview, Dashboard, Capacity, and Docs, which are in
the right pane. The Manage section also includes the Launch Cloudant Dashboard
button, which opens the Cloudant dashboard.
__ a. The Overview tab (shown in the following figure) shows data about the current service,
such as the deployment details, which include the location, endpoint, and authentication
method that is used. It also shows capacity details, which include the current plan and
the available storage. It also includes a section for important announcements that are
related to Cloudant, such as showing new features and updates.
EXempty
EXempty
__ b. The Dashboard tab (shown in the following figure) includes quick access to specific
sections in the Cloudant dashboard:
Databases, where you can create, update, and delete databases.
Replication, where you can create or edit existing replication jobs.
Active tasks, where you can see indexing, replication, and compaction tasks that are
actively running on your Cloudant instance.
Monitoring, where you can see the current consumption of provisioned throughput
capacity and data storage that is being used by your applications.
EXempty
__ c. The Capacity tab shows the current capacity that is available for your application and
the current plan that is used, as shown in the following figure.
__ d. The Docs tab includes a direct link for Cloudant documentation, some tutorials about
Cloudant, and open source libraries for connecting with Cloudant for all major
development languages, as shown in the following figure.
EXempty
__ e. In the Docs tab, select Learning Center or alternatively, from another browser tab, go to
the following address:
https://developer.ibm.com/clouddataservices/docs/cloudant/
__ f. In the IBM Cloudant Learning Center window, click Load Data, as shown in the
following figure.
EXempty
You can also get to this page by going to the following address:
https://developer.ibm.com/clouddataservices/docs/cloudant/load-data/
__ g. Read the entire document, including the tutorials, and watch all the videos in this page.
You do not need to do the tutorials. When you finish reading this document, close the
browser tab for IBM Cloudant dashboard.
__ 4. In the left pane of the Cloudant service overview page in IBM Cloud, explore the other
sections:
Service credentials, where you can create a set of credentials so that an external
consumer can use the Cloudant Rest APIs, as shown in the following figure.
Plan, where you can upgrade your current plan, as shown in the following figure.
EXempty
Connections, where you can connect the service with any existing Cloud Foundry
applications on the same organization.
__ 2. In the Service credentials page, click New credential + as shown in the following figure.
EXempty
__ 3. Keep the default values that are shown in the window and click Add, as shown in the
following figure.
EXempty
Note
__ 4. After the set of credentials is created successfully, open the new service credentials by
clicking View credentials, as shown in the following figure.
__ 5. Click the Copy to clipboard icon on the right to copy the credentials data that is kept in
JSON format, as shown in the following figure. Then, create a file on your favorite text
editor, paste the credentials into the newly created file, save it in your local workstation, and
name it cloudant_credentials.json.
EXempty
EXempty
__ 3. The Cloudant dashboard opens in a new browser tab, as shown in the following figure.
EXempty
__ 4. Create a database and name it novels by completing the following steps:
__ a. Click Create Database on the top bar, as shown in the following figure.
__ b. Name the database novels, select Non-partitioned, and then click Create, as shown in
the following figure.
Note
While you are creating a database, there is the option to make the database partitioned. A
partitioned database is a newer type of IBM Cloudant database. This option is an advanced feature,
so it is out of the scope of this exercise. For more information about this topic, see the following link:
https://cloud.ibm.com/docs/services/Cloudant/guides?topic=cloudant-database-partitioning#databa
se-partitioning
__ c. After you create the database, you are redirected to the new database overview, as
shown in the following figure.
EXempty
__ 5. Create a document for the “Oliver Twist” novel by completing the following steps:
__ a. Click Create Document, as shown in the following figure.
__ b. You are redirected to the document editor page, which contains by default a JSON
object with an auto-generated _id, as shown in the following figure.
Note
The _id field is automatically generated by Cloudant because it is considered the unique key
identifier for the document. You can override this key by providing a _id with a unique value across
the other documents.
EXempty
__ c. Replace the JSON object in the document editor with the following JSON object:
{
"_id": "novel_001",
"name": "Oliver Twist",
"author": "Charles Dickens",
"year": 1839
}
__ f. To show the full data of the newly created document in JSON format, select { } JSON
from the top tabs, as shown in the following figure.
EXempty
Information
After you create the document, you find a field that is called _rev that is automatically generated by
Cloudant. The _rev field is used internally by the Cloudant database as a revision number. A
revision number is added to your documents by the server when you insert or modify documents.
You must specify the latest _rev when you update a document or your request fails and returns a
409 error. This field helps you avoid conflicting data states. The revision number is also used to
confirm that a client is trying to modify the current version of a document.
EXempty
Note
You can also create a document by clicking the three dots vertical menu icon and selecting + New
Doc, as shown in the following figure.
Another option to create a document is to click the + icon next to All Documents in the left
pane then select New Doc + as shown in the following figure.
EXempty
__ 6. Repeat the steps to create a new document three times by using the following JSON
objects to add documents for “King Solomon's Mines”, “Treasure Island”, and “The Merry
Adventures of Robin Hood” novels:
{
"_id": "novel_002",
"name": "King Solomon's Mines",
"author": "H. Rider Haggard",
"year": 1885
}
{
"_id": "novel_003",
"name": "Treasure Island",
"author": "Robert Louis Stevenson",
"year": 1883
}
{
"_id": "novel_004",
"name": "The Merry Adventures of Robin Hood",
"author": "Howard Pyle",
"year": 1883
}
__ 7. Update the document of “Oliver Twist” novel by completing the following steps:
__ a. Return to the metadata view by selecting the Metadata tab from the top tabs, as shown
in the following figure.
__ b. Select the document where _id corresponds to novel_001 from the list of documents,
as shown in the following figure.
EXempty
__ c. The document editor opens. Change the name field to David Copperfield and the
year to 1850, as shown in the following figure.
Important
When you create a document, the _rev field that is generated by Cloudant always starts with 1-,
which indicates that this is the first version of this document. This number increments by 1 with
each document update to highlight the document version number. The first digit in the revision
number increments after the document update, as shown in the following figure.
__ 8. Delete the document for the “The Merry Adventures of Robin Hood” novel by completing the
following steps:
__ a. From the novels database overview page, select the document where _id corresponds
to novel_004 from the list of documents, as shown in the following figure.
EXempty
__ b. Click Delete from the top bar, as shown in the following figure.
EXempty
__ 9. Create a query to get the documents of the novels that are published after 1870 by
completing the following steps:
__ a. Select Query from left pane, as shown in the following figure.
__ b. You are redirected to the query editor page, as shown in the following figure.
EXempty
__ c. Replace the JSON object in the editor with the following code:
{
"selector": {
"year": {
"$gt": 1870
}
},
"fields": [
"_id",
"_rev",
"name",
"author",
"year"
]
}
__ d. Click Run Query, as shown in the following figure.
__ e. The results are populated in the right pane, as shown in the following figure.
EXempty
EXempty
Note
If a pop-up window opens when you open Postman, as shown in the following figure, close it and
proceed with the steps normally.
__ 2. You use the Cloudant API key to get a time-limited access token that is used in all the
upcoming requests to authenticate and authorize your access to Cloudant. After this token
expires, you cannot use it anymore. To retrieve this access token, complete the following
steps.
__ a. Select the POST method from the METHOD menu, as shown in the following figure.
__ b. Enter the following request URL into the POST field, as shown in the following figure:
https://iam.cloud.ibm.com/identity/token
EXempty
__ c. Select the Body tab from the bar below the Post menu URL field, as shown in the
following figure.
__ d. Select x-www-form-urlencoded and then add the following keys and values, as shown
in the following figure:
○ Key: grant_type and value: urn:ibm:params:oauth:grant-type:apikey
○ Key: response_type and value: cloud_iam
○ Key: apikey, and the value for this field must include your Cloudant instance API
key, which you can retrieve from the cloudant_credentials.json file. This file
includes the Cloudant credentials, and you already saved it in your workstation in a
previous step.
EXempty
__ e. Click Send, and you receive a JSON object response that includes the access token,
which is valid for 60 minutes, as shown in the following figure.
__ f. Copy the value of the access_token because it is used in all the upcoming requests.
EXempty
Important
If the access_token expires while you make any of the upcoming requests, repeat step 2 to
retrieve a new access token and use it instead of the old one.
Troubleshooting
If you receive the error in the response that is shown in the following figure, verify that you provided
the correct apikey.
You can check the credentials of the service again from the service overview page on IBM
Cloud, as shown in the following figure.
EXempty
Information
There is another way to retrieve an access token by using the IBM Cloud CLI as follows:
__ 1. Open the Command Prompt and log in to the IBM Cloud with the CLI where username is the
IBM ID (email) that you use to log in to IBM Cloud), password is your password and region
is the region that corresponds to your location.
ibmcloud login -u username -p password –r region
The following list shows the list of regions and locations
o Sydney: au-syd
o Frankfurt: eu-de
o London: eu-gb
o Dallas: us-south
o Washington DC: us-east
▪ To verify your Cloudant service region, you can check the location from the overview page
on the IBM Cloud page, as shown in the following figure.
EXempty
__ 2. Run the following command to get the access token that you can use in the Cloudant HTTP
requests:
ibmcloud iam oauth-tokens
__ 4. To view all the documents in a database, issue a GET request to the following URL:
$URL/$DATABASE/_all_docs?include_docs=true
To show all the documents in the novels database, complete the following steps:
__ a. Open a new tab in Postman by clicking + in the top bar, as shown in the following figure.
EXempty
__ d. Select the Headers tab and add the following key and value, as shown in the following
figure:
Key: Authorization.
Value: Bearer $ACCESS_TOKEN. Replace $ACCESS_TOKEN with the access token that you
obtained in the previous step Step 2).
__ e. Click Send. All three documents are displayed, as shown in the following figure.
EXempty
__ 5. To create a document, send a POST request to $URL/$DATABASE with the document's JSON
content in the request body. To create a new document for “The Merry Adventures of Robin
Hood” novel, complete the following steps, as shown in the following figure:
__ a. Update the request URL to $URL/$DATABASE.
__ b. Update the HTTP method to POST.
__ c. To specify the content of the document that you want to create, click the Body tab from
the tabs bar below the request URL, select the raw radio button, and then select the
type as JSON (application/json), as shown in the following figure.
EXempty
Information
The _id should be a unique identifier for the document. If the _id is not provided, Cloudant
generates an ID.
__ e. Click Send.
__ f. Check the response that shows the _id and _rev fields to verify the creation of the
document, as shown in the following figure.
EXempty
__ 6. Create another document for the “Oliver Twist” novel by completing the following steps:
__ a. Update the content of the body as follows. The results are shown in the following figure.
{
"_id": "novel_005",
"name": "Oliver Twist",
"author": "Charles Dickens",
"year": 2005
}
__ b. Click Send.
__ c. Check the response that shows the _id and _rev fields to verify the creation of the new
document, as shown in the following figure.
__ 7. Create another document for the “Ivanhoe” novel by completing the following steps:
__ a. Update the content of the body as follows. The results are shown in the following figure.
{
"_id": "novel_006",
"name": "Ivanhoe",
"author": "Walter Scott",
"year": 1820
}
EXempty
__ b. Click Send.
__ c. Check the response that shows the _id and _rev fields to verify the creation of the new
document, as shown in the following figure.
__ d. Click Send.
__ e. Check the response that is shown in the following figure and copy the _rev value
because it is required for updating the document in the next step.
EXempty
__ c. Update the request body with the following code, as shown in the following figure.
Replace $REV with the _rev value that you copied in the previous step.
{
"_rev": "$REV",
"name": "Oliver Twist",
"author": "Charles Dickens",
"year": 1839
}
__ d. Click Send. In the response, notice that the value of _rev is updated (Send response),
as shown in the following figure.
EXempty
__ d. Click Send.
__ e. Check the response as shown in the following figure and copy the _rev value because it
is required for deleting the document in the next step.
EXempty
__ d. Update the HTTP method to DELETE.
__ e. Click Send.
__ f. Check the response that is shown in the following figure to verify the deletion of the
document.
Troubleshooting
If you receive a conflict error in the response, as shown in the following figure, consider these
reasons:
• Document creation: the error indicates that the _id that is used to create this document is not
unique and belongs to another document in the database
• Document update or deletion: either the _id or the _rev that are used in the request are
wrong.
__ 12. In the next steps, you query the documents of the novels that are published after the year
1880 and sort them by year in ascending order. To achieve this query, create an index for
the year field that you apply to the query in the next step.
__ 13. To create an index, send a POST request to $URL/$DATABASE/_index with a body that
includes the fields to be indexed. To create an index for the year field, complete the
following steps, as shown in the following figure:
__ a. Update the request URL to $URL/$DATABASE/_index.
__ b. Update the HTTP method to POST.
EXempty
__ c. Update the request body with the following code, as shown in the following figure:
{
"index": {
"fields": [
"year"
]
},
"name": "year-json-index",
"type": "json"
}
__ d. Click Send.
__ e. Check the response that is shown in the following figure and verify that the index is
created successfully.
__ 14. To query a document, issue a POST request to $URL/$DATABASE/_find with a selector in the
body. A selector is a JSON object that describes the criteria that is used to select
documents. So, to query the documents of the novels that are published after the year 1880
and sort them by year in ascending order, complete the following steps:
__ a. Update the Request URL to $URL/$DATABASE/_find.
EXempty
__ b. Keep the HTTP Method as POST.
__ c. Update the Request body with the Cloudant Query content:
○ “selector” specifies querying all documents with a year greater than 1880.
○ “fields” specifies that _id, name, author, and year should be returned in the query
results.
○ “sort” specifies to sort by year. To sort by any other field, an index should be created
for the other field.
{
"selector": {
"year": {
"$gt": 1880
}
},
"fields": [
"_id",
"name",
"author",
"year"
],
"sort": [
{
"year": "asc"
}
]
}
__ d. Click Send. The query results are returned in the response that is shown in the following
figure. It is a good practice to create an index for each field that you are searching for in
the selector to optimize query performance.
EXempty
Information
By creating an index for the year field, you create and store a type of document that is called a
design document, which you store it in the Cloudant database. Design documents are special
documents that serve specific functions other than storing the data. Design documents are used to
build indexes, validate updates, and format query results.
EXempty
Troubleshooting
If in the response to a Cloudant HTTP API request you receive the error HTTP status code 401
Unauthorized, as shown in the following figure, the access token is expired and you must create a
new access token.
__ 2. You are redirected to the Resource list page. Under Services, you find your Cloudant
service instance. Click the More Actions icon (the three horizontal dots), and then click
Delete, as shown in the following figure.
EXempty
__ 3. Confirm that you want to delete the service by clicking Delete, as shown in the following
figure.
EXempty
Exercise review and wrap-up
In this exercise, you created an instance of Cloudant on IBM Cloud. You created credentials for the
service to make requests from an HTTP API client by using these credentials. You explored the
Cloudant dashboard and the features that are available in the Cloudant dashboard. You also
created, read, updated, deleted, and queried Cloudant documents by using an HTTP API client.