Introduction to data services in IBM Cloud
This unit covers the following topics:
Database services in IBM Cloud
IBM Cloudant
6.1 What you should be able to do
After completing this unit, you should be able to:
Describe the benefits of IBM Cloudant.
Explain how Cloudant databases and documents are accessed from IBM Cloud.
Describe how to use REST APIs to interact with Cloudant database.
6.2 References
The following publications are useful for further research on the topic presented in this unit:
Cloudant documentation:
IBM Cloud Data Services documentation:
IBM Cloudant videos on YouTube:
IBM developerWorks articles and resources:
Online Learning Labs:
Get IBM Cloud Essentials Open Badge:
6.3 Database choices on IBM Cloud
Figure 6-1 Database choices on IBM Cloud
Notes:
IBM Cloud provides the main types of databases and offers the following database services for each type:
NoSQL databases have the advantage of storing objects in the same format that is expected by applications. Data is transferred and stored in standard formats, such as JSON, or XML. To perform a more complex query on a NoSQL database, you typically must create a view. A view is a self-maintained index that the database management system provides for quickly traversing the data set.
SQL database service provides databases that are based on relational tables. They were used extensively to store data in enterprise computing. SQL allows you to write queries combining rows from multiple related tables. Each table has a record with various attributes (much like an object).
Columnar database is the database that stores data in form of columns instead of rows; for example, to model the following information:
 – ID:1, Name: Ahmed, Age: 29, Weight: 65
 – ID:2, Name: Ben, Age:34, Weight: 70
 – ID:3, Name: John, Age: 32, Weight: 73
Each column is stored in the following database records:
 – Ahmed:1, Ben: 2, John: 3
 – 29:1, 34:2, 32:3
 – 65:1, 70:2, 73:3
Key-value pair data services allow efficient storage of key-value pair data.
6.4 Data services in IBM Cloud catalog
Figure 6-2 Data services in IBM Cloud catalog
Notes:
IBM Cloud offers the following database management systems that are supported by IBM and third parties to use with cloud computing:
NoSQL database services, including, Cloudant NoSQL DB and Compose for MongoDB.
SQL database services. including IBM Db2® hosted, Compose for PostgreSQL, ElephantSQL, and ClearDB Managed MySQL Database.
Columnar database services, including Compose for ScyllaDB.
Key-value pair in-memory data services, including Compose for Redis, which is an open-source, fast key/value, low-maintenance store.
6.5 Cloudant capabilities
Figure 6-3 Cloudant capabilities
Notes:
IBM acquired Boston-based cloud database startup Cloudant in 2014.
IBM Cloudant is a NoSQL database as a service (DBaaS) that is optimized for handling heavy workloads of concurrent reads and writes in the cloud. These workloads are typical for large, fast-growing web and mobile apps. It is built to scale globally, run continuously, and handle various data types, such as JSON, full-text, and geospatial.
Cloudant is designed to ensure that the flow of data between an application and its database remains uninterrupted and performs to the users’ satisfaction. The data replication technology also allows developers to put data closer to where their applications need it most.
Cloudant frees developers from worrying about managing the database, which enables them to focus on the application. Cloudant eliminates the risk, cost, and distractions of database scalability, which enables you to regain valuable time and your applications to scale larger and remain consistently available to users worldwide.
Data is stored and sent in JSON format. The data documents are accessed with a simple REST-based HTTP method. Anything that is encoded into JSON can be stored as a document.
The NoSQL databases feature the following query capabilities that are well-suited for the data format used in the applications:
Cloudant’s search is powered by Apache Lucene, which is the most popular open-source search library that is designed to index and search JSON documents. By drawing on the speed and simplicity of Lucene, the Cloudant service provides a familiar way to add search to applications.
Cloudant Geo is an easy GeoJSON storage with built-in spatial querying and map visualization. GeoJSON is a format based on JSON that supports the encoding of geographic data structures.
Offline First Capabilities let users synchronize data with their mobile devices, which allows them to work in disconnected mode. Offline First applications that are built with Cloudant Sync provide a better and faster offline and online user experience by storing and accessing data locally and then synchronizing this data with the cloud database when an internet connection is available.
Language-specific libraries (wrappers that help you work with a simple API) for Cloudant are available.
For more information, see the following website:
 
