forked from awslabs/kinesis-aggregation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample-deaggregation.js
More file actions
173 lines (142 loc) · 5.18 KB
/
sample-deaggregation.js
File metadata and controls
173 lines (142 loc) · 5.18 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
var deagg = require('aws-kinesis-agg/kpl-deagg');
var async = require('async');
require('constants');
var computeChecksums = true;
var ok = 'OK';
var error = 'ERROR';
/** function which closes the context correctly based on status and message */
var finish = function(event, context, status, message) {
"use strict";
console.log("Processing Complete");
// log the event if we've failed
if (status !== ok) {
if (message) {
console.log(message);
}
// ensure that Lambda doesn't checkpoint to kinesis
context.done(status, JSON.stringify(message));
} else {
context.done(null, message);
}
};
/** function which handles cases where the input message is malformed */
var handleNoProcess = function(event, callback) {
"use strict";
var noProcessReason;
if (!event.Records || event.Records.length === 0) {
noProcessReason = "Event contains no Data";
}
if (event.Records[0].eventSource !== "aws:kinesis") {
noProcessReason = "Invalid Event Source " + event.eventSource;
}
if (event.Records[0].kinesis.kinesisSchemaVersion !== "1.0") {
noProcessReason = "Unsupported Event Schema Version " + event.Records[0].kinesis.kinesisSchemaVersion;
}
if (noProcessReason) {
finish(event, error, noProcessReason);
} else {
callback();
}
};
/**
* Example lambda function which uses the KPL asyncronous deaggregation
* interface to process Kinesis Records from the Event Source
*/
exports.exampleAsync = function(event, context) {
"use strict";
console.log("Processing KPL Aggregated Messages using kpl-deagg(async)");
handleNoProcess(event, function() {
var realRecords = [];
console.log("Processing " + event.Records.length + " Kinesis Input Records");
// process all records in parallel
async.map(event.Records, function(record, asyncCallback) {
// use the async deaggregate interface. the per-record callback
// appends the records to an array, and the after record callback
// calls the async callback to mark the kinesis record as completed
// within
// the async map operation
deagg.deaggregate(record.kinesis, computeChecksums, function(err, userRecord) {
if (err) {
console.log("Error on Record: " + err);
asyncCallback(err);
} else {
var recordData = new Buffer(userRecord.data, 'base64');
console.log("Per Record Callback Invoked with Record: " + recordData.toString('ascii'));
realRecords.push(userRecord);
// you can do something else with each kinesis user
// record here!
}
}, function(err) {
if (err) {
console.log(err);
}
// call the async callback to reflect that the kinesis message
// is completed
asyncCallback(err);
});
}, function(err, results) {
// function is called once all kinesis records have been processed
// by async.map
if (debug) {
console.log("Kinesis Record Processing Completed");
console.log("Processed " + realRecords.length + " Kinesis User Records");
}
if (err) {
finish(event, context, error, err);
} else {
finish(event, context, ok, "Success");
}
});
});
};
/**
* Example lambda function which uses the KPL syncronous deaggregation interface
* to process Kinesis Records from the Event Source
*/
exports.exampleSync = function(event, context) {
"use strict";
console.log("Processing KPL Aggregated Messages using kpl-deagg(sync)");
handleNoProcess(event, function() {
console.log("Processing " + event.Records.length + " Kinesis Input Records");
var totalUserRecords = 0;
async.map(event.Records, function(record, asyncCallback) {
// use the deaggregateSync interface which receives a single
// callback with an error and an array of Kinesis Records
deagg.deaggregateSync(record.kinesis, computeChecksums, function(err, userRecords) {
if (err) {
console.log(err);
asyncCallback(err);
} else {
console.log("Received " + userRecords.length + " Kinesis User Records");
totalUserRecords += userRecords.length;
userRecords.map(function(record) {
var recordData = new Buffer(record.data, 'base64');
console.log("Kinesis Aggregated User Record:" + recordData.toString('ascii'));
// you can do something else with each kinesis
// user record here!
});
// call the async callback to reflect that the kinesis
// message is completed
asyncCallback(err);
}
});
}, function(err, results) {
// function is called once all kinesis records have been processed
// by async.map
if (debug) {
console.log("Completed processing " + totalUserRecords + " Kinesis User Records");
}
if (err) {
finish(event, context, error, err);
} else {
finish(event, context, ok, "Success");
}
});
});
};