Skip to content

Instantly share code, notes, and snippets.

@frah
Last active April 17, 2016 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frah/ce78336e65808e4be68f045eafb45d53 to your computer and use it in GitHub Desktop.
Save frah/ce78336e65808e4be68f045eafb45d53 to your computer and use it in GitHub Desktop.
Put datas received at Kinesis into DynamoDB
'use strict';
var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();
console.log('Loading function');
exports.handler = (event, context, callback) => {
event.Records.forEach((record) => {
// Kinesis data is base64 encoded so decode here
const payload = new Buffer(record.kinesis.data, 'base64').toString('ascii');
const json = JSON.parse(payload);
let d = new Date(record.kinesis.approximateArrivalTimestamp * 1000);
json.datas.forEach((data) => {
dynamodb.putItem({
TableName: 'kinesis_sensor_datas',
Item: {
'id': record.kinesis.sequenceNumber,
'date': d.toString(),
'device': json.device,
'type': data.name,
'value': data.value
}
}, (err, data) => {
if (err) {
console.log('error','putting item into dynamodb failed: '+err);
}
else {
console.log('great success: '+JSON.stringify(data, null, ' '));
}
});
});
});
callback(null, `Successfully processed ${event.Records.length} records.`);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment