CouchDB Day NYC 2017: MapReduce Views
-
Upload
ibm-cloud-data-services -
Category
Software
-
view
17 -
download
0
Transcript of CouchDB Day NYC 2017: MapReduce Views
Lab: Map/Reduce Views (30 minutes)
Bradley Holt, Developer AdvocateThursday, February 9, 2017
CouchDB Developer Day
@BradleyHolt
Prerequisite: Replication Lab
@BradleyHolt
Key Concepts
Design documents Views Map functions Reduce functions
@BradleyHolt
http-console
$ npm install http-console -g
@BradleyHolthttps://github.com/cloudhead/http-console
http-console
$ npm install http-console -g$ http-console 127.0.0.1:5984
@BradleyHolthttps://github.com/cloudhead/http-console
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
Connecting to CouchDB 2.0
$ http-console root:[email protected]:5984 --json> http-console 0.6.3> Welcome, enter .help if you're lost.> Connecting to 127.0.0.1 on port 5984.
@BradleyHolt
Connecting to IBM Cloudant
$ http-console https://bradley-holt:[email protected] --json
@BradleyHolt
Connecting to IBM Cloudant
$ http-console https://bradley-holt:[email protected] --json> http-console 0.6.3> Welcome, enter .help if you're lost.> Connecting to bradley-holt.cloudant.com on port 443.
@BradleyHolt
Create a Design Document
/>
@BradleyHolt
Create a Design Document
/> /kittens
@BradleyHolt
Create a Design Document
/kittens>
@BradleyHolt
Create a Design Document
/kittens> PUT /_design/kittens
@BradleyHolt
Create a Design Document
/kittens> PUT /_design/kittens... { "views": { "by_age": { "map": "function(doc) { emit(doc.age_weeks) }" } } }
@BradleyHolt
Create a Design Document
/kittens> PUT /_design/kittens... { "views": { "by_age": { "map": "function(doc) { emit(doc.age_weeks) }" } } }HTTP/1.1 201 CreatedContent-Type: application/json
{ ok: true, id: '_design/kittens', rev: '1-c38a0039638b950f64e02c51fe2e9229'}
@BradleyHolt
Get the Design Document
/kittens>
@BradleyHolt
Get the Design Document
/kittens> GET /_design/kittens
@BradleyHolt
Get the Design Document
/kittens> GET /_design/kittensHTTP/1.1 200 OKContent-Type: application/json
{ _id: '_design/kittens', _rev: '1-c38a0039638b950f64e02c51fe2e9229', views: { by_age: { map: 'function(doc) { emit(doc.age_weeks) }' } }}
@BradleyHolt
Query a View
/kittens>
@BradleyHolt
Query a View
/kittens> GET /_design/kittens/_view/by_age
@BradleyHolt
Query a View
/kittens> GET /_design/kittens/_view/by_ageHTTP/1.1 200 OKContent-Type: application/json
{ total_rows: 5, offset: 0, rows: [ { id: 'smokey', key: 3, value: null }, { id: 'tiger', key: 7, value: null }, { id: 'daisy', key: 9, value: null }, { id: 'mittens', key: 10, value: null }, { id: 'lily', key: 16, value: null } ]}
@BradleyHolt
Query a View with Start Key and End Key
/kittens>
@BradleyHolt
Query a View with Start Key and End Key
/kittens> GET /_design/kittens/_view/by_age?startkey=7&endkey=10
@BradleyHolt
Query a View with Start Key and End Key
/kittens> GET /_design/kittens/_view/by_age?startkey=7&endkey=10HTTP/1.1 200 OKContent-Type: application/json
{ total_rows: 5, offset: 1, rows: [ { id: 'tiger', key: 7, value: null }, { id: 'daisy', key: 9, value: null }, { id: 'mittens', key: 10, value: null } ]}
@BradleyHolt
Add a Reduce Function
/kittens>
@BradleyHolt
Add a Reduce Function
/kittens> GET /_design/kittens
@BradleyHolt
Add a Reduce Function
/kittens> GET /_design/kittensHTTP/1.1 200 OKContent-Type: application/json
{ _id: '_design/kittens', _rev: '1-c38a0039638b950f64e02c51fe2e9229', views: { by_age: { map: 'function(doc) { emit(doc.age_weeks) }' } }}
@BradleyHolt
Add a Reduce Function
/kittens>
@BradleyHolt
Add a Reduce Function
/kittens> PUT /_design/kittens?rev=1-c38a0039638b950f64e02c51fe2e9229
@BradleyHolt
Add a Reduce Function
/kittens> PUT /_design/kittens?rev=1-c38a0039638b950f64e02c51fe2e9229... { "views": { "by_age": { "map": "function(doc) { emit(doc.age_weeks, doc.weight_kilograms) }", "reduce": "_stats" } } }
@BradleyHolt
Add a Reduce Function
/kittens> PUT /_design/kittens?rev=1-c38a0039638b950f64e02c51fe2e9229... { "views": { "by_age": { "map": "function(doc) { emit(doc.age_weeks, doc.weight_kilograms) }", "reduce": "_stats" } } }HTTP/1.1 201 CreatedContent-Type: application/json
{ ok: true, id: '_design/kittens', rev: '2-a64e3539068221e7b4eaa94917faed5f'}
@BradleyHolt
Query a View with a Reduce Function
/kittens>
@BradleyHolt
Query a View with a Reduce Function
/kittens> GET /_design/kittens/_view/by_age
@BradleyHolt
Query a View with a Reduce Function
/kittens> GET /_design/kittens/_view/by_ageHTTP/1.1 200 OKContent-Type: application/json
{ rows: [ { key: null, value: { sum: 4.8919999999999995, count: 5, min: 0.403, max: 1.95, sumsqr: 6.15185 } } ]}
@BradleyHolt
Try It On Your Own
1. Create an view of kittens by weight_kilograms, using age_weeks as the value and using a _stats reduce function
2. Find kittens weighing less than 1 kilogram (use the reduce=false parameter)
3. Find the stats of kittens weighing less than 1 kilogram
@BradleyHolt
Lab: Map/Reduce ViewsCouchDB Developer Day
@BradleyHolt