Google Cloud
03 / 04

BigQuery, Pub/Sub & Cloud Functions

Google Cloud: BigQuery, Pub/Sub & Functions

BigQuery

BigQuery is Google's fully managed, serverless data warehouse. Runs SQL queries over petabytes of data using massive parallelism. Pay per query (TB scanned) or flat-rate.

-- Standard SQL in BigQuery
SELECT
  user_id,
  COUNT(*) AS orders,
  SUM(total_amount) AS revenue
FROM `my-project.sales.orders`
WHERE DATE(created_at) >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
  AND status = 'completed'
GROUP BY user_id
HAVING revenue > 1000
ORDER BY revenue DESC
LIMIT 100;

-- Partition pruning (critical for cost — only reads relevant partitions)
SELECT * FROM `my-project.events.page_views`
WHERE _PARTITIONDATE = '2026-05-01';

-- Query external data (GCS)
SELECT * FROM `my-project.external.logs`
WHERE timestamp > '2026-05-01';
# bq CLI
bq query --use_legacy_sql=false 'SELECT COUNT(*) FROM `project.dataset.table`'
bq load --source_format=CSV dataset.table gs://my-bucket/data.csv schema.json
bq mk --dataset my-project:analytics
bq extract --destination_format=PARQUET dataset.table gs://my-bucket/export/

Pub/Sub

Cloud Pub/Sub is a managed messaging service for asynchronous event-driven systems. Publishers send messages to topics; subscribers pull or push messages from subscriptions.

import { PubSub } from '@google-cloud/pubsub'
const pubsub = new PubSub()

// Publish
const messageId = await pubsub.topic('my-topic').publish(
  Buffer.from(JSON.stringify({ event: 'user.created', userId: '123' }))
)

// Pull subscription
const [messages] = await pubsub.subscription('my-sub').pull({ maxMessages: 10 })
for (const msg of messages) {
  console.log(msg.data.toString())
  msg.ack()  // or msg.nack() to retry
}

// Streaming pull (long-lived connection)
const subscription = pubsub.subscription('my-sub')
subscription.on('message', (msg) => {
  processMessage(JSON.parse(msg.data.toString()))
  msg.ack()
})
subscription.on('error', console.error)

Cloud Functions

// functions/index.ts
import { onRequest } from 'firebase-functions/v2/https'
import { onDocumentCreated } from 'firebase-functions/v2/firestore'
import { onMessagePublished } from 'firebase-functions/v2/pubsub'

// HTTP trigger
export const api = onRequest({ region: 'europe-west1' }, (req, res) => {
  res.json({ message: 'Hello!' })
})

// Firestore trigger
export const onUserCreated = onDocumentCreated('users/{userId}', async (event) => {
  const user = event.data?.data()
  await sendWelcomeEmail(user.email)
})

// Pub/Sub trigger
export const processOrder = onMessagePublished('orders', async (event) => {
  const order = JSON.parse(Buffer.from(event.data.message.data, 'base64').toString())
  await fulfillOrder(order)
})
# Deploy
gcloud functions deploy my-function \
  --gen2 \
  --runtime nodejs20 \
  --trigger-http \
  --region europe-west1 \
  --allow-unauthenticated

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free