small medium large xlarge

Generic-user-small
10 Jul 2012, 17:58
Jeff Ostrin (2 posts)

I am working on Riak day 2 and trying to map/reduce all the rooms capacities (page 69).

I am using riak 1.0.2 on ubuntu 12.04.

Here is my curl POST:

{

  "inputs": [
              ["rooms","101"], ["rooms","102"], ["rooms","103"], ["rooms","104"], ["rooms","105"],
              ["rooms","106"], ["rooms","107"], ["rooms","108"], ["rooms","109"], ["rooms","110"],
              ["rooms","111"], ["rooms","112"], ["rooms","113"], ["rooms","114"], ["rooms","115"],
              ["rooms","116"], ["rooms","117"], ["rooms","118"], ["rooms","119"], ["rooms","120"],
              ["rooms","121"], ["rooms","122"], ["rooms","123"], ["rooms","124"], ["rooms","125"],
            ],
  "query":
  [
    {
      "map":
      {
        "language":"javascript",
        "source":
          "function(v)
          {
            var parsed_data = JSON.parse(v.values[0].data);
            var data = {};
            data[parsed_data.style] = parsed_data.capacity;
            return [data];
          }"
      }
    },
    {
      "reduce":
      {
        "language":"javascript",
        "source":
          "function(v)
          {
            var totals = {};
            for (var i in v)
            {
              for (var style in v[i])
              {
                if (totals[style]) totals[style] += v[i][style];
                else               totals[style] = v[i][style];
              }
            }

ejsLog('/tmp/map_reduce.log', JSON.stringify(totals));
            return totals;
          }"       
      }
    }
  ]
} 

Here is what I get back at the command line:

{"single":21,"queen":22,"king":27,"suite":20,"double":5,"struct":{"king":2},"struct":{"suite":3},"struct":{"single":1},"struct":{"queen":1},"struct":{"double":2}}

But here is what I see in the log file:

07/10/2012 (10:35:30): {"single":21,"queen":22,"king":27,"suite":20,"double":5}

Note: if I exclude rooms 121 through 125 then I do not get the extra “struct” values.

Anyone recognize this? what are those extra struct values? Why do they only show up after including more than 20 inputs?

Thanks

Generic-user-small
17 Jul 2012, 10:33
Mick Brooks (6 posts)

I’m using a different version of Riak (1.1.4), and can’t get the code sample you gave to run. I get an error:

{"phase":1,"error":"bad_json","input":null,"type":null,"stack":null}

I notice that the reduce function returns the totals struct, when it should return an array e.g. @return [totals]@. When I do that my error goes away. Maybe yours will too?

Aha - I think I have an idea what’s going on. The reduce function can be called more than once as results from different nodes are collected. When there are a small number of rooms, only one call is made to reduce and all looks well. When the extra rooms are added, multiple calls are made to reduce. The struct returned from a previous call is then passed as an argument to reduce, which doesn’t do the right thing because it’s in the wrong format.

My guess is that you’ll see two entries in your log file when the error occurs (one from each call of reduce). I also guess that the later version of Riak has “better” error handling, and so gives me a different message.

Generic-user-small
17 Jul 2012, 17:03
Jeff Ostrin (2 posts)

Thanks Michael. That fixed it!

That is what I get for going off the reservation and trying things myself huh? Good learning experience though!

You must be logged in to comment