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
}
]
}
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.
Have your map/reduce emit an array as the key. The 1st array element will be
month
and the second will be yourcorrelationid
. 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...
Here's a tiny sample database using your example: https://rajsingh.cloudant.com/so44106569/_all_docs?include_docs=true&limit=200
Here's the design document containing the map/reduce, along with the
dbcopy
command that updates a new database (in this case calledsob44106569
) with the results of the view calledview
:Here's the result of the map function (no reduce) showing 10 rows. Notice that there are two documents with month
201705
and machineaws-6032dcbce
: https://rajsingh.cloudant.com/so44106569/_design/ddoc/_view/view?limit=200&reduce=falseIf you just do the built-in
_count
reduce on this view atgroup_level=1
, you'll get a value of 9 for 201705, which is wrong for your purposes because you want to count thataws-6032dcbce
only once, even though it shows up in the data twice:https://rajsingh.cloudant.com/so44106569/_design/ddoc/_view/view?limit=200&reduce=true&group=true&group_level=1
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
Here you see that
aws-6032dcbce
only shows up once (but with value=2), so this is a useful view. Thedbcopy
part of our map/reduce creates the databasesob44106569
based on this view. Let's look at that: https://rajsingh.cloudant.com/sob44106569/_all_docs?include_docs=trueNow 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.
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
response:
Emit 1 instead of null and use the built-in reducer _count.