-
-
Save michaloo/6d7b60cbcc9e58cc026719d4f435fedb to your computer and use it in GitHub Desktop.
Google Function - Hull Events to BigQuery
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'; | |
const { BigQuery } = require('@google-cloud/bigquery'); | |
// Create a client | |
const bigqueryClient = new BigQuery(); | |
if (!process.env.DATASET_ID) { | |
console.error("DATASET_ID env var missing"); | |
process.exit(1); | |
} | |
if (!process.env.TABLE_ID) { | |
console.error("TABLE_ID env var missing"); | |
process.exit(1); | |
} | |
/** | |
* Process a HTTP request. | |
* | |
* @param {!express:Request} req HTTP request context. | |
* @param {!express:Response} res HTTP response context. | |
*/ | |
exports.handleHttpRequest = async function handleHttpRequest(req, res) { | |
if (!req.headers || !req.headers['x-secret'] || req.headers['x-secret'] !== process.env.SECRET) { | |
return res.status(401).send('Unauthorized'); | |
} | |
if (!req.body || !req.body.user || ! req.body.user.id) { | |
return res.status(400).send('Bad Request: no user id found'); | |
} | |
if (!req.body || !req.body.events || !Array.isArray(req.body.events)) { | |
return res.status(400).send('Bad Request: no events array'); | |
} | |
try { | |
const datasetId = process.env.DATASET_ID; | |
const tableId = process.env.TABLE_ID; | |
const userId = req.body.user.id | |
const rows = req.body.events.map(event => { | |
return { | |
user_id: userId, | |
event_id: event.event_id, | |
created_at: event.created_at, | |
event_name: event.event, | |
event_source: event.event_source, | |
event_type: event.event_type, | |
session_id: event.session_id, | |
context: JSON.stringify(event.context), | |
properties: JSON.stringify(event.properties) | |
}; | |
}); | |
// Insert data into a table | |
await bigqueryClient | |
.dataset(datasetId) | |
.table(tableId) | |
.insert(rows); | |
const message = `Inserted ${rows.length} rows`; | |
res.status(200).send(message); | |
} catch (error) { | |
res.status(500).send(`Error streaming BigQuery: ${error}`); | |
} | |
} |
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
{ | |
"name": "hull-bigquery-exporter-functions", | |
"version": "0.0.1", | |
"main": "index.js", | |
"scripts": { | |
"start": "functions-framework --target=handleHttpRequest", | |
"deploy": "gcloud functions deploy bigquery-exporter-demo --runtime nodejs10 --trigger-http --allow-unauthenticated --entry-point handleHttpRequest --env-vars-file .env.yaml" | |
}, | |
"dependencies": { | |
"@google-cloud/bigquery": "^5.2.0" | |
}, | |
"devDependencies": { | |
"@google-cloud/functions-framework": "^1.7.1", | |
"jest": "^26.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment