HDFS Operations
The HDFS operations module provides utilities for working with Hadoop Distributed File System operations, including file management, configuration, and environment setup.
Module Overview
Abstract HDFS Operations - Fully Configurable and Reusable Zero hard-coded project dependencies
- class siege_utilities.distributed.hdfs_operations.AbstractHDFSOperations[source]
Bases:
objectConfigurable HDFS operations for distributed pipelines.
Wraps the small set of HDFS interactions the library needs — status check, Spark session construction, distributed dependency staging — behind a single class parameterized by project-specific settings (cache locations, Hadoop timeout, hash functions).
- Parameters:
config (HDFSConfig) – Project-specific configuration object. Must expose a
cache_directorypath,get_cache_pathmethod,hdfs_timeout, and optionalhash_func/quick_signature_funcoverrides. Seecreate_hdfs_operations()andsetup_distributed_environment()for common construction patterns.
Notes
Instantiation eagerly creates
config.cache_directoryif absent. All HDFS interactions log throughconfig.log_info/config.log_errorrather than raising, matching the pre-check convention used elsewhere in the distributed module.- create_spark_session()[source]
Create Spark session using configuration.
Supports local, standalone cluster, and YARN deployments based on the master URL in the config.
- sync_directory_to_hdfs(local_path=None, hdfs_subdir='inputs')[source]
Sync local directory/file to HDFS with proper verification.
- Raises:
ValueError – If no data path is provided.
FileNotFoundError – If the local path does not exist.
RuntimeError – If HDFS is not accessible or sync verification fails.
subprocess.CalledProcessError – If an HDFS command fails.
subprocess.TimeoutExpired – If an HDFS command times out.
- Parameters:
- Return type:
- setup_distributed_environment(data_path=None, dependency_paths=None)[source]
Main setup function with proper verification.
- Returns:
Tuple of (spark_session, data_url, None).
- Raises:
ValueError – If no data path is provided.
FileNotFoundError – If the data path does not exist.
ImportError – If PySpark is not available.
RuntimeError – If HDFS sync or Spark session creation fails.
- Parameters:
- siege_utilities.distributed.hdfs_operations.create_hdfs_operations(config)[source]
Factory for
AbstractHDFSOperationsinstances.Wraps the constructor so callers can request an HDFS operations object without importing the class name directly — useful for keeping consumer code decoupled from the concrete implementation.
- Parameters:
config (HDFSConfig) – Project-specific HDFS config (see
AbstractHDFSOperations).- Returns:
A ready-to-use HDFS operations instance.
- Return type:
- siege_utilities.distributed.hdfs_operations.setup_distributed_environment(config, data_path=None, dependency_paths=None)[source]
One-shot helper to construct HDFS ops + stage data + build a Spark session.
Equivalent to
AbstractHDFSOperations(config).setup_distributed_environment(...)— provided as a top-level convenience so callers who only need a single distributed-setup call don’t have to instantiate the class explicitly.- Parameters:
config (HDFSConfig) – Project-specific HDFS config (see
AbstractHDFSOperations).data_path (str, optional) – Local path to a data payload that should be staged onto HDFS before the Spark session starts. When
None, no staging happens.dependency_paths (list of str, optional) – Additional local paths (e.g. Python source directories) to zip and stage as PySpark dependencies. When
None, only the library’s own default dependency bundle is staged.
- Returns:
(spark_session, staged_data_url, dependency_zip_url)— matching the tuple returned byAbstractHDFSOperations.setup_distributed_environment(). Both URLs areNonewhen their corresponding argument was omitted.- Return type:
Functions
- siege_utilities.distributed.hdfs_operations.create_hdfs_operations(config)[source]
Factory for
AbstractHDFSOperationsinstances.Wraps the constructor so callers can request an HDFS operations object without importing the class name directly — useful for keeping consumer code decoupled from the concrete implementation.
- Parameters:
config (HDFSConfig) – Project-specific HDFS config (see
AbstractHDFSOperations).- Returns:
A ready-to-use HDFS operations instance.
- Return type:
- siege_utilities.distributed.hdfs_operations.setup_distributed_environment(config, data_path=None, dependency_paths=None)[source]
One-shot helper to construct HDFS ops + stage data + build a Spark session.
Equivalent to
AbstractHDFSOperations(config).setup_distributed_environment(...)— provided as a top-level convenience so callers who only need a single distributed-setup call don’t have to instantiate the class explicitly.- Parameters:
config (HDFSConfig) – Project-specific HDFS config (see
AbstractHDFSOperations).data_path (str, optional) – Local path to a data payload that should be staged onto HDFS before the Spark session starts. When
None, no staging happens.dependency_paths (list of str, optional) – Additional local paths (e.g. Python source directories) to zip and stage as PySpark dependencies. When
None, only the library’s own default dependency bundle is staged.
- Returns:
(spark_session, staged_data_url, dependency_zip_url)— matching the tuple returned byAbstractHDFSOperations.setup_distributed_environment(). Both URLs areNonewhen their corresponding argument was omitted.- Return type:
Usage Examples
Basic HDFS operations setup:
File operations:
File transfer operations:
Batch operations:
Unit Tests
The HDFS operations module has comprehensive test coverage:
Test Results: All HDFS operations tests pass successfully with comprehensive coverage.