Skip to content

Instantly share code, notes, and snippets.

@frah
Last active Apr 17, 2016
Embed
What would you like to do?
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