SuiteScript - Map/Reduce 总结

SuiteScript - Map/Reduce summarize

我正在编写 map/reduce 脚本来搜索自定义记录并根据其结果创建 CSV 文件。 首先,在 getInputData() 中创建搜索并返回其结果。 之后是 map 函数,我在其中使用 JSON.parse 来解析结果值。

我有一个全局声明为 var data = '' 的数组,我在其中推送我在 map 函数中获得的值。 但是当所有的循环都以map()结束,进入summarize()时,data数组又变空了

我希望在 summarize() 中调用该数据数组,我可以在其中使用 file.create() 函数创建 csv 文件。

我在 map() 中使用了 return data;,但在 summarize() 中显示为空。

您必须在地图末尾使用context.write才能将数据发送到下一阶段。

context.write({
    key: key,
    value: value
});

添加一个简单的脚本来展示如何在汇总阶段访问值:

/**
 *@NApiVersion 2.x
 *@NScriptType MapReduceScript
 */
 
define([],
function () {
    function getInputData() {
        return [{
            id: 1,
            value: 'abc'
        }, {
            id: 2,
            value: 'def'
        }];        
    }

    function map(context) {
        var value = JSON.parse(context.value);
        
        context.write({
            key: value.id,
            value: value.value
        });
    }
    
    function summarize(context) {
        var values = [];
        
        context.output.iterator().each(function (key, value) {
            log.debug({
                title: 'Output for key ' + key, 
                details: value
            });
                
            values.push(value);    
            return true;
        });
        
        log.debug({
            title: 'Summary',
            details: JSON.stringify(values)
        });
    }

    return {
        getInputData: getInputData,
        map: map,
        summarize: summarize
    }
});