2. A Little about NoSQL
• “non SQL“ "non relational" or “Not Only SQL“
• Data modeling in other than tabular relations (column family store, key-value
stores, documents, xml, graph ; No Schema)
• useful for very large sets of distributed data
• seeks to solve the scalability and big data performance issues that
relational databases weren’t designed to address
• Pros: Elastic scaling, Flexible data models
• Cons: Maturity, Support, Analytics and business intelligence,
Administration, Expertise
4. Couchbase History
• CouchDB, actively maintained and
enhanced as an Apache project
• Couchbase not entirely open-
source/free software.
• Editions: enterprise, community…
• Few more confusing names
Couchbase Mobile, Couchbase Lite,
CouchApps, BigCouch, Touchbase,
Membase, Memcached,
MemcacheDB
5. Couchbase Basic Concepts
• NoSQL document database
• no database schema, flexible data model
• Easy scalability
• Consistent high performance
• Always online/high availability
• supports broad set of web, mobile and IoT applications
• Powerful Query Language (N1QL)
• Simple Administration (web console)
6. Couchbase Server Architecture
• works on peer-to-peer node mechanism
• no single point of failure. When there is a failure of one node,
another node takes up its responsibility, thus providing high
availability.
8. Terminology
• Node: A single Couchbase Server
• Cluster: a collection of nodes. Expand/Shrink. Cluster manager.
• Bucket: database (types: memcached and couchbase)
• Documents/Item/objects/key-value pair: as rows in the RDBMS table. Document id as PK. Metadata:
Rev/Expiration/flag
• vBucket: physical partitions of the bucket data. typically 1024.
• Replicas: redundant copies of data stored by buckets.-> again creates replica vBuckets
• Cluster map: mapping of which services belong to which nodes at a given point in time
• vBucket map: Cluster map contains a vBucket map.
• Replication: creating additional copies of active data on alternate nodes
• Rebalancing: As the number of nodes changes, the rebalance operation is used to redistribute the load.
• Failover: process that diverts traffic away from failing nodes
• Node Lifecycle: Add Node, Remove Node, Rebalance, and Failover cause state transitions
23. Querying Data using
Console
• MapReduce views accessed via the View
API.
• Spatial views accessed via the Spatial View
API.
• N1QL queries
24. Querying Data from Application
One of following;
• key-value access data service
• Views API
• Using N1QL
Need Indexes
25. Views
Indexes are data-structure that provides quick and efficient means to query
Views use existing indexes as is.
• Global Secondary Index
• MapReduce views (Views)
• Spatial views
• Full Text Index (not yet released)
• Created using
JavaScript map and reduce functions
• data service is responsible to handle views
26. MapReduce views (Views) Every document passes through View Map() function
Map
f u n c t i o n ( d o c , m e t a ) {
e m i t ( d o c . i d , d o c . t i t l e ) ;
}
JSON doc JSON metadata
Create row Indexed key Output value(s)
[doc.iata, doc.name, doc.country]
Select id, name, title, city from usertable
Where id = 55
27. Creating a MapReduce View
MapReduce View
emit function generates a list of key-value pairs
map function: generates output data, which represents
a mapping of the documents stored within the bucket
32. Using a custom Reduce Function
• It is Group By in nature
• complex logic written in javascript to
strict the output of map function.
• Should be used very carefully.
• They are very tricky.
36. Using MapReduce Views in node.js
HTTP method URI path Description
GET /[bucket_name]/_design/[ddoc-name] Retrieves design documents.
PUT /[bucket_name]/_design/[ddoc-name] Creates a news design document with one
or more views.
DELETE /[bucket_name]/_design/[ddoc-name] Deletes design documents.
GET /[bucket_name]/_design/[ddoc-
name]/_view/[view-name]
Retrieves views.
POST /internalSettings Changes the number of simultaneous
requests each node can accept.
with Views REST APIView functions are stored in a design document as JSON.
Use REST API to manage your design documents.Views endpoints
37. Using N1QL
system catalog stores metadata about a database.
A declarative query language that extends SQL for JSON.
SELECT * FROM system:datastores;
SELECT 2+5 FROM system:dual;
Namespace
41. Insert multiple records
INSERT INTO namespace:keyspace (KEY,
VALUE)
VALUES ( "1",
{ "value": "one" } ),
VALUES ( "2",
{ "value": "two" } ),
…,
VALUES ( "n",
{ "value": "n" } );
What an insert query can return?
default:`travel-sample` AS t
42. What Insert can return
Result keys Values description
requestID Request ID of the statement generated by the server.
signature Signature of the fields specified in the returning clause.
results If returning clause specified return
errors error codes and messages
status "successful" or "errors"
metrics elapsedTime elapsed time
executionTime Couchbase Server to execute time
resultCount May 0
resultSize Total number of results
mutationCount number of documents inserted
51. Views
View to return all airline docs only.
function (doc, meta) {
emit(meta.id, doc.name);
}
• Create a development view and view the sample
view output.
• Refine and update your view definition to suit your
needs, repeating the process until your view is
complete. During this phase you can access your
view from your client library and application to
ensure it suits your needs.
• Once the view definition is complete, apply your view
to your entire cluster data set.
• Push your development view into production. This
moves the view from development into production,
and renames the index (so that the index does not
need to be rebuilt).
• Start using your production view.
View – select all doc not recommended
function (doc, meta) {
if(doc.type == "airline")
{
emit(meta.id, doc);
}
}
52. Views
Check nulls – best practice
function (doc, meta)
{
if (doc.type && doc.type == "airline" && doc.name)
{
emit(doc.name, [doc.name, doc.id, doc.country]);
}
}
View – using variable as output
var getSpecificRecords = function(doc,
meta){
//emit dunction.
} };
This can later be use in design doc as follows;
var designDoc =
{
"views":
{
"winGameSessions": getSpecificRecords .toString();
}
};
53. Views
Getting all the flights
function(doc, meta) {
if (doc.type === 'route') {
for (var i = 0; i < doc.schedule.length; i++) {
var schedule = doc.schedule[i];
var time = parseInt(schedule.utc.replace(/:/g, ''));
var key = [schedule.day, time];
var value = [
schedule.flight,
doc.sourceairport,
doc.destinationairport
];
emit(key, value);
}
}
}
L3a
54. Getting hotel reviews
Can be done by adding a where param in the url or wth view
function(doc, meta)
{
if (doc.type === 'hotel')
{
for (i=0; i < doc.reviews.length; i++)
{
emit(doc.reviews[i].author, [doc.reviews[i].date, doc.reviews[i].ratings] );
}
}
}
L3b
55. L3b
Getting hotel reviews add where
Can be done by adding a where param in the url or wth view
function(doc, meta)
{
if (doc.type === 'hotel')
{
for (i=0; i < doc.reviews.length; i++)
{
emit(doc.reviews[i].author, [doc.reviews[i].date, doc.reviews[i].ratings] );
}
}
}
Param value to be added based on the key given in emit function.
http://127.0.0.1:8092/travel-
sample/_design/dev_simpleVU/_view/L4?connection_timeout=60000&full_set=true&inclusive_end=true&key=%22Un
ited%20Kingdom%22&limit=9000&skip=90&stale=false
Keys=[“c1",“c2"]
56. Adding reduce function
L5
function (doc, meta) {
emit(meta.id, [9,7,8,5]);
}
function (keys, values, rereduce) {
if(!rereduce) {
return values.length;
} else {
var sum = 0;
for (i in values) {
sum += values[i];
}
return sum;
}
}
57. Date and time selections
function(doc, meta) {
emit([doc.year, doc.mon, doc.day, doc.logtype], null);
}
L6
58. View (joins)
• Pseudo joins
Will create a join with airline and airport type documents.
function (keys, values){ // ignoring reduce here to join only
var airport = “’ ;
//write your logic on keys and values here
return values.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});
//return values;
}
function (doc, meta) {
if( doc.type == "airline")
{
emit(doc.id, doc.country);
}
else if(doc.type == "airport")
{
emit (doc.name, doc.country);
}
}