Skip to content

Instantly share code, notes, and snippets.

@timhudson
Last active December 23, 2015 00:09
Show Gist options
  • Save timhudson/6552011 to your computer and use it in GitHub Desktop.
Save timhudson/6552011 to your computer and use it in GitHub Desktop.

Usage

$ mongoexport -d test -c activities | node event-convert-stream.js | mongoimport -d test -c events
var Transform = require('stream').Transform
, util = require('util')
, split = require('split')
util.inherits(ActivityToEventStream, Transform)
function ActivityToEventStream() {
Transform.call(this, {
objectMode: true
})
}
ActivityToEventStream.prototype._write = function(obj, encoding, done) {
var e
// Skip non 'EPISODE WATCHED' events
if (obj.activityType !== 'EPISODE WATCHED')
return done()
e = {
_id: obj._id,
_type: 'ViewedEpisode',
app: obj.a,
showId: obj.sourceId,
timestamp: obj.timestamp || {$date: Date.now()}
}
// Set the userId or anonId
if (obj.a === '468')
e.anonId = obj.userId
else if (obj.userId.$oid)
e.userId = obj.userId.$oid
else
e.userId = obj.userId
// Set the episodeId if provided
if (obj.secondarySourceId) e.episodeId = obj.secondarySourceId
this.push(JSON.stringify(e) + '\n')
done()
}
process.stdin
.pipe(split(JSON.parse))
.pipe(new ActivityToEventStream())
.pipe(process.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment