0

I was unable to get the erlang function running so trying with Javascript as below :

curl -XPOST http://localhost:8098/mapred \
  -H "Content-Type: application/json" \
  -d @- \
<<EOF
{
  "inputs":"logs",
  "query":[{
    "map":{
      "language":"javascript",
      "source":"function(riakObject, keydata, arg) {
        var m = riakObject.values[0].data.match(/^INFO.*cart/);
        return [(m ? m.length : 0 )];
      }"
    },
    "reduce":{
      "language":"javascript",
      "source":"function(values, arg){
        return [values.reduce(
          function(total, v){ return total + v; }, 0)
        ];
      }"
    }
  }]
}
EOF

seems not working with JS as well. Shell just hangs and does not return at all. Please suggest.

** UPDATE **

Today when i tried i see the following error :

An error occurred parsing the "query" field.
["Unrecognized format of query phase:\n   ",
 [123,
  [34,<<"map">>,34],
  58,
  [123,
   [34,<<"language">>,34],
   58,
   [34,<<"javascript">>,34],
   44,
   [34,<<"source">>,34],
   58,
   [34,
    <<"function(riakObject, keydata, arg) {        var m = riakObject.values[0].data.match(/^INFO.*Milk/);        return [(m ? m.length : 0 )];      }">>,
    34],
   125],
  44,
  [34,<<"reduce">>,34],
  58,
  [123,
   [34,<<"language">>,34],
   58,
   [34,<<"javascript">>,34],
   44,
   [34,<<"source">>,34],
   58,
   [34,
    <<"function(values, arg){        return [values.reduce(          function(total, v){ return total + v; }, 0)        ];      }">>,
    34],
   125],
  125],
 "\n\nValid formats are:\n   {\"map\":{...spec...}}\n   {\"reduce\":{...spec...}}\n   {\"link:{...spec}}\n"]
Raghuveer
  • 2,859
  • 7
  • 34
  • 66
  • Javascript use in Riak's Map Reduce capability has been deprecated: http://docs.basho.com/riak/kv/2.2.0/release-notes/. I highly recommend looking at the Erlang examples in the documentation: http://docs.basho.com/riak/kv/2.2.0/developing/usage/mapreduce/. – Craig Jan 30 '17 at 15:13
  • Yes i know but earlier I posted issues with erlang functions also nobody gave a proper reply http://stackoverflow.com/questions/40750284/riak-mapreduce-error basically erlang just does not work in my case. It would be helpful if you can reply to the erlang question. thanks – Raghuveer Feb 01 '17 at 13:46
  • Sorry, would love to help but I am not really an Erlang expert. If you aren't getting responses here on Stack to specific questions you might want to check out the Riak mailing lists: http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com – Craig Feb 01 '17 at 15:54

1 Answers1

1

The query element should be a list of objects, with "map" and "reduce" being in discreet objects. Your JSON has them as properties of the same object.

This worked for me:

curl -XPOST http://localhost:8098/mapred -H "Content-Type: application/json" -d  '{
  "inputs":"logs",
  "query":[
    {"map":{
      "language":"javascript",
      "source":"function(riakObject, keydata, arg) {
        var m = riakObject.values[0].data.match(/^INFO.*cart/);
        return [(m ? m.length : 0 )];
      }"
    }},
    {"reduce":{
      "language":"javascript",
      "source":"function(values, arg){
        return [values.reduce(
          function(total, v){ return total + v; }, 0)
        ];
      }"
    }}
  ]
}'
Joe
  • 25,000
  • 3
  • 22
  • 44