-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap-reduce.js
107 lines (95 loc) · 3.9 KB
/
map-reduce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* @NApiVersion 2.x
* @NScriptType MapReduceScript
* @Description A Map/Reduce script to merge lines from invoices in NetSuite.
*/
define(['N/record', 'N/search', 'N/log'], function(record, search, log) {
/**
* Entry point for the getInputData stage.
* This stage is used to search for the data that will be processed.
*
* @returns {Array|Object|Search|RecordRef} The input data to be processed by the map/reduce script.
*/
function getInputData() {
// Perform a search to retrieve the invoices that need to be processed
return search.create({
type: search.Type.INVOICE,
filters: [
// Add any necessary filters here
],
columns: [
// Specify the columns to be retrieved
]
});
}
/**
* Entry point for the map stage.
* This stage is used to process each key-value pair.
*
* @param {Object} context - Data collection containing the key-value pairs to process through the map stage.
* @param {string} context.key - The key to be processed.
* @param {string} context.value - The value to be processed.
*/
function map(context) {
var result = JSON.parse(context.value);
var invoiceId = result.id;
try {
var invoice = record.load({
type: record.Type.INVOICE,
id: invoiceId
});
// Logic to merge lines from the invoice
// For example, combine lines with the same item and sum their quantities
invoice.save();
} catch (e) {
log.error('Error processing invoice', e.message);
}
}
/**
* Entry point for the reduce stage.
* This stage is used to aggregate results from the map stage.
*
* @param {Object} context - Data collection containing the key-value pairs to process through the reduce stage.
* @param {string} context.key - The key to be processed.
* @param {string} context.values - The values to be processed.
*/
function reduce(context) {
// Implement any necessary aggregation logic here
}
/**
* Entry point for the summarize stage.
* This stage is used to summarize the processing and report any errors.
*
* @param {Object} summary - Holds statistics regarding the execution of a map/reduce script.
* @param {number} summary.concurrency - Maximum concurrency number when executing parallel tasks.
* @param {Date} summary.dateCreated - Date and time when the script began running.
* @param {Date} summary.dateCompleted - Date and time when the script finished running.
* @param {Object} summary.inputSummary - Statistics about the script input stage.
* @param {Object} summary.mapSummary - Statistics about the script map stage.
* @param {Object} summary.reduceSummary - Statistics about the script reduce stage.
* @param {Array} summary.output - An array of values saved from the reduce stage.
*/
function summarize(summary) {
log.audit('Summary', 'Map/Reduce Script Completed');
log.audit('Concurrency', summary.concurrency);
log.audit('Date Created', summary.dateCreated);
log.audit('Date Completed', summary.dateCompleted);
if (summary.inputSummary.error) {
log.error('Input Error', summary.inputSummary.error);
}
summary.mapSummary.errors.iterator().each(function(key, error) {
log.error('Map Error', 'Error for key: ' + key + ', error: ' + error);
return true;
});
summary.reduceSummary.errors.iterator().each(function(key, error) {
log.error('Reduce Error', 'Error for key: ' + key + ', error: ' + error);
return true;
});
}
return {
getInputData: getInputData,
map: map,
reduce: reduce,
summarize: summarize
};
});