简体   繁体   English

Riak在MapReduce上排序

[英]Riak Sort on MapReduce

function(values) { 
    return values.sort(
        function(a, b) {
            return b['timestamp'] - a['timestamp'];
        }
    );
}

I'm currently executing the above code for the reduce phase of a Riak MapReduce query but it is not correctly sorting by the value of timestamp key. 我目前正在执行Riak MapReduce查询的reduce阶段的上述代码,但它没有按时间戳键的值正确排序。 Any ideas why? 有什么想法吗?

I'm using the Riak Python client and this is the full code which contains the above reduce step: 我正在使用Riak Python客户端,这是包含上述reduce步骤的完整代码:

query = riak_client.add('bucket')

query.map("function(v) { var data = JSON.parse(v.values[0].data); if (data.item == 'A') { return [[v.key, data]]; } return []; }")

query.reduce("function(values) { return values.sort(function(a, b) { return b['timestamp'] - a['timestamp']; }); }")

for result in query.run():
    print result

Try wrapping the return part in square brackets, effectively returning an array of results. 尝试将返回部分包装在方括号中,有效地返回结果数组。

function(values) { 
  return values.sort(
    function(a, b) {
        return b['timestamp'] - a['timestamp'];
    }
  );
}

Have it look like this: 看起来像这样:

function(values) { 
  return [values.sort(
    function(a, b) {
        return b['timestamp'] - a['timestamp'];
    }
  )];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM