Wednesday, October 14, 2026

Databricks using glob pattern or wildcard folder path

 To access data from folders using a wildcard path in Databricks, you use glob patterns (like * or ?) within your file path strings. This allows Spark to match and load multiple files or folders simultaneously.

How to Access Data Using Wildcards
You can apply wildcards to file names, subfolders, or dates within your cloud storage paths (e.g., ADLS Gen2, AWS S3, or DBFS).
1. Reading Multiple Files in a Folder
If you want to read all CSV files inside a specific directory:
python
df = spark.read.format("csv") \
    .option("header", "true") \
    .load("dbfs:/mnt/data/2026/*.csv")

2. Reading from Partitioned or Dated Folders
If your data is structured by year, month, or day, you can use wildcards to aggregate data across specific periods:
python
# Loads data for all months and days in the year 2026
df = spark.read.format("parquet") \
    .load("dbfs:/mnt/data/year=2026/month=*/day=*/*.parquet")

3. Advanced Matching (Braces)
If you want to load data from specific folders only (e.g., just January and February):
python
df = spark.read.format("json") \
    .load("dbfs:/mnt/data/year=2026/month={01,02}/*.json")


Pros and Cons of Using Wildcard Paths
While wildcard paths are highly convenient, they come with architectural trade-offs, especially as data scales.
Pros
  • Simplicity: It requires minimal code. You do not need to write complex loops or logic to list directories and append DataFrames together.
  • Bulk Ingestion: Easily aggregates historical data scattered across hundreds of files or subdirectories into a single Spark DataFrame.
  • Dynamic Loading: Automatically includes new files that match the pattern without needing to update the hardcoded paths in your notebook.
Cons
  • Performance Overhead (Directory Listing): Spark must scan the cloud storage file system to resolve the wildcards before starting the job. If you have thousands of nested folders or millions of files, this listing process can cause massive delays before data processing even begins.
  • Schema Evolution Mismatch: If one of the files matched by the wildcard has a mutated or corrupt schema (e.g., a missing column or changed data type), it can cause the entire read operation to fail or result in corrupted data.
  • Lack of State Tracking: Wildcards treat data as a static snapshot during execution. It cannot inherently track which files have already been processed in a previous run, making it inefficient for incremental or streaming data pipelines.
  • File Size Inefficiencies: Wildcards often lead to reading many small files, which degrades Spark's performance (the "small file problem").

Alternative: What to Use Instead
  • For Delta Lake: If you convert your folders into Delta tables, you no longer need wildcards. You simply point Spark to the root directory (spark.read.table("my_table")), and Delta efficiently handles file discovery, schema enforcement, and partition pruning using transaction logs.
  • For Incremental Ingestion: Use Auto Loader (cloudFiles). Auto Loader efficiently scales to millions of files, automatically tracks state (so it never reprocesses the same file twice), and completely avoids the costly directory listing overhead associated with wildcards.

No comments:

Post a Comment

Databricks lakehouse architecture (Medallion Architecture)

  The Databricks Lakehouse architecture combines the best elements of data lakes and data warehouses into a single unified platform. Hist...