cdm_data_loaders.utils.spark ¶
Utilities for interacting with Spark.
get_spark ¶
Set up a Spark session and namespace.
If no app_name is provided, the function will use the string "cdm_data_loader".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name
|
str | None
|
name for the app (used for the Spark session), defaults to None |
None
|
kwargs
|
dict[str, Any] | None
|
extra arguments to supply to the spark session |
{}
|
Returns:
| Type | Description |
|---|---|
SparkSession
|
a Spark session |
Source code in src/cdm_data_loaders/utils/spark.py
set_up_workspace ¶
set_up_workspace(
app_name: str | None = None,
namespace: str | None = None,
tenant_name: str | None = None,
**kwargs: str | int | bool | dict | None
) -> tuple[SparkSession, str]
Set up a Spark session and namespace.
If no app_name is provided, the function will use the string "cdm_data_loader".
N.b. this is the only function where the "raw" namespace and tenant are specified. All other functions use the catalog_db namespace generated by notebook_utils (from parent spark_notebook docker container)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name
|
str | None
|
name for the app (used for the Spark session), defaults to None |
None
|
namespace
|
str | None
|
namespace for data I/O, defaults to None |
None
|
tenant_name
|
str | None
|
name of the tenant, if applicable; defaults to None |
None
|
kwargs
|
str | int | bool | dict | None
|
extra arguments to supply to the spark session |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[SparkSession, str]
|
a Spark session and the BERDL-approved namespace |
Source code in src/cdm_data_loaders/utils/spark.py
get_existing_database_save_dir ¶
Check whether a table is already registered in the Hive metastore, and if so, where data is saved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spark
|
SparkSession
|
spark session |
required |
catalog_db
|
str
|
catalog_db name |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
path to the existing table data or None |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the table exists but the SQL query to retrieve the description returns unexpected results |
Source code in src/cdm_data_loaders/utils/spark.py
get_existing_table_save_dir ¶
get_existing_table_save_dir(
spark: SparkSession,
catalog_db: str,
table: str | None = None,
) -> str | None
Check whether a table is already registered in the Hive metastore, and if so, where data is saved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spark
|
SparkSession
|
spark session |
required |
catalog_db_table
|
str
|
catalog_db.table name |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
path to the existing table data or None |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the table exists but the SQL query to retrieve the description returns unexpected results |
Source code in src/cdm_data_loaders/utils/spark.py
write_table ¶
write_table(
spark: SparkSession,
sdf: DataFrame,
catalog_db: str,
table: str,
mode: str = DEFAULT_WRITE_MODE,
) -> None
Write data as database tables and register it in the catalog.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spark
|
SparkSession
|
spark sesh |
required |
sdf
|
DataFrame
|
data frame to be written |
required |
catalog_db
|
str
|
catalog database name assigned by |
required |
table
|
str
|
table name |
required |
mode
|
str
|
spark write mode; one of the modes specified in WRITE_MODE |
DEFAULT_WRITE_MODE
|
Source code in src/cdm_data_loaders/utils/spark.py
preview_or_skip ¶
Preview table if it exists.