使用map-reduce(Couchbase)编写一个简单的分组 [英] Writing a simple group by with map-reduce (Couchbase)

查看:76
本文介绍了使用map-reduce(Couchbase)编写一个简单的分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是整个map-reduce概念的新手,我正在尝试执行一个简单的map-reduce功能.

I'm new to the whole map-reduce concept, and i'm trying to perform a simple map-reduce function.

我目前正在使用Couchbase服务器作为NoSQL数据库.

I'm currently working with Couchbase server as my NoSQL db.

我想获取所有类型的列表:

I want to get a list of all my types:

key: 1, value: null
key: 2, value: null
key: 3, value: null

这是我的文件:

{
   "type": "1",
   "value": "1"
}

{
   "type": "2",
   "value": "2"
}

{
   "type": "3",
   "value": "3"
}

{
   "type": "1",
   "value": "4"
}

我一直试图做的是: 编写地图功能:

What I've been trying to do is: Write a map function:

function (doc, meta) {
  emit(doc.type, 0);
}

使用内置的reduce函数:

Using built-in reduce function:

_count

但是我没有得到预期的结果.

But i'm not getting the expected result.

如何获取所有类型?

更新

请注意,这些类型是不同的文档,而且我知道reduce可以在文档上工作,并且不能在文档外部执行.

Please notice that the types are different documents, and I know that reduce works on a document and doesn't executes outside of it.

推荐答案

默认情况下,它将减少所有键组.您想要的功能称为group_level:

By default it will reduce all key groups. The feature you want is called group_level:

这等效于reduce=true

~ $ curl 'http://localhost:8092/so/_design/dev_test/_view/test?group_level=0'
{"rows":[
{"key":null,"value":4}
]
}

但是,这是如何通过密钥的第一级进行还原的方法

But here is how you can get reduction by the first level of the key

~ $ curl 'http://localhost:8092/so/_design/dev_test/_view/test?group_level=1'
{"rows":[
{"key":"1","value":2},
{"key":"2","value":1},
{"key":"3","value":1}
]
}

也有关于此的博客文章: http://blog. couchbase.com/understanding-grouplevel-view-queries-compound-keys

There is also blog post about this: http://blog.couchbase.com/understanding-grouplevel-view-queries-compound-keys

couchbase管理控制台中有一个适当的选项:

There is appropriate option in couchbase admin console:

这篇关于使用map-reduce(Couchbase)编写一个简单的分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