未加星标

MapReduce views

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
Introduction

Frank van der Linden posted he made an update to his Cloudant connector plugin , so from now it should be possible to manage design documents.

To become more familiar with couchDB’s design documents I canrecommend reading ‘ Writing and Querying MapReduce Views in CouchDB ‘.

Design documents

Design documentsyou mostly manage in CouchDB / Cloudant are documents that describe views of documents. Great if you can manage them via http and not as in IBM Notes via theDomino Designer client:wink:

You could store other documents in your couchDb which are part of the design of your application (e.g. html, css, js files but that is another story).

Design documents are nothing more than JSON documents which are stored under the convention: must start with the _design/ pattern followed by the identifier e.g.: _design/default.

MapReduce

Views in couchdb are based on the MapReduce principle:

A MapReduce program is composed of a Map () procedure that performs filtering and sorting (such as sorting students by first name into queues, one queue for each name) and a Reduce () procedure that performs a summary operation (such as counting the number of students in each queue, yielding name frequencies).

This video explains it by usingplaying cards .

Mapping

The filtering and sorting are defined by a javascript function. You can state conditions in the function to return the documents you are interested in e.g.:

function(doc) {

if (doc.labels) {

for (var idx in doc.labels) {

if (doc.labels[idx] === “organic”) {

emit(doc._id, doc.name);

}

}

}

}

//docs have to have the field doc.labels and only the id and name from the ones that have the label organic are returned

Reducing

Reducing is optional and results in a summary operation. The options in couchDb are:

_sum _count _stats

Count is probably the most common function to use. Notice that the returned key value might be null if values appear in multiple documents. The key is no longer unique and therefor null.

{

“rows”: [{

“key”: null,

“value”: 7

}]

}

If you apply the group parameter in the requested url the values will be grouped so the response could look like:

{

“rows”: [{

“key”: “appel”,

“value”: 3

}, {

“key”: “banaan”,

“value”: 2

}, {

“key”: “peer”,

“value”: 2

}

]

}

Sum works only on numeric values (of course) so if your mapping functions looks like:

function(doc) {

if (doc.fruits) {

for (var i in doc.fruits) {

emit(doc.fruits[i], doc.calories);

}

}

}

with the _sum reduce option the response could look like:

{

“rows”: [{

“key”: “appel”,

“value”: 720

}, {

“key”: “banaan”,

“value”: 1368

}, {

“key”: “peer”,

“value”: 320

}]

}

The _stats optionreturns a JSON object containing the sum, count, minimum, maximum, and sum over all square roots of mapped values. So for the mapping function above the response could be:

{

“rows”: [{

“key”: “appel”,

“value”: {

“sum”: 720,

“count”: 2,

“min”: 272,

“max”: 448,

“sumsqr”: 274688

}

}, {

“key”: “banaan”,

“value”: {

“sum”: 1368,

“count”: 3,

“min”: 272,

“max”: 648,

“sumsqr”: 694592

}

}, {

“key”: “peer”,

“value”: {

“sum”: 720,

“count”: 2,

“min”: 272,

“max”: 448,

“sumsqr”: 274688

}

}]

}

With this knowledge fresh in mind you can use them in your view(s) setup. Remember the design documents can be managed via CRUD operations, e.g. on addresshttp://localhost:5984/veganstore/_design/default

{

“_id”: “_design/default”,

“language”: “javascript”,

“views”: {

“store”: {

“map”: “function(doc) { if (doc.label) { emit(doc.label, doc.origin); }}”,

“reduce”: “_stats”

}

}

}

In one design document you can define multiple views…

You can query your views by a set of parameters :

conflicts ( boolean ) Includes conflicts information in response. Ignored if include_docs isn’t true . Default is false descending ( boolean ) Return the documents in descending by key order. Default is false endkey ( json ) Stop returning records when the specified key is reached. Optional end_key ( json ) Alias for endkey param endkey_docid ( string ) Stop returning records when the specified document ID is reached. Requires endkey to be specified for this to have any effect. Optional end_key_doc_id ( string ) Alias for endkey_docid param group ( boolean ) Group the results using the reduce function to a group or single row. Default is false group_level ( number ) Specify the group level to be used. Optional include_docs ( boolean ) Include the associated document with each row. Default is false . attachments ( boolean ) Include the Base64-encoded content of attachments in the documents that are included if include_docs is true . Ignored if include_docs isn’t true . Default is false . att_encoding_info ( boolean ) Include encoding information in attachment stubs if include_docs is true and the particular attachment is compressed. Ignored if include_docs isn’t true . Default is false . inclusive_end ( boolean ) Specifies whether the specified end key should be included in the result. Default is true key ( json

本文数据库(综合)相关术语:系统安全软件

主题: MapReduceCouchDBJavaScriptJavaIBM
分页:12
转载请注明
本文标题:MapReduce views
本站链接:http://www.codesec.net/view/484077.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(31)