【问题标题】:CouchDB: How to use array keys in Map functions when using Reduce?CouchDB:使用 Reduce 时如何在 Map 函数中使用数组键?
【发布时间】:2016-04-11 11:09:33
【问题描述】:

我想在 CouchDB 中编写一个 MapReduce 函数,其中 Map 函数将键作为数组发出,但 reduce 函数仅使用映射键中的一个值。例如:

地图功能:

function(doc) {
    if (doc.type_ === 'survey') {
        emit([doc.timeRecorded_, doc.imei_], 1);
    };
};

Reduce 函数:

function(k,v) {

  // How to handle only the doc.imei_ as the value?
  // Or, alternatively, how to filter based on timeRecorded_ somewhere other than the map function?
  return sum(v)
}

timeRecorded_ 在 EPOCH 编号中,因此不会有重复(偶然除外)。如果我要对其进行汇总,则需要将其四舍五入为“天”值。或者,数据可以这样准备,即 timeRecorded_ 在源数据中已经四舍五入(可能更改为 dateRecorded_)

【问题讨论】:

    标签: mapreduce couchdb


    【解决方案1】:

    这个问题的一个众所周知的模式是将日期拆分为一个数组(例如[year, month, day, hour, minute];间隔可能不同,但要保持顺序)并使用数组作为映射函数中的键。

    因此,您将能够根据您需要的group_level 减少行数(例如“按年”、“按月”、“按天”、“按小时”、“按分钟”等) .

    来源:http://blog.couchbase.com/understanding-grouplevel-view-queries-compound-keys

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      相关资源
      最近更新 更多