-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformSensorJSON.js
More file actions
41 lines (33 loc) · 1.32 KB
/
Copy pathtransformSensorJSON.js
File metadata and controls
41 lines (33 loc) · 1.32 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
// transformSensorJSON is a Lambda Function for Amazon Kinesis Firehose stream
// processing. It appends a newline to each JSON record. This allows the
// Athena crawler to index the sensor JSON files in S3 correctly.
// Settings for the transformSensorJSON Lambda Function:
// Runtime: Node.js 8.10
// Handler: index.handler
// Execution Role: lambda_basic_execution
// Memory: 128 MB
// Timeout: 1 min
console.log('Loading function');
exports.handler = (event, context, callback) => {
let success = 0; // Number of valid entries found
let failure = 0; // Number of invalid entries found
// Process the list of records and transform them
const output = event.records.map((record) => {
// Kinesis data is base64 encoded so decode here
console.log(record.recordId);
let payload = new Buffer(record.data, 'base64').toString();
console.log('>>> Decoded payload:', payload);
// Append a newline to the record.
payload += '\n';
console.log('<<< Transformed payload:', payload);
// Return the transformed record.
success++;
return {
recordId: record.recordId,
result: 'Ok',
data: new Buffer(payload).toString('base64'),
};
});
console.log(`Processing completed. Successful records ${success}, Failed records ${failure}.`);
callback(null, { records: output });
};