Introduction to data services in IBM Bluemix
This unit covers the following topics:
Database services in IBM Bluemix
IBM Cloudant
6.1 What you should be able to do
After completing this unit, you should be able to:
Describe the main types of data services in IBM Bluemix.
Describe the benefits of IBM Cloudant.
Explain how Cloudant databases and documents are accessed from IBM Bluemix.
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.com documentation:
IBM Cloud Data Services documentation:
IBM DeveloperWorks articles and resources:
6.3 Database choices on Bluemix
Figure 6-1 Database choices on IBM Bluemix
Notes:
IBM Bluemix provides the main types of databases and 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 are used extensively to store data in enterprise computing. SQL allows you to write queries that combine 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 a database record:
 – 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 Bluemix catalog
Figure 6-2 Data services in IBM Bluemix catalog
Notes:
IBM Bluemix offers the following database management systems that are supported by IBM and other third-party vendors to use with cloud computing:
NoSQL database services, such as Cloudant NoSQL DB and Compose for MongoDB.
SQL database services, such as Db2 hosted, Compose for PostgreSQL, ElephantSQL, and ClearDB Managed MySQL Database.
In-memory columnar database services, such as IBM dashDB® for Analytics.
Key-value pair data services, such as Redis Cloud, which is an open-source, fast key/value low maintenance store.
The database services in the catalog are offered by IBM or third parties.
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 non-stop, 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 and 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, and 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 Search is powered by Apache Lucene, 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 apps.
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, allowing them to work in disconnected mode. Offline-first applications built with Cloudant Sync provide a better, faster user experience offline and online, by storing and accessing data locally and then synchronizing this data with the cloud database when an internet connection is available.
Language-specific libraries for Cloudant are available, which are wrappers that help you work with a simple API.
For more information, see this website:
6.6 Cloudant in IBM Bluemix versus Cloudant.com
Figure 6-4 Cloudant in IBM Bluemix versus Cloudant.com
Notes:
You can sign up to use the Cloudant database at the following website or use the Cloudant data management services that are provided by IBM Bluemix:
When you sign up, users are offered a choice of the underlying infrastructure as a service, including SoftLayer, Rackspace, and Azure.
When you create an IBM Bluemix Cloudant service, an account is generated for you on the SoftLayer Cloudant cluster for the specific IBM Bluemix region that you are signed in with IBM Bluemix.
6.7 Documents in Cloudant
Figure 6-5 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.8 Getting started with Cloudant on IBM Bluemix
Figure 6-6 Getting started with Cloudant on IBM Bluemix
Notes:
IBM Bluemix 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.
Cloudant also features rich documentation.
6.9 IBM Bluemix Cloudant: VCAP_SERVICES
Figure 6-7 IBM Bluemix Cloudant: VCAP_SERVICES
Notes:
When you create an application in IBM Bluemix that uses the Cloudant database, or when you add the Cloudant service to an application, IBM Bluemix adds the credentials of the Cloudant database to the VCAP_SERVICES environment variable. This 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 variable into your app 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.10 Cloudant Dashboard
Figure 6-8 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 documents, create documents, create and run queries, and manage the permissions to the database.
You can display the contents of a Cloudant document in IBM Bluemix 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.
The Cloudant Dashboard user interface is similar whether you use the following website or IBM Bluemix:
 
6.11 Cloudant REST API
Figure 6-9 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.12 Sample database at Cloudant
Figure 6-10 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 earlier in the Bluemix 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-10.
6.13 Reading a document in Cloudant
Figure 6-11 Reading a document in Cloudant
Notes:
Accessing a document with Cloudant Dashboard is shown in Figure 6-11. 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 used to access this document in the Cloudant sample database is https://$USERNAME.cloudant.com/$DATABASE/$DOCUMENT_ID, which is accessed by using a GET HTTP REST request command 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-11 includes three fields in addition to the mandatory fields _id and _rev: name, company, and age.
6.14 View all documents
Figure 6-12 View all Documents
Notes:
Cloudant includes an index called _all_docs with which you can use a URL to list all the documents in the database. You can pass to it an optional parameter called include_docs to return the contents of the documents, not just the _id and _rev.
As shown in Figure 6-12, you can click the API 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.15 More Cloudant REST APIs
Figure 6-13 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 and the latest _rev value (not needed for creating documents) 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.
6.16 Cloudant query
Figure 6-14 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.17 HTTP status codes
Figure 6-15 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-15.
The language-specific libraries often include error handling for these various cases.
6.18 Unit summary
 
Figure 6-16 Unit summary
6.19 Checkpoint questions
Figure 6-17 Checkpoint questions
 
6.20 Checkpoint answers
Figure 6-18 Checkpoint answers
 
 
..................Content has been hidden....................

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