Last active
July 24, 2018 13:39
-
-
Save mrlynn/ece7039cc4b0c44057bbba16c4791f35 to your computer and use it in GitHub Desktop.
MongoDB Atlas Stitch Example Function Integrating with Amazon Kinesis
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
exports = function(event){ | |
const db = context.services.get("mongodb-atlas").db("streamdata"); | |
const clickdata = db.collection("clickdata"); | |
const awsService = context.services.get('aws'); | |
const geoAPI = "http://ip-api.com/json/"; | |
const eventFullDocument = event.fullDocument; | |
const eventDoc = event; | |
const eventIP = eventFullDocument.ipaddress; | |
const url = geoAPI + eventIP; | |
const locationService = context.services.get("locationService"); | |
const now = new Date(); | |
return locationService.get({ url }).then(response => { | |
const locationJSON = EJSON.parse(response.body.text()); | |
eventDoc.city = locationJSON.city; | |
eventDoc.state = locationJSON.region; | |
eventDoc.zip = locationJSON.zip; | |
eventDoc.org = locationJSON.org; | |
eventDoc.Timestamp = now.getTime(); | |
eventDoc.Date = now; | |
clickdata.insertOne(eventDoc); | |
try{ | |
awsService.kinesis().PutRecord({ | |
Data: JSON.stringify(eventDoc), | |
StreamName: "stitchStream", | |
PartitionKey: "1" | |
}).then(function(response) { | |
return response; | |
}); | |
} | |
catch(error){ | |
console.log(JSON.parse(error)); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment