Last active
April 17, 2016 04:23
-
-
Save frah/ce78336e65808e4be68f045eafb45d53 to your computer and use it in GitHub Desktop.
Put datas received at Kinesis into DynamoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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