O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
VS
Database
No SQL
Key-Value Database
Document Database
Document
         {
             "day": [ 2010, 01, 23 ],
             "products": {
                 "apple": {
         ...
Couchdb                           Mongodb

Data Model      Document-Oriented (JSON)           Document-Oriented (BSON)


 ...
Map/Reduce???
Example: Tickets




{                      {                      {                      {
    "id": 1,               "id...
Sum(checkout)?




{                      {                      {                      {
    "id": 1,               "id":...
Map: emit(checkout)




       100                     42                    215                     73


{               ...
Reduce: sum(checkouts)



                   142                                          288



        100              ...
Reduce: sum(checkouts)

                                         430



                   142                            ...
Reduce must be associative

reduce(         100         42        215          73    ) ==   430




                      ...
SELECT
SUM(checkout)
 FROM ticket
    ?!?!?!?
Inherently distributed

                                        430



                  142                              ...
Logaritmic Update

                                        430



                  142                                   ...
Logaritmic Update

                                        430



                  142                                   ...
Logaritmic Update

                                        430



                  142                                   ...
Logaritmic Update

                                        425



                  142                                   ...
Logaritmic Update
Sum(checkout)
Sum(checkout)
Sum(checkout)
Sum(checkout)
Sum(checkout)
# START SERVER
$ ~/opt/mongodb-1.3.0/bin/mongod 
  --dbpath=./db/mongodb.01/ 
  --logpath=./log/mongodb.01 
...
Sum(checkout)
> use checkout
switched to db checkout

>   db.tickets.save({   "_id":   1,   "day":   20100123,   "checkout...
Sum(checkout)
> var map = function() {
... emit(null, this.checkout)
... }

> var reduce = function(key, values) {
... var...
Sum(checkout)
                               Temporary Collection
> sumOfCheckouts = db.tickets.mapReduce(map, reduce)
{
 ...
Sum(checkout)
                                  Persistent Collection
> db.tickets.mapReduce(map, reduce, { “out”: “sumOfC...
Sum(checkout)
                                    Reduce by Group
# GROUP AS MAP/REDUCE ALTERNATIVE

> db.tickets.group({
...
Sum(checkout) Group By day




   {                      {                      {                      {
       "id": 1,  ...
Map: emit(day,checkout)




 “20100123”:100         “20100124”:42          “20100123”:215         “20100124”:73




 {    ...
Reduce: sum(checkouts)


                  “20100123”:315




 “20100123”:100         “20100124”:42          “20100123”:21...
Reduce: sum(checkouts)


                  “20100123”:315                        “20100124”:115




 “20100123”:100       ...
Sum(checkout)
 Group By day
Sum(checkout)
 Group By day
Design Documents are
          Documents
Design Documents are
          Documents
Non trivial Map:
Calculate Checkout
Non trivial Map:
Calculate Checkout
Non trivial Map:
Calculate Checkout
Structured Keys and
       Group Levels
Structured Keys and
       Group Levels
Structured Keys and
       Group Levels
Structured Keys and
       Group Levels
Structured Keys and
       Group Levels
Structured Keys and
       Group Levels
Structured Keys and
       Group Levels
Sum(Checkout) by day
                            Update In-Place
> db.tickets.update({ "_id": 1 }, {
... $set: { "products...
Sum(Checkout) by day
                         Calculate Checkout
> db.tickets.find()
{ "_id" : 1, "day" : 20100123, "produ...
Sum(Checkout) by day
                         Calculate Checkout
> var map = function() {
... var checkout = 0
... for (va...
Sum(Checkout) by day
                         Calculate Checkout
> db.tickets.mapReduce(map, reduce, { "out": "sumOfChecko...
Sum(Checkout) by day
                           Data Normalization
> db.tickets.find()
{ "_id" : 1, "day" : 20100123, "pro...
Sum(Checkout) by day
                         Data Normalization
> var map = function() {
... var checkout = 0
... for (va...
Sum(Checkout) by day
                         Data Normalization
> db.tickets.mapReduce(map, reduce, { "out": "sumOfChecko...
Count of unique
                                                  elements?
>   db.view.find();
{   "user" : "001",   "pag...
Count of unique
                                          elements?
> var map = function() {
... var accumulator = {
........
Count of unique
                                             elements?
# EASY TO DEBUG

> var aUser = db.view.findOne({ "u...
Count of unique
                                                  elements?
> var reduce = function(key, values) {
... var...
Count of unique
                                            elements?
> db.view.mapReduce(map, reduce,
  { "out": "userNav...
Count of unique
                                          elements?
> var finalize = function(key, accumulator) {
... accu...
Count of unique
                                          elements?
> db.view.mapReduce(map, reduce, {
... "finalize": fin...
Count of unique
                                       elements by steps
# STEP 1: CREATE THE BASE COLLECTION (WITHOUT UNI...
Count of unique
                                         elements by steps
> var finalizeBase = function(key, accumulator)...
Count of unique
                                          elements by steps
# STEP 2: CREATE THE COLLECTION OF UNIQUE ELEM...
Count of unique
                                       elements by steps
> db.userUniquePages.find()

{ "_id" : "001-examp...
Count of unique
                                         elements by steps
# STEP 3: UPDATE BASE COLLECTION WITH UNIQUE EL...
Architecture

          Webmachine




HTTP




             HTTP


 HTTP
Scalability

        Webmachine




Nginx




                         Master/Master

        Webmachine
User Account
Message
Received by <account>
                          After <timestamp>?

function(document) {
    if (document.from && document...
Received by <account>
  After <timestamp>?
Received by <account>
                               After <timestamp>?

> curl -X GET ".../mercurio/_design/message/_view...
Results are
                                      ordered by Key
{ "key": [ "chiara", 126 ],
  "value": {
    "id": "ff353...
Select with Key

{ "key": [ "chiara", 126 ],
  "value": {
    "id": "ff35356344ee0e992...",
                              ...
Select with range of Keys

{ "key": [ "chiara", 126 ],         received_after?
  "value": {
    "id": "ff35356344ee0e992.....
Select with range of Keys

{ "key": [ "chiara", 126 ],
  "value": {
    "id": "ff35356344ee0e992...",
    "from": "gabriel...
Select with range of Keys

{ "key": [ "chiara", 126 ],         received_after?
  "value": {
    "id": "ff35356344ee0e992.....
Received by “chiara”
                                              After 126
{ "key": [ "chiara", 126 ],         received_...
Push Received
             Messages from Server



Check for Messages           _changes?
  received by <account.id>     f...
Push Received
                         Messages from Server

_changes?filter=message/received&by=<account.id>&after=<times...
Backoffice as
Couch Application
CouchDB Vs MongoDB
Próximos SlideShares
Carregando em…5
×

de

CouchDB Vs MongoDB Slide 1 CouchDB Vs MongoDB Slide 2 CouchDB Vs MongoDB Slide 3 CouchDB Vs MongoDB Slide 4 CouchDB Vs MongoDB Slide 5 CouchDB Vs MongoDB Slide 6 CouchDB Vs MongoDB Slide 7 CouchDB Vs MongoDB Slide 8 CouchDB Vs MongoDB Slide 9 CouchDB Vs MongoDB Slide 10 CouchDB Vs MongoDB Slide 11 CouchDB Vs MongoDB Slide 12 CouchDB Vs MongoDB Slide 13 CouchDB Vs MongoDB Slide 14 CouchDB Vs MongoDB Slide 15 CouchDB Vs MongoDB Slide 16 CouchDB Vs MongoDB Slide 17 CouchDB Vs MongoDB Slide 18 CouchDB Vs MongoDB Slide 19 CouchDB Vs MongoDB Slide 20 CouchDB Vs MongoDB Slide 21 CouchDB Vs MongoDB Slide 22 CouchDB Vs MongoDB Slide 23 CouchDB Vs MongoDB Slide 24 CouchDB Vs MongoDB Slide 25 CouchDB Vs MongoDB Slide 26 CouchDB Vs MongoDB Slide 27 CouchDB Vs MongoDB Slide 28 CouchDB Vs MongoDB Slide 29 CouchDB Vs MongoDB Slide 30 CouchDB Vs MongoDB Slide 31 CouchDB Vs MongoDB Slide 32 CouchDB Vs MongoDB Slide 33 CouchDB Vs MongoDB Slide 34 CouchDB Vs MongoDB Slide 35 CouchDB Vs MongoDB Slide 36 CouchDB Vs MongoDB Slide 37 CouchDB Vs MongoDB Slide 38 CouchDB Vs MongoDB Slide 39 CouchDB Vs MongoDB Slide 40 CouchDB Vs MongoDB Slide 41 CouchDB Vs MongoDB Slide 42 CouchDB Vs MongoDB Slide 43 CouchDB Vs MongoDB Slide 44 CouchDB Vs MongoDB Slide 45 CouchDB Vs MongoDB Slide 46 CouchDB Vs MongoDB Slide 47 CouchDB Vs MongoDB Slide 48 CouchDB Vs MongoDB Slide 49 CouchDB Vs MongoDB Slide 50 CouchDB Vs MongoDB Slide 51 CouchDB Vs MongoDB Slide 52 CouchDB Vs MongoDB Slide 53 CouchDB Vs MongoDB Slide 54 CouchDB Vs MongoDB Slide 55 CouchDB Vs MongoDB Slide 56 CouchDB Vs MongoDB Slide 57 CouchDB Vs MongoDB Slide 58 CouchDB Vs MongoDB Slide 59 CouchDB Vs MongoDB Slide 60 CouchDB Vs MongoDB Slide 61 CouchDB Vs MongoDB Slide 62 CouchDB Vs MongoDB Slide 63 CouchDB Vs MongoDB Slide 64 CouchDB Vs MongoDB Slide 65 CouchDB Vs MongoDB Slide 66 CouchDB Vs MongoDB Slide 67 CouchDB Vs MongoDB Slide 68 CouchDB Vs MongoDB Slide 69 CouchDB Vs MongoDB Slide 70 CouchDB Vs MongoDB Slide 71 CouchDB Vs MongoDB Slide 72 CouchDB Vs MongoDB Slide 73 CouchDB Vs MongoDB Slide 74 CouchDB Vs MongoDB Slide 75 CouchDB Vs MongoDB Slide 76 CouchDB Vs MongoDB Slide 77 CouchDB Vs MongoDB Slide 78 CouchDB Vs MongoDB Slide 79 CouchDB Vs MongoDB Slide 80 CouchDB Vs MongoDB Slide 81 CouchDB Vs MongoDB Slide 82 CouchDB Vs MongoDB Slide 83 CouchDB Vs MongoDB Slide 84 CouchDB Vs MongoDB Slide 85
Próximos SlideShares
Rise of Column Oriented Database
Avançar
Transfira para ler offline e ver em ecrã inteiro.

301 gostaram

Compartilhar

Baixar para ler offline

CouchDB Vs MongoDB

Baixar para ler offline

Comparison by example between CouchDB and MongoDB

CouchDB Vs MongoDB

  1. 1. VS
  2. 2. Database
  3. 3. No SQL
  4. 4. Key-Value Database
  5. 5. Document Database
  6. 6. Document { "day": [ 2010, 01, 23 ], "products": { "apple": { "price": 10 Key -> "quantity": 6 }, "kiwi": { "price": 20 "quantity": 2 } }, "checkout": 100 }
  7. 7. Couchdb Mongodb Data Model Document-Oriented (JSON) Document-Oriented (BSON) Interface HTTP/REST Custom protocol over TCP/IP Object Database contains Collections Database contains Documents Storage Collections contains Documents Map/Reduce (javascript) creating Query Map/Reduce (javascript + others) Collections + Object-Based query Method creating Views + Range queries language Master-Master with custom Replication Master-Slave conflict resolution functions MVCC (Multi Version Concurrency Concurrency Update in-place Control) Written In Erlang C++
  8. 8. Map/Reduce???
  9. 9. Example: Tickets { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  10. 10. Sum(checkout)? { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  11. 11. Map: emit(checkout) 100 42 215 73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  12. 12. Reduce: sum(checkouts) 142 288 100 42 215 73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  13. 13. Reduce: sum(checkouts) 430 142 288 100 42 215 73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  14. 14. Reduce must be associative reduce( 100 42 215 73 ) == 430 Must be equal to reduce( reduce( 100 42 ) == 142 reduce( 215 73 ) == 288 ) == 430
  15. 15. SELECT SUM(checkout) FROM ticket ?!?!?!?
  16. 16. Inherently distributed 430 142 288 100 42 215 73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  17. 17. Logaritmic Update 430 142 288 100 42 215 73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 210 "checkout": 73 } } } }
  18. 18. Logaritmic Update 430 142 288 100 42 210 73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 210 "checkout": 73 } } } }
  19. 19. Logaritmic Update 430 142 283 100 42 210 73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 210 "checkout": 73 } } } }
  20. 20. Logaritmic Update 425 142 283 100 42 210 73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100123, "day": 20100123, "day": 20100123, "checkout": 100 "checkout": 42 "checkout": 210 "checkout": 73 } } } }
  21. 21. Logaritmic Update
  22. 22. Sum(checkout)
  23. 23. Sum(checkout)
  24. 24. Sum(checkout)
  25. 25. Sum(checkout)
  26. 26. Sum(checkout) # START SERVER $ ~/opt/mongodb-1.3.0/bin/mongod --dbpath=./db/mongodb.01/ --logpath=./log/mongodb.01 --port 30001 # START SHELL $ ~/opt/mongodb-1.3.0/bin/mongo localhost:30001 connecting to: localhost:30001/test type "help" for help > show dbs admin local
  27. 27. Sum(checkout) > use checkout switched to db checkout > db.tickets.save({ "_id": 1, "day": 20100123, "checkout": 100 }) > db.tickets.save({ "_id": 2, "day": 20100123, "checkout": 42 }) > db.tickets.save({ "_id": 3, "day": 20100123, "checkout": 215 }) > db.tickets.save({ "_id": 4, "day": 20100123, "checkout": 73 }) > db.tickets.count() 4 > db.tickets.find() { "_id" : 1, "day" : 20100123, "checkout" : 100 } ... > db.tickets.find({ "_id": 1 }) { "_id" : 1, "day" : 20100123, "checkout" : 100 }
  28. 28. Sum(checkout) > var map = function() { ... emit(null, this.checkout) ... } > var reduce = function(key, values) { ... var sum = 0 ... for (var index in values) sum += values[index] ... return sum ... }
  29. 29. Sum(checkout) Temporary Collection > sumOfCheckouts = db.tickets.mapReduce(map, reduce) { "result" : "tmp.mr.mapreduce_1263717818_4", "timeMillis" : 8, "counts" : { "input" : 4, "emit" : 4, "output" : 1 }, "ok" : 1 } > db.getCollectionNames() [ "tickets", "tmp.mr.mapreduce_1263717818_4", ] > db[sumOfCheckouts.result].find() { "_id" : null, "value" : 430 }
  30. 30. Sum(checkout) Persistent Collection > db.tickets.mapReduce(map, reduce, { “out”: “sumOfCheckouts” }) > db.getCollectionNames() [ “sumOfCheckouts”, "tickets", "tmp.mr.mapreduce_1263717818_4" ] > db.sumOfCheckouts.find() { "_id" : null, "value" : 430 } > db.sumOfCheckouts.findOne().value 430
  31. 31. Sum(checkout) Reduce by Group # GROUP AS MAP/REDUCE ALTERNATIVE > db.tickets.group({ ... "initial": { "sum": 0 }, ... "reduce": function(ticket, checkouts) { ...... checkouts.sum += ticket.checkout ...... } ... }) [ { "sum" : 430 } ]
  32. 32. Sum(checkout) Group By day { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100124, "day": 20100123, "day": 20100124, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  33. 33. Map: emit(day,checkout) “20100123”:100 “20100124”:42 “20100123”:215 “20100124”:73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100124, "day": 20100123, "day": 20100124, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  34. 34. Reduce: sum(checkouts) “20100123”:315 “20100123”:100 “20100124”:42 “20100123”:215 “20100124”:73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100124, "day": 20100123, "day": 20100124, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  35. 35. Reduce: sum(checkouts) “20100123”:315 “20100124”:115 “20100123”:100 “20100124”:42 “20100123”:215 “20100124”:73 { { { { "id": 1, "id": 2, "id": 3, "id": 4, "day": 20100123, "day": 20100124, "day": 20100123, "day": 20100124, "checkout": 100 "checkout": 42 "checkout": 215 "checkout": 73 } } } }
  36. 36. Sum(checkout) Group By day
  37. 37. Sum(checkout) Group By day
  38. 38. Design Documents are Documents
  39. 39. Design Documents are Documents
  40. 40. Non trivial Map: Calculate Checkout
  41. 41. Non trivial Map: Calculate Checkout
  42. 42. Non trivial Map: Calculate Checkout
  43. 43. Structured Keys and Group Levels
  44. 44. Structured Keys and Group Levels
  45. 45. Structured Keys and Group Levels
  46. 46. Structured Keys and Group Levels
  47. 47. Structured Keys and Group Levels
  48. 48. Structured Keys and Group Levels
  49. 49. Structured Keys and Group Levels
  50. 50. Sum(Checkout) by day Update In-Place > db.tickets.update({ "_id": 1 }, { ... $set: { "products": { ...... "apple": { "quantity": 5, "price": 10 }, ...... "kiwi": { "quantity": 2, "price": 25 } ...... } ... }, ... $unset: { "checkout": 1 } ... }) > db.tickets.find() { "_id" : 1, "day" : 20100123, "products" : { "apple" : { "quantity" : 5, "price" : 10 }, "kiwi" : { "quantity" : 2, "price" : 25 } }} { "_id" : 2, "day" : 20100123, "checkout" : 42 } { "_id" : 3, "day" : 20100123, "checkout" : 215 } { "_id" : 4, "day" : 20100123, "checkout" : 73 }
  51. 51. Sum(Checkout) by day Calculate Checkout > db.tickets.find() { "_id" : 1, "day" : 20100123, "products" : { "apple" : { "quantity" : 5, "price" : 10 }, "kiwi" : { "quantity" : 2, "price" : 25 } } } { "_id" : 2, "day" : 20100124, "products" : { "banana" : { "quantity" : 2, "price" : 20 } } } { "_id" : 3, "day" : 20100123, "products" : { "kiwi" : { "quantity" : 4, "price" : 25 }, "babana" : { "quantity" : 5, "price" : 20 }, "lemon" : { "quantity" : 3, "price" : 5 } } } { "_id" : 4, "day" : 20100124, "products" : { "kiwi" : { "quantity" : 2, "price" : 25 }, "babana" : { "quantity" : 1, "price" : 20 } } }
  52. 52. Sum(Checkout) by day Calculate Checkout > var map = function() { ... var checkout = 0 ... for (var name in this.products) { ...... var product = this.products[name] ...... checkout += product.quantity * product.price ...... } ... emit(this.day, checkout) } > var reduce = function(key, values) { ... var sum = 0 ... for (var index in values) sum += values[index] ... return sum }
  53. 53. Sum(Checkout) by day Calculate Checkout > db.tickets.mapReduce(map, reduce, { "out": "sumOfCheckouts" }) > db.sumOfCheckouts.find() { "_id" : 20100123, "value" : 315 } { "_id" : 20100124, "value" : 110 }
  54. 54. Sum(Checkout) by day Data Normalization > db.tickets.find() { "_id" : 1, "day" : 20100123, "products" : { "apple" : 5, "kiwi" : 2 } } { "_id" : 2, "day" : 20100124, "products" : { "banana" : 2 } } { "_id" : 3, "day" : 20100123, "products" : { "kiwi" : 4, "banana" : 5, "lemon" : 3 } } { "_id" : 4, "day" : 20100124, "products" : { "kiwi" : 2, "banana" : 1 } } > db.product.find() { "_id" : "apple", "price" : 10 } { "_id" : "kiwi", "price" : 25 } { "_id" : "banana", "price" : 20 } { "_id" : "lemon", "price" : 5 }
  55. 55. Sum(Checkout) by day Data Normalization > var map = function() { ... var checkout = 0 ... for (var name in this.products) { ...... var quantity = this.products[name] ...... var price = db.product.findOne({ "_id": name }).price ...... checkout += quantity * price ...... } ... emit(this.day, checkout) } > var reduce = function(key, values) { ... var sum = 0 ... for (var index in values) sum += values[index] ... return sum }
  56. 56. Sum(Checkout) by day Data Normalization > db.tickets.mapReduce(map, reduce, { "out": "sumOfCheckouts" }) > db.sumOfCheckouts.find() { "_id" : 20100123, "value" : 315 } { "_id" : 20100124, "value" : 110 }
  57. 57. Count of unique elements? > db.view.find(); { "user" : "001", "page" : "example.com/001", "time" : 2 } { "user" : "001", "page" : "example.com/002", "time" : 4 } { "user" : "002", "page" : "example.com/001", "time" : 6 } { "user" : "002", "page" : "example.com/002", "time" : 10 } { "user" : "002", "page" : "example.com/002", "time" : 12 } { "user" : "002", "page" : "example.com/003", "time" : 1 } { "user" : "003", "page" : "example.com/001", "time" : 42 } { "user" : "003", "page" : "example.com/001", "time" : 9 } # USER NAVIGATION SURVEY = FOR EACH USER # NUMBER OF UNIQUE PAGES # AVERAGE TIME ON A PAGE
  58. 58. Count of unique elements? > var map = function() { ... var accumulator = { ...... "numberOfViews": 1, ...... "visitedPages": {}, ...... "totalTime": 0 ...... }; ... accumulator["visitedPages"][this.page] = 1 ... accumulator["totalTime"] += this.time ... emit(this.user, accumulator) }
  59. 59. Count of unique elements? # EASY TO DEBUG > var aUser = db.view.findOne({ "user": "001" }) > var emit = function(id, value) { print(tojson(value)) } > map.call(aUser) { "numberOfViews" : 1, "visitedPages" : { "example.com/001" : 1 }, "totalTime" : 2 }
  60. 60. Count of unique elements? > var reduce = function(key, values) { ... var accumulator = { ...... "numberOfViews": 0, ...... "visitedPages": {}, ...... "totalTime": 0 ...... }; ... values.forEach(function(value) { ...... accumulator["numberOfViews"] += value["numberOfViews"] ...... accumulator["totalTime"] += value["totalTime"] ...... for (var page in value["visitedPages"]) { ......... if (accumulator["visitedPages"][page] === undefined) { ............ accumulator["visitedPages"][page] = 0 ......... } ......... accumulator["visitedPages"][page] += 1 ...... } ... }) ... return accumulator }
  61. 61. Count of unique elements? > db.view.mapReduce(map, reduce, { "out": "userNavigationSurvey" }) # NOT AS WE WANTED > db.userNavigationSurvey.find() { "_id" : "001", "value" : { "numberOfViews" : 2, "visitedPages" : { "example.com/001" : 1, "example.com/002" : 1 }, "totalTime" : 6 } } { "_id" : "002", "value" : { "numberOfViews" : 4, "visitedPages" : { ...
  62. 62. Count of unique elements? > var finalize = function(key, accumulator) { ... accumulator["averageTime"] = ...... accumulator["totalTime"] / accumulator["numberOfViews"] ... accumulator["numberOfUniquePages"] = 0 ... for (var page in accumulator["visitedPages"]) { ...... accumulator["numberOfUniquePages"] += 1 ... } ... delete accumulator["totalTime"] ... delete accumulator["numberOfViews"] ... delete accumulator["visitedPages"] ... return accumulator }
  63. 63. Count of unique elements? > db.view.mapReduce(map, reduce, { ... "finalize": finalize, ... "out": "userNavigationSurvey" }) > db.userNavigationSurvey.find() { "_id" : "001", "value" : { "averageTime" : 3, "numberOfUniquePages" : 2 } } { "_id" : "002", "value" : { "averageTime" : 7.25, "numberOfUniquePages" : 3 } } { "_id" : "003", "value" : { "averageTime" : 25.5, "numberOfUniquePages" : 1 } }
  64. 64. Count of unique elements by steps # STEP 1: CREATE THE BASE COLLECTION (WITHOUT UNIQUE ELEMENTS) > var mapBase = function() { ... emit(this.user, { ...... "numberOfViews": 1, ...... "totalTime": this.time ... }) } > var reduceBase = function(key, values) { ... var accumulator = { ...... "numberOfViews": 0, ...... "totalTime": 0 ... }; ... values.forEach(function(value) { ...... accumulator["numberOfViews"] += value["numberOfViews"] ...... accumulator["totalTime"] += value["totalTime"] ... }) ... return accumulator }
  65. 65. Count of unique elements by steps > var finalizeBase = function(key, accumulator) { ... accumulator["numberOfUniquePages"] = 0 ... accumulator["averageTime"] = ...... accumulator["totalTime"] / accumulator["numberOfViews"] ... delete accumulator["totalTime"] ... delete accumulator["numberOfViews"] ... return accumulator } > db.view.mapReduce(mapBase, reduceBase, { "finalize": finalizeBase, "out": "userNavigationSurvey" }) > db.userNavigationSurvey.find() { "_id" : "001", "value" : { "numberOfUniquePages" : 0, "averageTime" : 3 } } { "_id" : "002", "value" : { "numberOfUniquePages" : 0, "averageTime" : 7.25 } } { "_id" : "003", "value" : { "numberOfUniquePages" : 0, "averageTime" : 25.5 } }
  66. 66. Count of unique elements by steps # STEP 2: CREATE THE COLLECTION OF UNIQUE ELEMENTS > var mapUniquePages = function() { ... emit(this.user + "-" + this.page, { ...... "user": this.user, ...... "page": this.page ... }) } > var reduceUniquePages = function(key, values) { ... return values[0] } > db.view.mapReduce(mapUniquePages, reduceUniquePages { "out": "userUniquePages" })
  67. 67. Count of unique elements by steps > db.userUniquePages.find() { "_id" : "001-example.com/001", "value" : { "user" : "001", "page" : "example.com/001" } } { "_id" : "001-example.com/002", "value" : { "user" : "001", "page" : "example.com/002" } } { "_id" : "002-example.com/001", "value" : { "user" : "002", "page" : "example.com/001" } } { "_id" : "002-example.com/002", "value" : { "user" : "002", "page" : "example.com/002" } } { "_id" : "002-example.com/003", "value" : { "user" : "002", "page" : "example.com/003" } } { "_id" : "003-example.com/001", "value" : { "user" : "003", "page" : "example.com/001" } }
  68. 68. Count of unique elements by steps # STEP 3: UPDATE BASE COLLECTION WITH UNIQUE ELEMENTS COUNT > db.userUniquePages.find().forEach(function(userUniquePage) { db.userNavigationSurvey.update( { "_id": userUniquePage.value.user }, { $inc: { "value.numberOfUniquePages": 1 } } ) }) > db.userNavigationSurvey.find() { "_id" : "001", "value" : { "numberOfUniquePages" : 2, "averageTime" : 3 } } { "_id" : "002", "value" : { "numberOfUniquePages" : 3, "averageTime" : 7.25 } } { "_id" : "003", "value" : { "numberOfUniquePages" : 1, "averageTime" : 25.5 } }
  69. 69. Architecture Webmachine HTTP HTTP HTTP
  70. 70. Scalability Webmachine Nginx Master/Master Webmachine
  71. 71. User Account
  72. 72. Message
  73. 73. Received by <account> After <timestamp>? function(document) { if (document.from && document.to) { var key = [ document.to, document.timestamp ] var content = document._attachments["content"] var outline = { "id": document._id, "from": document.from, "timestamp": document.timestamp, "type": content["content_type"], "length": content["length"], } emit(key, outline) } }
  74. 74. Received by <account> After <timestamp>?
  75. 75. Received by <account> After <timestamp>? > curl -X GET ".../mercurio/_design/message/_view/received_after" { "total_rows":3, "offset":0, "rows": [ { "id": "ff35356344ee0e9928c212b52e36e6f3", "key": [ "gabriele", 1263655442 ], "value": { "id": "ff35356344ee0e9928c212b52e36e6f3", "from": "chiara", "timestamp": 1263655442, "type": "text/plain;charset=utf-8", "length": 16 } }, ... }
  76. 76. Results are ordered by Key { "key": [ "chiara", 126 ], "value": { "id": "ff35356344ee0e992...", "from": "gabriele" [ "chiara", 126 ] } == < } { "key": [ "chiara", 128 ], "value": { "id": "0deff99666425bacc...", [ "chiara", 128 ] "from": "gabriele" } < } { "key": [ "gabriele", 120 ], "value": { "id": "9842063609746c661...", [ "gabriele", 120 ] "from": "chiara" } }
  77. 77. Select with Key { "key": [ "chiara", 126 ], "value": { "id": "ff35356344ee0e992...", received_after? } "from": "gabriele" key=["chiara",126] } { "key": [ "chiara", 128 ], "value": { "id": "0deff99666425bacc...", "from": "gabriele" } } { "key": [ "gabriele", 120 ], "value": { "id": "9842063609746c661...", "from": "chiara" } }
  78. 78. Select with range of Keys { "key": [ "chiara", 126 ], received_after? "value": { "id": "ff35356344ee0e992...", "from": "gabriele" startkey=["chiara",126]& } } endkey=["gabriele",0] { "key": [ "chiara", 128 ], "value": { "id": "0deff99666425bacc...", "from": "gabriele" } } { "key": [ "gabriele", 120 ], "value": { "id": "9842063609746c661...", "from": "chiara" } }
  79. 79. Select with range of Keys { "key": [ "chiara", 126 ], "value": { "id": "ff35356344ee0e992...", "from": "gabriele" } } [ "chiara", [] ] { "key": [ "chiara", 128 ], "value": { "id": "0deff99666425bacc...", "from": "gabriele" } { "key": [ "chiara", [] ], } "value": { "id": "0deff99666425bacc...", "from": "gabriele" { "key": [ "gabriele", 120 ], } "value": { } "id": "9842063609746c661...", "from": "chiara" } }
  80. 80. Select with range of Keys { "key": [ "chiara", 126 ], received_after? "value": { "id": "ff35356344ee0e992...", "from": "gabriele" startkey=["chiara",126]& } } endkey=["chiara",[]] { "key": [ "chiara", 128 ], "value": { "id": "0deff99666425bacc...", "from": "gabriele" } } { "key": [ "gabriele", 120 ], "value": { "id": "9842063609746c661...", "from": "chiara" } }
  81. 81. Received by “chiara” After 126 { "key": [ "chiara", 126 ], received_after? "value": { "id": "ff35356344ee0e992...", "from": "gabriele" startkey=["chiara",127]& } } endkey=["chiara",[]] { "key": [ "chiara", 128 ], "value": { "id": "0deff99666425bacc...", "from": "gabriele" } } { "key": [ "gabriele", 120 ], "value": { "id": "9842063609746c661...", "from": "chiara" } }
  82. 82. Push Received Messages from Server Check for Messages _changes? received by <account.id> filter=message/received& after <timestamp> by=<account.id>& after=<timestamp> Send Message Save Document to <account.id> to: <account.id>
  83. 83. Push Received Messages from Server _changes?filter=message/received&by=<account.id>&after=<timestamp> function(document, request) { var receivedByMe = document.to === request.query.by var receivedAfterLastTime = document.receivedAt >= request.query.after return receivedByMe && receivedAfterLastTime }
  84. 84. Backoffice as Couch Application
  • SugiyamaYuichi

    Feb. 26, 2020
  • DiaaeBakri

    Feb. 5, 2019
  • ananp11

    Oct. 9, 2018
  • heinnge

    Aug. 24, 2018
  • NehaPriya2

    Jul. 24, 2018
  • anhtunguyen89

    Jul. 19, 2018
  • maroofshittu

    Jul. 17, 2018
  • bluehopper94

    Apr. 6, 2018
  • ya3qoob

    Jan. 21, 2018
  • SYEDAYESHA1

    Jul. 31, 2017
  • doninigor

    Apr. 17, 2017
  • NehaChugh4

    Apr. 13, 2017
  • taw3004

    Apr. 7, 2017
  • JaviLorbada

    Mar. 14, 2017
  • devmilan

    Feb. 28, 2017
  • FaisalArkan2

    Feb. 26, 2017
  • HenokVermeire

    Dec. 13, 2016
  • PatrickSmith55

    Dec. 11, 2016
  • tamilarya

    Nov. 9, 2016
  • ahmedzoghlami

    Oct. 25, 2016

Comparison by example between CouchDB and MongoDB

Vistos

Vistos totais

138.581

No Slideshare

0

De incorporações

0

Número de incorporações

7.099

Ações

Baixados

3.240

Compartilhados

0

Comentários

0

Curtir

301

×