计算响应中cloudant的行数 [英] count number of rows in cloudant in response

查看:59
本文介绍了计算响应中cloudant的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的地图reduce的反应如下。
现在我想计算响应中的行数,有人可以帮助我如何在cloudant中做到这一点吗?我需要一些响应,例如获取某个时期中不同的correlationid的总数。

I have below response from my map reduce . Now i want to count the number of rows in the response can any one help me how i can do it in cloudant? I need something in response like to get the total count of distinct correlationid in a period.

{
rows: [
{
key: [
"201705",
"aws-60826346-"
],
value: null
},
{
key: [
"201705",
"aws-60826348802-"
],
value: null
},
{
key: [
"201705",
"aws-las97628elb"
],
value: null
},
{
key: [
"201705",
"aws-ve-test"
],
value: null
},
{
key: [
"201705",
"aws-6032dcbce"
],
value: null
},
{
key: [
"201705",
"aws-60826348831d"
],
value: null
},
{
key: [
"201705",
"aws-608263488833926e"
],
value: null
},
{
key: [
"201705",
"aws-608263488a74f"
],
value: null
}
]
}


推荐答案

您需要实现一个稍微模糊的概念,称为链式地图-reduce来完成此任务。您无法在Cloudant管理GUI中执行此操作,因此必须手动编写设计文档。

You need to implement a slightly obscure concept called "chained map-reduce" to accomplish this. You can't do this in the Cloudant administrative GUI, so you'll have to write your design document by hand.

让您的map / reduce发出一个数组作为键。第一个数组元素为 month ,第二个元素为您的 correlationid 。该值应为1。然后将内置 _count 指定为reduce函数。

Have your map/reduce emit an array as the key. The 1st array element will be month and the second will be your correlationid. The value should be 1. Then specify the built-in _count as the reduce function.

现在,您需要添加链接部分。链接基本上涉及自动将映射/归约的结果复制到新数据库中。然后,您可以在该数据库上执行另一个映射/归约。从而创建了一个地图/减少链...

Now you need to add the chaining part. Chaining basically involves automatically copying the result of a map/reduce into a new database. You can then do another map/reduce on that database. Thereby creating a chain of map/reduces...

这里是一个使用您的示例的小型示例数据库:
https://rajsingh.cloudant.com/so44106569/_all_docs?include_docs=true&limit=200

Here's a tiny sample database using your example: https://rajsingh.cloudant.com/so44106569/_all_docs?include_docs=true&limit=200

以下是包含地图/缩小图的设计文档,以及用于更新新数据库的 dbcopy 命令(在这种情况下,称为 sob44106569 ),视图的结果称为 view

Here's the design document containing the map/reduce, along with the dbcopy command that updates a new database (in this case called sob44106569) with the results of the view called view:

{
    "_id": "_design/ddoc",
    "_rev": "11-88ff7d977dfff81a05c50b13d854a78f",
    "options": {
      "epi": {
        "dbcopy":
          {
            "view": "sob44106569"
          }
      }
    },
    "language": "javascript",
    "views": {
      "view": {
        "reduce": "_count",
        "map": "function (doc) {\n  emit([doc.month, doc.machine], 1);\n}"
      }
    }
}

这是map函数(不减少)的结果,显示10行。请注意,有两个文档的月份为 201705 和机器 aws-6032dcbce
> https://rajsingh.cloudant.com/so44106569/_design/ddoc / _view / view?limit = 200& reduce = false

Here's the result of the map function (no reduce) showing 10 rows. Notice that there are two documents with month 201705 and machine aws-6032dcbce: https://rajsingh.cloudant.com/so44106569/_design/ddoc/_view/view?limit=200&reduce=false

如果您只是执行内置的 _count 在此视图上将 group_level = 1 减少,您会得到201705的 9 值,这对您而言是错误的,因为您只想算一次 aws-6032dcbce ,即使它两次出现在数据中:

If you just do the built-in _count reduce on this view at group_level=1, you'll get a value of 9 for 201705, which is wrong for your purposes because you want to count that aws-6032dcbce only once, even though it shows up in the data twice:

< a href = https://rajsingh.cloudant.com/so44106569/_design/ddoc/_view/view?limit=200&reduce=true&group=true&group_level=1 rel = nofollow noreferrer> https:/ /rajsingh.cloudant.com/so44106569/_design/ddoc/_view/view?limit=200&reduce=true&group=true&group_level=1

因此,让我们快速浏览一下地图/缩小处的k在 group_level = 2 。这是复制到新数据库的内容:

So let's take a quick look at the map/reduce at group_level=2. This is what gets copied to the new database:

https://rajsingh.cloudant.com/so44106569/_design/ddoc/_view/view?limit=200&reduce=true& ; group = true& group_level = 2

在这里您看到 aws-6032dcbce 仅显示一次(但value = 2),所以这是一个有用的视图。 Map / reduce的 dbcopy 部分基于此视图创建数据库 sob44106569 。让我们看一下:
https://rajsingh.cloudant.com/sob44106569 / _all_docs?include_docs = true

Here you see that aws-6032dcbce only shows up once (but with value=2), so this is a useful view. The dbcopy part of our map/reduce creates the database sob44106569 based on this view. Let's look at that: https://rajsingh.cloudant.com/sob44106569/_all_docs?include_docs=true

现在我们可以在该数据库上运行非常简单的map / reduce,再次发出月份和机器信息(现在它们在数组,因此具有不同的名称),但是这次机器的重复值已经被减少了。

Now we can run a very simple map/reduce on that database, emitting the month and machine again (now they are in an array so have different names), but this time the repeated values for machine have already been "reduced" away.

function (doc) {
  if (doc.key && doc.key.length == 2 )
    emit(doc.key[0], doc.key[1]);
}

最后是不同机器的数量。现在我们终于可以看到201705的期望值 8

And finally here's the count of distinct "machines". Now we can finally see the desired value of 8 for 201705.

https://rajsingh.cloudant.com/sob44106569/_design/views/_view / counted?limit = 200& reduce = true& group = true& group_level = 1

响应

{
  "rows": [
    {
      "key": "201705",
      "value": 8
    },
    {
      "key": "201706",
      "value": 1
    }
  ]
}

这篇关于计算响应中cloudant的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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