Skip to content

Instantly share code, notes, and snippets.

@michaloo

michaloo/index.js Secret

Created Sep 10, 2020
Embed
What would you like to do?
Google Function - Hull Events to BigQuery
'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}`);
}
}
{
"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