# Databricks

Databricks reads the gzip NDJSON objects that [Amazon S3](../destinations/aws-s3.md) and [Google Cloud Storage](../destinations/gcs.md) destinations write, and [S3-compatible](../destinations/s3-compatible.md) destinations on Cloudflare R2. Query them in place with `read_files`, or ingest them into a Delta table with Auto Loader and query the Delta table. It is faster for dashboards and repeated queries; `read_files` needs no pipeline. For the record layout, see [Delivery format](../delivery-format.md).

Databricks runs Apache Spark, so the schema and the `variant_get` queries from the [Apache Spark](./spark.md) page carry over. External locations, `read_files`, Auto Loader, streaming tables and the `column:field` path syntax are Databricks-only.

## Connect to bucket objects

Unity Catalog reads a bucket through an [external location](https://docs.databricks.com/aws/en/connect/unity-catalog/cloud-storage/manage-external-locations): a path plus a storage credential that can read it. On AWS the credential is an IAM role; on Google Cloud it is a service account. A metastore admin creates the location once:

```sql
CREATE EXTERNAL LOCATION s2b_raw
  URL 's3://<bucket>/<prefix>/s2b/'
  WITH (STORAGE CREDENTIAL <credential_name>);

GRANT READ FILES ON EXTERNAL LOCATION s2b_raw TO `<group>`;
```

The credential needs read access only, for example `s3:GetObject` and `s3:ListBucket` on the prefix.

- **Google Cloud Storage**: on Databricks on Google Cloud, use `gs://<bucket>/<prefix>/s2b/` and a service account credential. See [Connect to a Google Cloud Storage external location](https://docs.databricks.com/gcp/en/connect/unity-catalog/cloud-storage/external-locations-gcs).
- **Cloudflare R2**: use `r2://<bucket>@<account_id>.r2.cloudflarestorage.com/<prefix>/s2b/` and a Cloudflare API token credential, on Databricks Runtime 14.3 or later. See [Connect to a Cloudflare R2 external location](https://docs.databricks.com/aws/en/connect/unity-catalog/cloud-storage/external-locations-r2).
- Databricks does not document external locations for other S3-compatible stores, such as MinIO or Backblaze B2. Read those with another engine, such as [Apache Spark](./spark.md) or [DuckDB](./duckdb.md).

The rest of this page uses `s3://` paths. Swap in your `gs://` or `r2://` path.

## Query objects in place

`read_files` reads the objects from Databricks SQL or a notebook, on Databricks Runtime 13.3 LTS or later. `VARIANT` columns need Databricks Runtime 15.3 or later:

```sql
CREATE OR REPLACE TEMPORARY VIEW records AS
SELECT * FROM read_files(
  's3://<bucket>/<prefix>/s2b/stream=*/date=*/hour=*/*.ndjson.gz',
  format => 'json',
  schema => 'v INT, delivery_batch_id STRING, batch_id STRING, received_at TIMESTAMP,
             signal STRING, resource VARIANT, scope VARIANT, data VARIANT');
```

- The `.gz` extension tells Spark to decompress each object.
- The `schema` keeps types stable. Without it, `read_files` infers a schema from the data and turns `data` into a struct of whichever event fields it saw.
- `stream=*` keeps the destination's test objects under `s2b/_verify/` out of the view.
- Narrow the path to read less: `stream=<stream_id>/date=2026-11-19/hour=*/*.ndjson.gz` reads one stream's day.

`read_files` lists and reads the objects on every query, so it always sees the latest deliveries, and a query over many days is slow. For anything you run repeatedly, ingest into a Delta table.

## Load into a Delta table

Auto Loader keeps a checkpoint of the objects it has ingested and picks up new ones on each run. Send2Bucket never rewrites an object, so each object is ingested once. In a notebook or job:

```python
(spark.readStream
  .format("cloudFiles")
  .option("cloudFiles.format", "json")
  .option("pathGlobFilter", "*.ndjson.gz")
  .option("cloudFiles.partitionColumns", "stream,date,hour")
  .schema("v INT, delivery_batch_id STRING, batch_id STRING, received_at TIMESTAMP, "
          "signal STRING, resource VARIANT, scope VARIANT, data VARIANT, "
          "stream STRING, date DATE, hour INT")
  .load("s3://<bucket>/<prefix>/s2b/")
  .writeStream
  .option("checkpointLocation", "<checkpoint_path>")
  .trigger(availableNow=True)
  .toTable("<catalog>.<schema>.s2b_records"))
```

- `pathGlobFilter` reads only delivered objects and skips the `.json` test objects under `s2b/_verify/`.
- Auto Loader takes `stream`, `date` and `hour` from the `key=value` folders. With an explicit schema, the schema must list them.
- `availableNow` ingests everything new and stops. Schedule the notebook as a job, hourly for example, or drop the trigger to run continuously.

In Lakeflow Declarative Pipelines or Databricks SQL, a streaming table does the same with `read_files`:

```sql
CREATE OR REFRESH STREAMING TABLE s2b_records AS
SELECT * FROM STREAM read_files(
  's3://<bucket>/<prefix>/s2b/stream=*/date=*/hour=*/*.ndjson.gz',
  format => 'json',
  schema => 'v INT, delivery_batch_id STRING, batch_id STRING, received_at TIMESTAMP,
             signal STRING, resource VARIANT, scope VARIANT, data VARIANT');
```

## Read Iceberg tables through catalog federation

Databricks reads Iceberg tables that another system writes as foreign tables in Unity Catalog. It federates the catalog that tracks them, then reads the metadata and Parquet files from your storage. The documented catalogs are AWS Glue, Hive metastores and [Snowflake](./snowflake.md) Horizon, for Snowflake-managed Iceberg tables. There is no federation with a generic Iceberg REST catalog, so Databricks cannot read Send2Bucket's [Apache Iceberg](../destinations/iceberg-rest.md) or [Amazon S3 Tables](../destinations/s3-tables.md) destinations; use a bucket destination and Auto Loader instead.

For Iceberg tables in AWS Glue, on Databricks Runtime 16.4 LTS or later, or a SQL warehouse:

1. Create an IAM role that can read the Glue catalog, and a Unity Catalog service credential for it.
2. Create a storage credential and an external location covering the tables' data, as in [Connect to bucket objects](#connect-to-bucket-objects).
3. Create the connection and the foreign catalog:

```sql
CREATE CONNECTION glue_iceberg TYPE glue
  OPTIONS (aws_region '<region>', aws_account_id '<account_id>', credential '<service_credential>');

CREATE FOREIGN CATALOG glue
  USING CONNECTION glue_iceberg
  OPTIONS (authorized_paths 's3://<bucket>/<table_path>/', storage_root 's3://<bucket>/<databricks_root>/');

SELECT count(*) FROM glue.<database>.<table>;
```

- `authorized_paths` limits which table locations the catalog may read; each must sit inside an external location. `storage_root` is required for Iceberg tables.
- Unity Catalog refreshes a foreign table's metadata at query time. `REFRESH FOREIGN TABLE glue.<database>.<table>` forces it, for example from a scheduled job.
- Foreign Iceberg tables are read-only. Iceberg v1, v2 and v3 tables with Parquet data are supported, but not v2 position or equality deletes, partition evolution, branches or tags. Time travel reaches only snapshots Databricks has already read.

## Example queries

These run against the `records` view or the `s2b_records` table. They use the Databricks path syntax `column:field::type` on `VARIANT` columns. On Apache Spark, use the `variant_get` versions on the [Apache Spark](./spark.md#example-queries) page; Databricks runs those too.

Records per signal:

```sql
SELECT signal, count(*) AS records
FROM records
GROUP BY ALL
ORDER BY records DESC;
```

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

```sql
SELECT data:event::string AS event,
       count(*) AS events,
       sum(data:cents::int) / 100 AS revenue_usd
FROM records
WHERE signal = 'event'
GROUP BY ALL
ORDER BY events DESC;
```

Log records by service and severity. `service.name` is one entry in the resource's attribute list. This query is the one we ran on Apache Spark 4.0:

```sql
SELECT variant_get(
         filter(variant_get(resource, '$.attributes', 'array<variant>'),
                a -> variant_get(a, '$.key', 'string') = 'service.name')[0],
         '$.value.stringValue', 'string') AS service,
       data:severityText::string AS severity,
       count(*) AS logs
FROM records
WHERE signal = 'log'
GROUP BY ALL;
```

The five slowest spans. The timestamps are nanosecond strings; cast them to `bigint` before subtracting:

```sql
SELECT data:name::string AS span,
       data:traceId::string AS trace_id,
       (data:endTimeUnixNano::bigint - data:startTimeUnixNano::bigint) / 1e6 AS duration_ms
FROM records
WHERE signal = 'span'
ORDER BY duration_ms DESC
LIMIT 5;
```

## Notes

- **Runtime versions.** `read_files` needs Databricks Runtime 13.3 LTS or later. Ingesting JSON as `VARIANT` needs 15.3 or later, and Databricks documents the `VARIANT` type from 15.4. On older runtimes, declare `resource`, `scope` and `data` as `STRING` and use the [Spark 3.5](./spark.md#spark-35) queries.
- **Iceberg destinations.** See [Read Iceberg tables through catalog federation](#read-iceberg-tables-through-catalog-federation): Databricks cannot federate an Iceberg REST catalog or S3 Tables, so use an [Amazon S3](../destinations/aws-s3.md) or [Google Cloud Storage](../destinations/gcs.md) destination for Databricks.
- **DuckLake destinations.** Databricks has no DuckLake reader. Use [DuckDB](./duckdb.md).
- **Costs.** `read_files` and Auto Loader in directory listing mode list the prefix on each run. For a bucket with many streams and months of objects, point the path at one stream, or see the Auto Loader documentation for file notification mode.
- Databricks documentation used: [read_files](https://docs.databricks.com/aws/en/sql/language-manual/functions/read_files), [Auto Loader options](https://docs.databricks.com/aws/en/ingestion/cloud-object-storage/auto-loader/options), [Ingest data as VARIANT](https://docs.databricks.com/aws/en/ingestion/variant), [Query VARIANT data](https://docs.databricks.com/aws/en/semi-structured/variant), [CREATE EXTERNAL LOCATION](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-location), [Apache Iceberg in Databricks](https://docs.databricks.com/aws/en/iceberg/), [AWS Glue federation](https://docs.databricks.com/aws/en/query-federation/hms-federation-glue), [catalog federation](https://docs.databricks.com/aws/en/query-federation/catalog-federation) and [REFRESH FOREIGN](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-refresh-foreign), checked 2026-11-29.
- For the same questions in other engines, see the [overview](./index.md).
