# BigQuery

Send2Bucket accepts JSON over HTTP and writes it to Cloud Storage as gzip newline-delimited JSON, which BigQuery reads in place or loads into a table. To query bucket objects in BigQuery, use a [Google Cloud Storage](../destinations/gcs.md) destination. The objects follow [Delivery format](../delivery-format.md). You can query the delivered objects in place through an external table, or load them into a native table on a schedule. Use a native table for repeated queries. An external table reads the latest deliveries directly.

## Query objects in place

Your events can change shape from day to day, so the external table reads each line as one string and parses it at query time. The CSV reader does this when quoting is off and the delimiter is a byte that never appears in Send2Bucket's output:

```sql
CREATE SCHEMA IF NOT EXISTS s2b;

CREATE OR REPLACE EXTERNAL TABLE s2b.raw_lines (line STRING)
WITH PARTITION COLUMNS (stream STRING, date DATE, hour INT64)
OPTIONS (
  format = 'CSV',
  field_delimiter = '\x01',
  quote = '',
  compression = 'GZIP',
  uris = ['gs://<bucket>/<prefix>/s2b/stream=*'],
  hive_partition_uri_prefix = 'gs://<bucket>/<prefix>/s2b',
  require_hive_partition_filter = false
);
```

- `stream=*` keeps the destination's test objects under `s2b/_verify/` out of the table.
- Send2Bucket escapes control characters inside JSON strings, so byte `0x01` never occurs in a line.
- Gzip objects are read one per worker, so a query over many hours of objects is slower than over Parquet. Filter on `date` and `hour`: BigQuery reads only the matching folders.

A view turns the line into the same columns the other engine pages use:

```sql
CREATE OR REPLACE VIEW s2b.records AS
SELECT stream, date, hour,
       JSON_VALUE(line, '$.signal') AS signal,
       TIMESTAMP(JSON_VALUE(line, '$.received_at')) AS received_at,
       JSON_VALUE(line, '$.delivery_batch_id') AS delivery_batch_id,
       SAFE.PARSE_JSON(JSON_QUERY(line, '$.resource')) AS resource,
       SAFE.PARSE_JSON(JSON_QUERY(line, '$.data')) AS data
FROM s2b.raw_lines;
```

## Load into a native table

Load each finished day once, for example with a scheduled query that runs after midnight UTC for the previous day:

```sql
CREATE TABLE IF NOT EXISTS s2b.records_native (
  v INT64, delivery_batch_id STRING, batch_id STRING, received_at TIMESTAMP,
  signal STRING, resource JSON, scope JSON, data JSON)
PARTITION BY DATE(received_at)
CLUSTER BY signal;

LOAD DATA INTO s2b.records_native
FROM FILES (
  format = 'JSON',
  uris = ['gs://<bucket>/<prefix>/s2b/stream=<stream_id>/date=2026-11-18/*']
);
```

Create `s2b.records_native` with the schema above. For each day, run either the SQL `LOAD DATA` statement or the following `bq load` command once.

```bash
bq load --source_format=NEWLINE_DELIMITED_JSON \
  s2b.records_native \
  'gs://<bucket>/<prefix>/s2b/stream=<stream_id>/date=2026-11-18/*'
```

Both commands decompress gzip files automatically and append rows to the table. To retry a day, load its files into a staging table. Insert all rows from each delivery batch that is missing from `s2b.records_native`. Other streams can share the same date partition.

## Query JSON fields

BigQuery has two families of JSON functions. Use `JSON_VALUE` and `JSON_QUERY`; Google keeps `JSON_EXTRACT` and `JSON_EXTRACT_SCALAR` for older queries ([JSON functions](https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions)).

| To get | Use | Legacy equivalent |
|---|---|---|
| A scalar as `STRING`: a name, an ID, a number to cast | `JSON_VALUE(x, '$.path')` | `JSON_EXTRACT_SCALAR(x, '$.path')` |
| An object or array, as JSON | `JSON_QUERY(x, '$.path')` | `JSON_EXTRACT(x, '$.path')` |
| Each element of an array | `JSON_QUERY_ARRAY(x, '$.path')` | `JSON_EXTRACT_ARRAY(x, '$.path')` |
| A typed value from a `JSON` column | `INT64(x.cents)`, `STRING(x.event)` | |

The functions take a `STRING` or a `JSON` value, so they work on the `line` column of `s2b.raw_lines` and on the `data` column of `s2b.records` alike. Keys with dots or dashes need quoting in the path, for example `$."service.name"`.

The items of each paid order, one row per item:

```sql
SELECT JSON_VALUE(data, '$.order_id') AS order_id,
       JSON_VALUE(item, '$.sku') AS sku,
       CAST(JSON_VALUE(item, '$.qty') AS INT64) AS qty
FROM s2b.records,
     UNNEST(JSON_QUERY_ARRAY(data, '$.items')) AS item
WHERE signal = 'event' AND JSON_VALUE(data, '$.event') = 'order.paid';
```

