Skip to content

Instantly share code, notes, and snippets.

@tylr
Created December 9, 2014 00:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tylr/e8baf45c07ced23ef013 to your computer and use it in GitHub Desktop.
Save tylr/e8baf45c07ced23ef013 to your computer and use it in GitHub Desktop.
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client();
var eventPattern = /([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) \"(.*?)\" \"(.*?)\" \"(.*?)\" \"(.*?)\" \"(.*?)\"/;
exports.handler = function(data, context) {
var events = [];
var encodedEvent, rawEvent,
matchedEvent, event, i,
commands;
console.log('data', JSON.stringify(data, null, ' '));
try {
for(i = 0; i < data.Records.length; ++i) {
encodedEvent = data.Records[i].kinesis.data;
rawEvent = new Buffer(encodedEvent, 'base64').toString();
matchedEvent = rawEvent.match(eventPattern);
events.push({
time : new Date(parseInt(matchedEvent[1])),
key : matchedEvent[2],
ip : matchedEvent[3],
session : matchedEvent[4],
is_mobile : matchedEvent[5] === 'true',
is_desktop : matchedEvent[6] === 'true',
browser : matchedEvent[8],
version : matchedEvent[9],
os : matchedEvent[10],
platform : matchedEvent[11],
referer : matchedEvent[12]
});
}
console.log('events', JSON.stringify(events, null, ' '));
commands = events.reduce(function(bulk, e) {
return bulk.concat({
index: {
_index: 'event_log',
_type: 'event'
}
}, e);
}, []);
console.log('commands', JSON.stringify(commands, null, ' '));
client.bulk({
body: commands
}, function(err, resp) {
console.log('bulk', err, resp);
context.done(null, 'success');
});
} catch(error) {
console.log('error', error);
context.done(null, 'fail');
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment