Skip to content

Instantly share code, notes, and snippets.

View mr-pascal's full-sized avatar

Pascal mr-pascal

View GitHub Profile
package main
import (
"encoding/csv"
"io"
"log"
"os"
)
func main () {
@mr-pascal
mr-pascal / main.go
Last active February 27, 2021 14:56
package main
import (
"github.com/gofiber/fiber/v2"
jwtware "github.com/gofiber/jwt/v2"
"log"
)
func main() {
// Create new Go Fiber application web server
Firstname Lastname Country
Hans Meier Germany
Franz Josef France
// Imports the Google Cloud client library
const { BigQuery } = require('@google-cloud/bigquery');
// Defines the location of the dataset and tables
const location = 'US';
// Creates a BigQuery client
const bigquery = new BigQuery({
// The relative file path to your Service Account key file
keyFilename: 'bigquery-sa.json',
/**
* Insert a certain set of rows into the passed table
* @param {string} datasetId The ID of the parent dataset
* @param {string} tableId The ID of the table to insert data in
* @returns {Promise<void>}
*/
const insertRows = async (datasetId, tableId) => {
const rows = [
{ Name: 'Tom', Age: 30 },
{ Name: 'Jane', Age: 32, Weight: 87, IsMagic: true },
/**
* Retrieve data from BigQuery
* @param {string} projectId The ID of the GCP project the dataset/table is located
* @param {string} datasetId The ID of the parent dataset
* @param {string} tableId The ID of the table to get data from
* @returns {Promise<void>}
*/
const getData = async (projectId, datasetId, tableId) => {
const query = `
SELECT * from \`${projectId}.${datasetId}.${tableId}\`
/**
const rows = [
// invalid row, there is no "Hello" field in the table schema
{ Name: 'Tom', Age: 30, Hello: "string" }
],
**/
// --> Output of: console.error(JSON.stringify(e.response.insertErrors, null, 2));
[
const importCSV = async (datasetId, tableId, pathToLocalFile, timePartitioning) => {
const metadata = {
sourceFormat: 'CSV',
// Ignore first row since it contains the column headers
skipLeadingRows: 1,
schema: {
fields: [
{ name: 'date', type: 'DATETIME', mode: 'REQUIRED' },
{ name: 'name', type: 'STRING', mode: 'REQUIRED' },
{ name: 'event', type: 'INT64', mode: 'REQUIRED' },
/**
* Run query on BigQuery and print query statistics
* @param {string} query The query to run
* @param {string} location The location of the dataset
* @returns {Promise<void>}
*/
const runQueryWithStatistics = async (query, location) => {
const options = {
query,
--- Without Partition ---
--- Job Statistics ---
┌─────────┬────────────────────────┬──────────┬─────────┐
│ (index) │ Description │ Value │ Unit │
├─────────┼────────────────────────┼──────────┼─────────┤
│ 0 │ 'Cache hit' │ false │ 'Bool' │
│ 1 │ 'Time taken' │ 221 │ 'ms' │
│ 2 │ 'Partitions processed' │ 0 │ 'Count' │
│ 3 │ 'Rows read' │ 73728 │ 'Count' │
│ 4 │ 'Bytes processed' │ 1572864 │ 'Bytes' │