Skip to content

Instantly share code, notes, and snippets.

@adamrneary
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save adamrneary/ff7826f127b2b6a043db to your computer and use it in GitHub Desktop.

Select an option

Save adamrneary/ff7826f127b2b6a043db to your computer and use it in GitHub Desktop.
A simple node script for piping live Amazon Kinesis events to a flat file for analysis and replication
var kinesis = require('kinesis');
Transform = require('stream').Transform;
dotenv = require('dotenv');
es = require('event-stream');
fs = require('fs');
dotenv.load();
// Data is retrieved as Record objects, so we transform into Buffers.
var bufferify = new Transform({objectMode: true})
bufferify._transform = function(record, encoding, cb) {
cb(null, record.Data)
}
// Capture the incoming data and insert newline characters
readStream = kinesis.stream({name: 'video-events'})
.pipe(bufferify)
.pipe(es.map(function (data, cb) {
cb(null, new Buffer(data, 'base64').toString('utf-8') + "\r\n")
}));
// Send to stdout and a flatfile
readStream.pipe(process.stdout);
readStream.pipe(fs.createWriteStream('test/events.log'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment