Databricks Data Engineer
Study Notes ā Unit 2.6 (Delta Lake Operations) & Unit 2.7 (Jobs & Task Orchestration)
Unit 2.6 ā Databricks Delta Lake Operations
1. Extracting Data Directly from Files
⢠Query a single file: SELECT * FROM file_format.`/path/to/file`
⢠Example: SELECT * FROM json.`${da.paths.datasets}/raw/events-kafka/001.json`
⢠Query an entire directory (same format & schema for all files) by pointing to the directory path instead of one file:
SELECT * FROM json.`${da.paths.datasets}/raw/events-kafka`
⢠Chain Spark logic: create a temp view from a query against a path, then reference it later:
CREATE OR REPLACE TEMP VIEW events_temp_view AS SELECT * FROM json.`$
{da.paths.datasets}/raw/events-kafka/`; SELECT * FROM events_temp_view
⢠Extract text files as raw strings:
SELECT * FROM text.`${da.paths.datasets}/raw/events-kafka/`
⢠binaryFile format ā for images/unstructured data. Returns fields: path, modificationTime, length, content.
SELECT * FROM binaryFile.`${da.paths.datasets}/raw/events-kafka/`
2. Providing Options for External Sources
⢠CSV is NOT self-describing (unlike JSON/Parquet) ā direct queries rarely give the desired result (header row may be
read as data, columns may be a single truncated column).
⢠General syntax to create a table with options against an external source:
CREATE TABLE table_identifier (col_name1 col_type1, ...) USING data_source OPTIONS
(key1 = "val1", key2 = "val2", ...) LOCATION path
⢠Example ā registering a CSV table with header & delimiter options:
CREATE TABLE sales_csv (order_id LONG, email STRING, transactions_timestamp LONG,
total_item_quantity INTEGER, purchase_revenue_in_usd DOUBLE, unique_items INTEGER,
items STRING) USING CSV OPTIONS (header = "true", delimiter = "|") LOCATION "$
{da.paths.working_dir}/sales-csv"
⢠DESCRIBE EXTENDED table_name ā shows detailed table metadata.
3. Limits of Tables with External Data Sources
⢠Tables over external sources do NOT get Delta Lake / Lakehouse performance guarantees.
⢠Delta Lake tables always show the most recent data; tables on external sources may show older cached versions.
⢠If an external system updates files directly (bypassing Spark), Spark isn't notified ā you must manually refresh:
REFRESH TABLE sales_csv
4. CTAS ā CREATE TABLE AS SELECT
⢠Automatically infers schema from query results ā does NOT support manual schema declaration.
⢠Useful for external data with well-defined schema (e.g., Parquet).
⢠Does NOT support specifying additional file options ā a limitation for CSV ingestion.