Databricks¶
Databricks platform implementation (Unity Catalog / DBFS).
- class scamander.processor.databricks.Databricks(*args, **kwargs)¶
Basisklassen:
PlatformPlatform implementation for Databricks (Unity Catalog / DBFS environments).
Instantiated automatically by
Platform.__new__when the runtime is identified as Databricks (viaDATABRICKS_RUNTIME_VERSIONorDB_CLUSTER_IDenvironment variables). UsesDBUtilsfor all filesystem operations.Example:
from scamander import Platform # Running on a Databricks cluster: Platform() auto-detects and # returns a Databricks instance transparently. platform = Platform() platform.dbutils.fs.ls("/mnt/raw")
- check_file_exists(file_name: str, raise_exceptions=False) bool¶
Checks whether a file or directory exists on the Databricks filesystem (DBFS).
Uses
dbutils.fs.lsto probe the path; any exception is treated as “not found” rather than propagated (unlessraise_exceptions=True).- Parameters:
file_name (str) – DBFS path to check, e.g.
"dbfs:/mnt/raw/config.json".raise_exceptions (bool) – When
True, raisesFileNotFoundErrorif the path does not exist instead of returningFalse. Defaults toFalse.
- Returns:
Trueif the path exists,Falseotherwise.- Return type:
bool
- Veroorzaakt:
FileNotFoundError – Only when
raise_exceptions=Trueand the path is missing.
- create_empty_table_from_metadata(metadata: dict)¶
Creates an empty Delta table from a metadata definition dictionary, including
NOT NULL,PRIMARY KEY, andFOREIGN KEYconstraints.When
metadata["track_history"]isTrue, five SCD Type-2 history-tracking columns are appended:HashSleutel,HashWaarde,GeldigVan,GeldigTot,IsActief.- Parameters:
metadata (dict) – Metadata dictionary with
"table","columns", and"track_history"keys.
- get_dbutils()¶
Retrieves a DBUtils instance, supporting both interactive cluster and job cluster environments.
Attempts to import
pyspark.dbutils.DBUtilsfirst (job clusters / newer runtimes); falls back to readingdbutilsfrom the IPython user namespace (interactive notebooks).- Returns:
An initialised Databricks Utilities object.
- Return type:
DBUtils
- get_notebook_name() str¶
Returns the base name of the currently executing Databricks notebook.
- Returns:
The notebook’s base name, e.g.
"Ingest_S4SD".- Return type:
str
- get_notebook_url() str¶
Returns a browser-accessible URL to the currently executing Databricks notebook.
- Returns:
A full HTTPS URL pointing to the notebook in the Databricks workspace UI.
- Return type:
str
- move_file(source: str, destination: str, overwrite=True, create_missing_dirs: bool = False) bool¶
Moves a file from source to destination on DBFS using
dbutils.fs.mv.- Parameters:
source (str) – DBFS path to the source file.
destination (str) – DBFS target path.
overwrite (bool) – If
True(default), remove an existing destination file before moving.create_missing_dirs (bool) – If
True, create the destination directory if it does not already exist. Defaults toFalse.
- Returns:
Trueon success,Falseon any failure.- Return type:
bool
- resolve_filemask(mask_path: str, case_sensitive: bool = False, full_path: bool = False) List[str]¶
Resolves a filemask with wildcard into a list of matching file names.
Splits the path on the last
/to obtain the folder and glob pattern, lists the folder viadbutils.fs.ls, and returns names of files whose base name matches the pattern. Matching is case-insensitive by default.- Parameters:
mask_path (str) – Full path including a wildcard, e.g.
"dbfs:/mnt/raw/sales_*.csv".case_sensitive (bool) – When
True, match file names exactly as cased. Defaults toFalse.full_path (bool) – When
True, return full paths (folder ofmask_pathjoined with each name). Defaults toFalse.
- Returns:
- Sorted list of matching file base names, or full paths when
full_path=True. Returns an empty list if the folder does not exist or no files match.
- Return type:
list[str]
- validate_compatibility()¶
No additional compatibility checks are required for Databricks.