Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CouchDB Day NYC 2017: Introduction to CouchDB 2.0

418 views

Published on

For more than 10 years, developers have relied on Apache(R) CouchDB(TM) - a versatile and highly scalable open source database - to build apps for web, mobile and IoT platforms.

The release of CouchDB 2.0 in 2016 has generated even more interest in the freely available JSON database, which now includes clustering capabilities contributed from IBM Cloudant for high availability and performance.

Published in: Software
  • Be the first to comment

CouchDB Day NYC 2017: Introduction to CouchDB 2.0

  1. 1. Apache CouchDB Developer Day Bradley Holt, Developer Advocate Thursday, February 9, 2017 Introducing Apache CouchDB 2.0 @BradleyHolt
  2. 2. @BradleyHolt
  3. 3. IBM Cloud Data Services Open for Data A comprehensive portfolio of open source data services
  4. 4. What is Apache CouchDB?
  5. 5. Document Database Apache CouchDB is a JSON document database
  6. 6. HTTP API Apache CouchDB is accessed through an HTTP API @BradleyHolt GET / GET /db/docPOST /db DELETE /db/doc PUT /db/doc PUT /db
  7. 7. Peer-to-Peer Replication @BradleyHolt Cloudant SyncCouchDB PouchDB CouchDB Replication Protocol CouchDB
  8. 8. What's new in CouchDB 2.0?
  9. 9. CouchDB 1.x: Standalone @BradleyHolt CouchDB
  10. 10. CouchDB 2.0: Clusterered @BradleyHolt CouchDB node1 CouchDB node2 CouchDB node3 haproxy
  11. 11. Shards and Replicas  q=8 – Number of shards – One or more shards per node – Cannot have more nodes than shards  r=2 – Read quorum  w=2 – Write quorum  n=3 – Number of replicas of every document @BradleyHolt CouchDB node1 CouchDB node2 CouchDB node3
  12. 12. Mango  Declarative indexes  MongoDB-style query language  You can still use map/reduce views @BradleyHolt
  13. 13. couch_peruser @BradleyHolt userdb-51d055 userdb-d76846 userdb-905cec userdb-adc95b userdb-c082f2 userdb-730bba userdb-c3d3e5 userdb-da userdb-a1ec1f db-85bcfe userdb-85a327userdb-9b9aba
  14. 14. What else is new?  Improved database compaction  Faster index updates  New _bulk_get endpoint for optimized replication  View-based filters in _changes feed  Filter _changes feed with _doc_ids  _all_docs and _changes will support attachments=true @BradleyHolt
  15. 15. Installing CouchDB 2.0
  16. 16. Apache CouchDB 2.0 Sandbox Cluster Try Apache CouchDB 2.0 on an IBM Cloudant sandbox cluster @BradleyHolt
  17. 17. http-console
  18. 18. http-console $ npm install http-console -g @BradleyHolthttps://github.com/cloudhead/http-console
  19. 19. http-console $ npm install http-console -g $ http-console 127.0.0.1:5984 @BradleyHolthttps://github.com/cloudhead/http-console
  20. 20. http-console $ npm install http-console -g $ http-console 127.0.0.1:5984 > http-console 0.6.3 > Welcome, enter .help if you're lost. > Connecting to 127.0.0.1 on port 5984. @BradleyHolthttps://github.com/cloudhead/http-console
  21. 21. http-console http://127.0.0.1:5984/> @BradleyHolthttps://github.com/cloudhead/http-console
  22. 22. http-console http://127.0.0.1:5984/> GET / @BradleyHolthttps://github.com/cloudhead/http-console
  23. 23. http-console http://127.0.0.1:5984/> GET / HTTP/1.1 200 OK Content-Type: text/plain; charset=utf-8 { couchdb: 'Welcome', version: '9afa6a0', vendor: { name: 'The Apache Software Foundation' } } @BradleyHolthttps://github.com/cloudhead/http-console
  24. 24. http-console http://127.0.0.1:5984/> @BradleyHolthttps://github.com/cloudhead/http-console
  25. 25. http-console http://127.0.0.1:5984/> .q @BradleyHolthttps://github.com/cloudhead/http-console
  26. 26. http-console $ http-console https://bradley-holt.cloudant.com @BradleyHolthttps://github.com/cloudhead/http-console
  27. 27. http-console $ http-console https://bradley-holt.cloudant.com > http-console 0.6.3 > Welcome, enter .help if you're lost. > Connecting to bradley-holt.cloudant.com on port 443. @BradleyHolthttps://github.com/cloudhead/http-console
  28. 28. http-console https://bradley-holt.cloudant.com:443/> @BradleyHolthttps://github.com/cloudhead/http-console
  29. 29. http-console https://bradley-holt.cloudant.com:443/> GET / @BradleyHolthttps://github.com/cloudhead/http-console
  30. 30. http-console https://bradley-holt.cloudant.com:443/> GET / HTTP/1.1 200 OK Content-Type: application/json { couchdb: 'Welcome', version: '9d28c57', vendor: { name: 'IBM Cloudant', version: '5331', variant: 'paas' }, features: [ 'geo' ] } @BradleyHolthttps://github.com/cloudhead/http-console
  31. 31. http-console https://bradley-holt.cloudant.com:443/> @BradleyHolthttps://github.com/cloudhead/http-console
  32. 32. http-console https://bradley-holt.cloudant.com:443/> .q @BradleyHolthttps://github.com/cloudhead/http-console
  33. 33. Exploring the CouchDB API
  34. 34. Connecting to CouchDB 2.0 $ http-console root:passw0rd@127.0.0.1:5984 --json @BradleyHolt
  35. 35. Connecting to CouchDB 2.0 $ http-console root:passw0rd@127.0.0.1:5984 --json > http-console 0.6.3 > Welcome, enter .help if you're lost. > Connecting to 127.0.0.1 on port 5984. @BradleyHolt
  36. 36. Connecting to IBM Cloudant $ http-console https://bradley-holt:passw0rd@bradley-holt.cloudant.com --json @BradleyHolt
  37. 37. Connecting to IBM Cloudant $ http-console https://bradley-holt:passw0rd@bradley-holt.cloudant.com --json > http-console 0.6.3 > Welcome, enter .help if you're lost. > Connecting to bradley-holt.cloudant.com on port 443. @BradleyHolt
  38. 38. Request Headers /> @BradleyHolt
  39. 39. Request Headers /> .headers @BradleyHolt
  40. 40. Request Headers /> .headers Accept: application/json Content-Type: application/json Authorization: Basic cm9vdDorMi95N3Y2aA== Host: 127.0.0.1 @BradleyHolt
  41. 41. PUT a Database /> @BradleyHolt
  42. 42. PUT a Database /> PUT /kittens @BradleyHolt
  43. 43. PUT a Database /> PUT /kittens ... @BradleyHolt
  44. 44. PUT a Database /> PUT /kittens ... HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens { ok: true } @BradleyHolt
  45. 45. PUT a Database Again /> @BradleyHolt
  46. 46. PUT a Database Again /> PUT /kittens @BradleyHolt
  47. 47. PUT a Database Again /> PUT /kittens ... @BradleyHolt
  48. 48. PUT a Database Again /> PUT /kittens ... HTTP/1.1 412 Precondition Failed Content-Type: application/json { error: 'file_exists', reason: 'The database could not be created, the file already exists.' } @BradleyHolt
  49. 49. POST a Document /> @BradleyHolt
  50. 50. POST a Document /> /kittens @BradleyHolt
  51. 51. POST a Document /kittens> @BradleyHolt
  52. 52. POST a Document /kittens> POST / @BradleyHolt
  53. 53. POST a Document /kittens> POST / ... @BradleyHolt
  54. 54. POST a Document /kittens> POST / ... { "_id": "mittens", "age_weeks": 10, "weight_kilograms": 0.997 } @BradleyHolt
  55. 55. POST a Document /kittens> POST / ... { "_id": "mittens", "age_weeks": 10, "weight_kilograms": 0.997 } HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens/mittens { ok: true, id: 'mittens', rev: '1-e665a40d9ea9711c983e907f0b0b6e8a' } @BradleyHolt
  56. 56. GET All Documents /kittens> @BradleyHolt
  57. 57. GET All Documents /kittens> GET /_all_docs @BradleyHolt
  58. 58. GET All Documents /kittens> GET /_all_docs HTTP/1.1 200 OK Content-Type: application/json Etag: "92afa0f309a9fd9f140cd31ff5000b5c" { total_rows: 1, offset: 0, rows: [ { id: 'mittens', key: 'mittens', value: { rev: '1-e665a40d9ea9711c983e907f0b0b6e8a' } } ] } @BradleyHolt
  59. 59. GET a Document /kittens> @BradleyHolt
  60. 60. GET a Document /kittens> GET /mittens @BradleyHolt
  61. 61. GET a Document /kittens> GET /mittens HTTP/1.1 200 OK Content-Type: application/json Etag: "1-e665a40d9ea9711c983e907f0b0b6e8a" { _id: 'mittens', _rev: '1-e665a40d9ea9711c983e907f0b0b6e8a', age_weeks: 10, weight_kilograms: 0.997 } @BradleyHolt
  62. 62. PUT an Attachment /kittens> @BradleyHolt
  63. 63. PUT an Attachment /kittens> .headers @BradleyHolt
  64. 64. PUT an Attachment /kittens> .headers Accept: application/json Content-Type: application/json Authorization: Basic cm9vdDorMi95N3Y2aA== Host: 127.0.0.1 @BradleyHolt
  65. 65. PUT an Attachment /kittens> .headers Accept: application/json Content-Type: application/json Authorization: Basic cm9vdDorMi95N3Y2aA== Host: 127.0.0.1 /kittens> Content-Type: image/gif @BradleyHolt
  66. 66. PUT an Attachment /kittens> @BradleyHolt
  67. 67. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a @BradleyHolt
  68. 68. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... @BradleyHolt
  69. 69. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= @BradleyHolt
  70. 70. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens/mittens/photo { ok: true, id: 'mittens', rev: '2-d858e51453a5785bafe517b7eddc5a98' } @BradleyHolt
  71. 71. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens/mittens/photo { ok: true, id: 'mittens', rev: '2-d858e51453a5785bafe517b7eddc5a98' } /kittens> @BradleyHolt
  72. 72. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens/mittens/photo { ok: true, id: 'mittens', rev: '2-d858e51453a5785bafe517b7eddc5a98' } /kittens> Content-Type: application/json @BradleyHolt
  73. 73. GET an Attachment /kittens> @BradleyHolt
  74. 74. GET an Attachment /kittens> GET /mittens/photo @BradleyHolt
  75. 75. GET an Attachment /kittens> GET /mittens/photo HTTP/1.1 200 OK Content-Type: image/gif Etag: "UsqjdPnY6ApD2ENFOglFHg==" R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= @BradleyHolt
  76. 76. Conditional Caching /kittens> @BradleyHolt
  77. 77. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" @BradleyHolt
  78. 78. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> @BradleyHolt
  79. 79. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> GET /mittens/photo @BradleyHolt
  80. 80. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> GET /mittens/photo HTTP/1.1 304 Not Modified @BradleyHolt
  81. 81. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> GET /mittens/photo HTTP/1.1 304 Not Modified /kittens> @BradleyHolt
  82. 82. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> GET /mittens/photo HTTP/1.1 304 Not Modified /kittens> If-None-Match: @BradleyHolt
  83. 83. DELETE a Document /kittens> @BradleyHolt
  84. 84. DELETE a Document /kittens> DELETE /mittens @BradleyHolt
  85. 85. DELETE a Document /kittens> DELETE /mittens HTTP/1.1 409 Conflict Content-Type: application/json { error: 'conflict', reason: 'Document update conflict.' } @BradleyHolt
  86. 86. DELETE a Document /kittens> @BradleyHolt
  87. 87. DELETE a Document /kittens> HEAD /mittens @BradleyHolt
  88. 88. DELETE a Document /kittens> HEAD /mittens HTTP/1.1 200 OK Etag: "2-d858e51453a5785bafe517b7eddc5a98" @BradleyHolt
  89. 89. DELETE a Document /kittens> @BradleyHolt
  90. 90. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" @BradleyHolt
  91. 91. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> @BradleyHolt
  92. 92. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> DELETE /mittens @BradleyHolt
  93. 93. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> DELETE /mittens HTTP/1.1 200 OK Content-Type: application/json { ok: true, id: 'mittens', rev: '3-d0780627ddff7a7f536fe273100cec41' } @BradleyHolt
  94. 94. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> DELETE /mittens HTTP/1.1 200 OK Content-Type: application/json { ok: true, id: 'mittens', rev: '3-d0780627ddff7a7f536fe273100cec41' } /kittens> @BradleyHolt
  95. 95. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> DELETE /mittens HTTP/1.1 200 OK Content-Type: application/json { ok: true, id: 'mittens', rev: '3-d0780627ddff7a7f536fe273100cec41' } /kittens> If-Match: @BradleyHolt
  96. 96. Replication /kittens> @BradleyHolt
  97. 97. Replication /kittens> .. @BradleyHolt
  98. 98. Replication /kittens> .. /> @BradleyHolt
  99. 99. Replication /kittens> .. /> DELETE /kittens @BradleyHolt
  100. 100. Replication /kittens> .. /> DELETE /kittens HTTP/1.1 200 OK Content-Type: application/json { ok: true } @BradleyHolt
  101. 101. Replication /> @BradleyHolt
  102. 102. Replication /> POST /_replicate @BradleyHolt
  103. 103. Replication /> POST /_replicate ... @BradleyHolt
  104. 104. Replication /> POST /_replicate ... { "create_target": true, "source": "https://bradley-holt.cloudant.com/kittens", "target": "kittens" } @BradleyHolt
  105. 105. Replication /> POST /_replicate ... { "create_target": true, "source": "https://bradley-holt.cloudant.com/kittens", "target": "kittens" } HTTP/1.1 200 OK Content-Type: application/json { ok: true, session_id: '9f7bd286a0001ece5bf0bf65dd83c5ab', source_last_seq: '5- g1AAAAFDeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtVLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1 YiA6oWQ9xakhyAZFI9SFcGcyJzLpDHbplmZmxkaULYBKIdlscCJBkagBTQov2kuA- i8wBEJ9iNTGA3mhoampmnGBA2JQsA1LtoyA', … } @BradleyHolt
  106. 106. Mango /> @BradleyHolt
  107. 107. Mango /> /kittens @BradleyHolt
  108. 108. Mango /kittens> @BradleyHolt
  109. 109. Mango /kittens> POST /_index @BradleyHolt
  110. 110. Mango /kittens> POST /_index ... @BradleyHolt
  111. 111. Mango /kittens> POST /_index ... { "index": { "fields": [ "age_weeks", "weight_kilograms" ] } } @BradleyHolt
  112. 112. Mango /kittens> POST /_index ... { "index": { "fields": [ "age_weeks", "weight_kilograms" ] } } HTTP/1.1 200 OK Content-Type: application/json { result: 'created', id: '_design/e19dde7f518129a966ebe072edc66be88d54e694', name: 'e19dde7f518129a966ebe072edc66be88d54e694' } @BradleyHolt
  113. 113. @BradleyHolt
  114. 114. Mango /kittens> @BradleyHolt
  115. 115. Mango /kittens> POST /_find @BradleyHolt
  116. 116. Mango /kittens> POST /_find ... @BradleyHolt
  117. 117. Mango /kittens> POST /_find ... { "selector": { "age_weeks": { "$gte": 7, "$lte": 10 } }, "fields": [ "_id", "age_weeks" ] } @BradleyHolt
  118. 118. Mango /kittens> POST /_find ... { "selector": { "age_weeks": { "$gte": 7, "$lte": 10 } }, "fields": [ "_id", "age_weeks" ] } HTTP/1.1 200 OK Content-Type: application/json { docs: [ { _id: 'tiger', age_weeks: 7 }, { _id: 'daisy', age_weeks: 9 }, { _id: 'mittens', age_weeks: 10 } ] } @BradleyHolt
  119. 119. @BradleyHolt
  120. 120. Mango /kittens> @BradleyHolt
  121. 121. Mango /kittens> POST /_find @BradleyHolt
  122. 122. Mango /kittens> POST /_find ... @BradleyHolt
  123. 123. Mango /kittens> POST /_find ... { "selector": { "age_weeks": { "$gte": 0 }, "weight_kilograms": { "$gte": 0.5, "$lte": 1 } }, "fields": [ "_id", "weight_kilograms" ] } @BradleyHolt
  124. 124. Mango /kittens> POST /_find ... { "selector": { "age_weeks": { "$gte": 0 }, "weight_kilograms": { "$gte": 0.5, "$lte": 1 } }, "fields": [ "_id", "weight_kilograms" ] } HTTP/1.1 200 OK Content-Type: application/json { docs: [ { _id: 'tiger', weight_kilograms: 0.726 }, { _id: 'daisy', weight_kilograms: 0.816 }, { _id: 'mittens', weight_kilograms: 0.997 } ] } @BradleyHolt
  125. 125. @BradleyHolt
  126. 126. Contributing
  127. 127. Offline First
  128. 128. Offline First Lack of connectivity is not an error condition.
  129. 129. Cloudant Sync
  130. 130. Cloudant FoodTracker
  131. 131. Cloudant FoodTracker
  132. 132. Cloudant Location Tracker
  133. 133. Offline Camp Berlin April 28th - May 1st, Berlin, Germany
  134. 134. offlinefirst.org/camp
  135. 135. Image Credits  paper by malik, on Flickr <https://flic.kr/p/aZjTXv>  person by Tim Morgan, on Flickr <https://flic.kr/p/7DSF5>  database by Tim Morgan, on Flickr <https://flic.kr/p/7DUk5>  gear by Tim Morgan, on Flickr <https://flic.kr/p/7DSF1>  Mango with section on a white background by bangdoll, on Flickr <https://flic.kr/p/9CBP2h> @BradleyHolt
  136. 136. Questions? @BradleyHolt

×