O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Couchbas for dummies

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Om nom nom nom
Om nom nom nom
Carregando em…3
×

Confira estes a seguir

1 de 58 Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (20)

Semelhante a Couchbas for dummies (20)

Anúncio

Mais recentes (20)

Couchbas for dummies

  1. 1. Couchbase NoSQL document-oriented database Basic Knowledge Document is prepared by
  2. 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
  3. 3. Trends Some of Couchbase Customers
  4. 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. 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. 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.
  7. 7. Couchbase Data Flow
  8. 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
  9. 9. Installation & Setup https://www.couchbase.com/nosql-databases/downloads SDKs available at: https://developer.couchbase.com/open-source-projects Couchbase Node.js Client Library
  10. 10. Installation completed services.msc http://localhost:8091/
  11. 11. Using Couchbase Querying data by • SDK (node.js) • command-line cbc tool (mainly used by MAC / Linux) • Web console
  12. 12. Couchbase Web Console /Admin UI http://localhost:8091/ Web Console Pre-release in angular material different view
  13. 13. Configuring a server
  14. 14. Configuring a server
  15. 15. Configuring a server
  16. 16. Configuring a server Note: Don’t give your mobile number you will end up getting lots of calls from Couchbase
  17. 17. Couchbase Web Console /Admin UI
  18. 18. Couchbase Web Console /Admin UI
  19. 19. Couchbase Web Console /Admin UI
  20. 20. Couchbase Web Console /Admin UI buckets
  21. 21. Couchbase Web Console /Admin UI documents
  22. 22. Documents in ‘travel-sample’ airline airport
  23. 23. Querying Data using Console • MapReduce views accessed via the View API. • Spatial views accessed via the Spatial View API. • N1QL queries
  24. 24. Querying Data from Application One of following; • key-value access data service • Views API • Using N1QL Need Indexes
  25. 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. 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. 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
  28. 28. Adding array of values
  29. 29. Adding Where clause • Simply add &key="landmark_37519“ in last
  30. 30. Using a built-in Reduce Function
  31. 31. Count and Group by
  32. 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.
  33. 33. Adding filter from console
  34. 34. Using MapReduce Views in node.js with viewQuery class
  35. 35. 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
  36. 36. 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
  37. 37. N1QL – Retrieve data
  38. 38. N1QL – Retrieve data
  39. 39. N1QL – Insert
  40. 40. 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
  41. 41. 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
  42. 42. DML Operation error Documents with same id cannot be created. But by default there is no restriction on values.
  43. 43. N1QL – Update UPDATE keyspace-ref [use-keys-clause] [set-clause] [unset-clause] [where-clause] [limit-clause] [returning-clause] default:`travel-sample` AS t [(namespace-name :)] keyspace [ [AS] alias] ‘document name’ country = “my country”
  44. 44. Sample Update Updating one row Multiple rows array of values
  45. 45. N1QL – Upsert Insert or Update UPSERT INTO keyspace-ref [insert-values | insert-select] [ returning-clause] default:`travel-sample` AS t [(namespace-name :)] keyspace [ [AS] alias]
  46. 46. Sample Upsert Query
  47. 47. Using N1QL in Node.JS bucket.query(couchbase.N1qlQuery.fromSt ring("SELECT name FROM ‘travel-sample’ WHERE type=‘airline'"));
  48. 48. Travel-sample data model Airline Airport Route landmark Id/key = primary key (airline_10) UUID() Inbound/outbound refernces Key ref hotel Review, amenities, geo etc
  49. 49. hotel
  50. 50. 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); } }
  51. 51. 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(); } };
  52. 52. 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
  53. 53. 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
  54. 54. 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"]
  55. 55. 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; } }
  56. 56. Date and time selections function(doc, meta) { emit([doc.year, doc.mon, doc.day, doc.logtype], null); } L6
  57. 57. 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); } }

×