## Export query results to JSON

`EXPORT DATA` writes a query's result to Cloud Storage as newline-delimited JSON. The URI needs one `*`, which BigQuery replaces with a file number ([export statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/export-statements)):

```sql
EXPORT DATA OPTIONS (
  uri = 'gs://<bucket>/exports/paid-orders/*.json.gz',
  format = 'JSON',
  compression = 'GZIP',
  overwrite = true
) AS
SELECT received_at, data
FROM s2b.records
WHERE signal = 'event' AND JSON_VALUE(data, '$.event') = 'order.paid';
```

To export a whole native table instead, use `bq extract`:

```bash
bq extract --destination_format=NEWLINE_DELIMITED_JSON --compression=GZIP \
  s2b.records_native 'gs://<bucket>/exports/records/*.json.gz'
```

## Example queries

These run against the `s2b.records` view, or `s2b.records_native` with the same column names.

Records per signal:

```sql
SELECT signal, COUNT(*) AS records
FROM s2b.records
WHERE date = '2026-11-19'
GROUP BY signal
ORDER BY records DESC;
```

JSON events by name, with revenue from `order.paid`:

```sql
SELECT JSON_VALUE(data, '$.event') AS event,
       COUNT(*) AS events,
       SUM(CAST(JSON_VALUE(data, '$.cents') AS INT64)) / 100 AS revenue_usd
FROM s2b.records
WHERE signal = 'event'
GROUP BY event
ORDER BY events DESC;
```

Log records by service and severity:

```sql
SELECT (SELECT JSON_VALUE(a, '$.value.stringValue')
        FROM UNNEST(JSON_QUERY_ARRAY(resource, '$.attributes')) AS a
        WHERE JSON_VALUE(a, '$.key') = 'service.name') AS service,
       JSON_VALUE(data, '$.severityText') AS severity,
       COUNT(*) AS logs
FROM s2b.records
WHERE signal = 'log'
GROUP BY service, severity;
```

The five slowest spans:

```sql
SELECT JSON_VALUE(data, '$.name') AS span,
       JSON_VALUE(data, '$.traceId') AS trace_id,
       (CAST(JSON_VALUE(data, '$.endTimeUnixNano') AS INT64)
        - CAST(JSON_VALUE(data, '$.startTimeUnixNano') AS INT64)) / 1e6 AS duration_ms
FROM s2b.records
WHERE signal = 'span'
ORDER BY duration_ms DESC
LIMIT 5;
```

## Query Iceberg tables

BigQuery reads an Iceberg table in Cloud Storage as a BigLake external table created from one metadata file. It does not follow new commits on its own, so the table lags behind Send2Bucket's appends until you point it at a newer metadata file. For BigQuery, a Cloud Storage bucket destination is simpler.

## Other destinations

- **Amazon S3**: BigQuery Omni can create the same external table over S3 through an AWS connection (`WITH CONNECTION`), in Omni regions only. Otherwise copy objects to Cloud Storage with Storage Transfer Service and use the steps above.

## Tools that connect through BigQuery

These tools have a guide for reading Send2Bucket data through BigQuery:

- **BI tools:** [Apache Superset](../integrations/bi/superset.md), [Grafana](../integrations/bi/grafana.md), [Looker Studio](../integrations/bi/looker-studio.md), [Metabase](../integrations/bi/metabase.md), [Power BI](../integrations/bi/power-bi.md), [Tableau](../integrations/bi/tableau.md)
- **SQL clients:** [DataGrip](../integrations/sql-clients/datagrip.md), [DBeaver](../integrations/sql-clients/dbeaver.md), [DuckDB UI and Harlequin](../integrations/sql-clients/duckdb-ui.md), which run [DuckDB](./duckdb.md), [TablePlus](../integrations/sql-clients/tableplus.md)
- **Notebooks:** [Deepnote](../integrations/notebooks/deepnote.md), [Hex](../integrations/notebooks/hex.md), [Jupyter](../integrations/notebooks/jupyter.md)

## Notes

- External table queries are billed on the bytes BigQuery reads from Cloud Storage, which for gzip is the decompressed size of every object in the folders you select. Filter on `date` and `hour`.
- A native table loaded from these objects is cheaper and faster for repeated dashboards. Partition it on `received_at` and cluster it on `signal`.
- `JSON_VALUE` returns `NULL` for a missing field instead of failing, so a typo in a path returns empty results rather than an error.
- [Create a BigQuery external table over JSON in Cloud Storage](../guides/bigquery-external-json-gcs.md) covers external tables over any NDJSON files: declared schemas, autodetect, nested fields and the `JSON` type.
- See the [querying overview](./index.md) for other engines.