6.6 Documents in Cloudant
Figure 6-4 Documents in Cloudant
Notes:
Cloudant documents are containers for the data, and the documents are JSON objects. All documents in Cloudant must contain the following unique fields:
An identifier _id field serves as the document key. It can be created by the application or generated automatically by Cloudant.
A revision number _rev field is automatically generated and 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 them. You must specify the latest _rev when updating a document or your request fails. It also helps avoid conflicting data states.
6.7 Getting started with Cloudant on IBM Cloud
Figure 6-5 Getting started with Cloudant on IBM Cloud
Notes:
IBM Cloud provides boilerplate starter applications for Cloudant with ASP.NET, Java, and Node.js. The Node.js Cloudant DB Web Starter is used in one of the exercises in this unit.
For more information about Cloudant documentation, see the following website:
6.8 IBM Cloud: VCAP_SERVICES
Figure 6-6 IBM Cloud: VCAP_SERVICES
Notes:
When you create an application in IBM Cloud that uses the Cloudant database, or when you add the Cloudant service to an application, IBM Cloud adds the credentials of the Cloudant database to the VCAP_SERVICES environment variable. This environment variable includes the credentials, such as the URL, host, port number, username, and password for accessing the Cloudant database. It is easy to parse this into your application and extract the information that you need for a connection.
You are automatically assigned all of the credentials that you need to access the Cloudant database with the Cloudant Dashboard user interface.
6.9 Cloudant Dashboard
Figure 6-7 Cloudant Dashboard
Notes:
Cloudant Dashboard is a cloud-based web interface that makes it easy to develop, administer, and monitor your databases. You can perform many tasks, such as:
View and manage Cloudant databases
View and create documents
Create and run queries
Manage the permissions to the database
You can also display the contents of a Cloudant document in IBM Cloud by selecting the database. Then, select All Documents to display the list of documents. You can edit each of the documents in the list to display or modify the document contents.
 
6.10 Cloudant REST API
Figure 6-8 Cloudant REST API
Notes:
Cloudant uses a REST API to provide simple, web-based access to data in the Cloudant data store. The REST API is a programmatic way of accessing the data from your applications. It provides several REST access methods for data read, add, update, and delete functions.
The following HTTP Request methods can be used:
GET: Request a specific JSON document
PUT: Create databases and documents
POST: Set values, and create documents
DELETE: Delete a specific document
6.11 Sample database at Cloudant
Figure 6-9 Sample database at Cloudant
Notes:
The sample database that was created by Node.js Cloudant DB Web Starter Boilerplate is named my_sample_db.
You can get more information about the database by calling this REST API https://$USERNAME.cloudant.com/my_sample_db/ where $USERNAME is the username of Cloudant DB that you saw in the IBM Cloud VCAP_SERVICES environment variables.
The permissions that are assigned to the creator of the database as seen in the Cloudant Dashboard UI are shown in Figure 6-9.
6.12 Reading a document in Cloudant
Figure 6-10 Reading a document in Cloudant
Notes:
This figure in this slide shows accessing a document from the browser. From your application, you can access the document through REST APIs.
To access a document with the Cloudant RESTful API, append the document ID to the URL of the database. The URL that is used to access this document in the Cloudant sample database is https://$USERNAME.cloudant.com/$DATABASE/$DOCUMENT_ID, which is accessed with a GET HTTP REST Request if you have sufficient permissions to access the DB. The _id is a unique key that is used when reading a document in the Cloudant database.
The sample document in the Cloudant Dashboard that is shown in Figure 6-10 includes three fields (name, value, and _attachments) in addition to the mandatory fields _id and _rev.
6.13 View all documents
Figure 6-11 View all Documents
Notes:
Cloudant includes an index that is named _all_docs with which you can build a URL to list all the documents in the database. You can pass to it an optional parameter that is named include_docs to return the contents of the documents, not just the _id and _rev. To change this parameter from the Cloudant Dashboard, click Options on the top toolbar then, click Include Docs.
As shown in Figure 6-11, you can click the {} JSON link at any stage in Cloudant Dashboard to view the generated REST API for the page that the user is viewing in the Cloudant Dashboard.
6.14 More Cloudant REST APIs
Figure 6-12 More Cloudant REST APIs
Notes:
To create a document, send a POST request to https://$USERNAME.cloudant.com/$DATABASE with the document's JSON content in the request body.
To update (or create) a document, send a PUT request to https://$USERNAME.cloudant.com/$DATABASE/$DOCUMENT_ID with the updated JSON content, including the latest _rev value in the request body.
To delete a document, send a DELETE request to https://$USERNAME.cloudant.com/$DATABASE/$DOCUMENT_ID?rev=$REV where $REV is the document’s latest _rev.
 
Note: All the previous operations on the database (Create, Update and Delete) can be done by using POST and PUT methods, with different parameters sent.
 
6.15 Cloudant Query
Figure 6-13 Cloudant query
Notes:
Before you query for a specific field, it is suggested that an index is created for each field in the selector to optimize query performance.
6.16 HTTP status codes
Figure 6-14 HTTP status codes
Notes:
Cloudant uses HTTP status codes that are returned in HTTP response headers.
More information might also be included in the response body area for the message.
The following example status codes adhere to the widely-accepted status codes for HTTP:
200 - OK
201 - Created
400 - Bad request
401 - Unauthorized
404 - Not Found
For example, if you try to use https://$USERNAME.cloudant.com/$DATABASE/$DOCUMENT_ID to retrieve a document that does not exist in the database, Cloudant responds with status code 404 in the header, and more information about the error is returned in the response as JSON, as shown in Figure 6-14.
The language-specific libraries often include error handling for these various cases.
6.17 Unit summary
 
Figure 6-15 Unit summary
6.18 Checkpoint questions
Figure 6-16 Checkpoint questions
 
6.19 Checkpoint answers
Figure 6-17 Checkpoint answers
 
 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.226.84