Friday 13 May 2016

IBM Bluemix : Using Data Services - Part 2

b. Describe the unique features of IBM Bluemix PaaS data services

1. Understand the unique features of Cloudant NoSQL Database

a. IBM Cloudant NoSQL DB for Bluemix is a NoSQL database as a service (DBaaS) that scales globally, runs non-stop, and handles data in JSON format and supports full text queries and geospatial queries. Cloudant NoSQL DB is an operational data store optimized to handle concurrent reads and writes, and provide high availability and data durability.

b. Cloudant provides a RESTful API to create, read, update and delete documents

Reading a document:

Cloudant's RESTful API makes every document in your Cloudant database accessible as JSON via a URL; this is one of the features that make Cloudant so powerful for web applications.
Each document in Cloudant has a unique _id field which can be used to retrieve it.
To retrieve it via the API, simply append the document's id to the URL of the database. For a document of _id foo in the mydb database the GET URL to retrieve the document would look like this

GET https://[username].cloudant.com/mydb/foo

Insert a document:

Documents can be inserted into Cloudant individually or in bulk.
To insert documents you have the option to provide a unique value for the _id field. If the document to be inserted doesn't define a _id value, one gets assigned on insert.
If you define a _id for your document up front you need to make sure that the _id isn't already in use. If it is, the insert will fail.

Code examples

Insert via CURL

Command

curl -d '{"season": "summer", "weather": "usually warm and sunny"}' -X POST https://[username].cloudant.com/crud/ -H "Content-Type:application/json"

Response

{"ok":true,"id":"590e2bca76c09882e37dea534b000be3","rev":"1-0af5e64fe24d262db237b9f14046f490"}

If you want to set the _id when you insert, you can do it in two ways: POST and PUT.

Set the _id via POST

POST the document with the _id in the document body:

Command

curl -d '{"season": "summer", "weather": "usually warm and sunny", "_id":"foo"}' -X POST https://[username].cloudant.com/crud/ -H "Content-Type:application/json"

Response

{"ok":true,"id":"foo","rev":"1-0af5e64fe24d262db237b9f14046f490"}

Set the _id via PUT

Or PUT the document, specifying the _id in the URL:

Command

curl -d '{"season": "summer", "weather": "usually warm and sunny"}' -X PUT https://[username].cloudant.com/crud/bar -H "Content-Type:application/json"

Response

{"ok":true,"id":"bar","rev":"1-0af5e64fe24d262db237b9f14046f490"}

Update and delete documents:

The _rev field gets added to your documents by the server when you insert or modify them, and is included in the server response when you make changes or read a document. The _rev is built from a crude counter and a hash of the document and is used to determine what needs to be replicated between servers, and if a client is trying to modify the latest version of a document. For this reason updates need to send the _rev token to be able to modify a document.
It is important to note that _rev should not be used to build a version control system, it’s an internal value used by the server and older revisions are transient, and removed regularly.

The code or command line to update a document is the same as to insert, just be sure to include the _rev in the document body.

As you might expect deletions are done by using the DELETE HTTP method. There are some cases where firing a DELETE might not be possible so you can also delete a document by adding _deleted to the document and update it. This is especially useful for bulk operations, where many documents may be created, updated or deleted in a single HTTP operation. As you'll be removing the document you can delete the rest of its contents, apart from the _id, _rev and _deleted fields. If you leave other fields they will be in the documents "tombstone", this can be useful when replicating or validating document edits.

Code examples

To delete a document you need its _id and _rev, the easiest way to get the _rev for a known document _id is to issue a HEAD request against the document:

Get the _rev

Command

curl -i -X HEAD https://[username].cloudant.com/crud/[doc_id]

Response

HTTP/1.1 200 OK
X-Couch-Request-ID: 89d9d456
Server: CouchDB/1.0.2 (Erlang OTP/R14B)
ETag: "2-e4b98cc0fc2a181e4eb26f8ad41fa5fe"
Date: Mon, 04 Jun 2012 14:47:15 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 113
Cache-Control: must-revalidate

Delete the document

CouchDB sets the ETag to be the document _rev (which is handy for caching purposes). Now we have the _id and _rev we can delete the document with:

Command

curl -X DELETE https://[username].cloudant.com/crud/[doc_id]\?rev\=[doc_rev]

Response

{"ok":true,"id":"[doc_id]","rev":"[doc_rev]"}

Verify deletion

Command

curl https://[username].cloudant.com/crud/[doc_id]

Response
{"error":"not_found","reason":"deleted"}

Delete via PUT

You can also delete a document via a PUT by adding the _deleted attribute to the document:

Command
curl -d '{"_rev":"[doc_rev]", "_deleted":true}' -X PUT https://[username].cloudant.com/crud/[doc_id]

Response

{"ok":true,"id":"[doc_id]","rev":"[doc_rev]"}

Verify deletion

Command

curl https://[username].cloudant.com/crud/[doc_id]

Response

{"error":"not_found","reason":"deleted"}

Reference: https://cloudant.com/for-developers/crud/

c. Cloudant allows the creation of indexes via the use of MapReduce

Secondary indexes, or views, are defined in a map function, which pulls out data from your documents and an optional reduce function that aggregates the data emitted by the map.

These functions are written in JavaScript and held in "design documents"; special documents that the database knows contain these - and other - functions. Design documents are special documents that define secondary indexes.

A sample design document with MapReduce functions

{
"_id": "_design/name",
"views": {
"view1": {
"map":"function(doc){emit(doc.field, 1)}",
"reduce": "function(key, value, rereduce){return sum(values)}"
}
}
}

The naming convention for design documents is such that the name follows _design/ in the _id. This code defines view1 for the design document name. Design documents can contain multiple views; each is added to the views object.

Reference: https://cloudant.com/for-developers/views/

d. Cloudant Sync simplifies large-scale mobile development

Cloudant Sync enables you to push database access to the farthest edge of the network —mobile devices, remote facilities, sensors, and internet-enabled goods, so that you can:

 Scale bigger

 Enable client apps to continue running off-line

Cloudant Sync allows mobile and distributed apps to scale by replicating and synching data between multiple readable, writeable copies of the data in other data centers, and even on mobile iOS and Android devices. This is much easier and more cost efficient than growing a single, central database to handle all data collection.

Cloudant Sync allows you to create a single database for every user; you simply replicate and sync the copy of this database in Cloudant with a local copy on their phone or tablet (or vehicle, sensor, appliance, etc.). This can reduce round-trip database requests with the server. If there’s no network connection, the app runs off the database on the device; when the network connection is restored, Cloudant re-syncs the device and server. Some of Cloudant’s largest mobile developers have scaled into the millions of databases.

2. Understand the unique features of dashDB

a. dashDB is a data warehousing service that stores relational data, including special types such as geospatial data. Stored data can be analyzed with SQL or advanced built-in analytics like predictive analytics and data mining, analytics with R, and geospatial analytics. dashDB provides in-memory database technology to support both columnar and row-based tables.

3. Understand the unique features of SQL Database

a. SQL Database adds an on-demand relational database for applications running on IBM Bluemix.

Powered by DB2, it provides a managed database service to handle web and transactional workload offering high availability, automated backups and data privacy.

4. Understand the unique features of the IBM Time Series Database for Bluemix

a. IBM Time Series Database for Bluemix service is a managed data store for Internet of Things device data and time series analysis of the data. The Time Series database service supports multiple methods for applications to access it to store, update, and query the data in a Time Series Database including the MongoDB APIs, a REST API, the IoT REST API, and the IBM Informix JDBC API.

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.

Recent Comments

Contact Form

Name

Email *

Message *

Followers