Skip to main content

spark_storage_conf()

Build the Hadoop fs.azure.* keys that let Spark read ADLS Gen2.

Usage

Source

spark_storage_conf(
    auth_type="refresh_token",
    *,
    storage_account=None,
    tenant_id=None,
    client_id=None,
    client_secret=None,
    refresh_token=None,
    account_key=None,
    token_file=None,
    authority_host=None,
    prefix="spark.hadoop"
)

A port of azr::azure_spark_storage_conf(): with arguments passed explicitly the keys and values are azr’s, key for key. With the default prefix="spark.hadoop" the result goes to SparkSession.builder.config(map=...) or a cluster’s Spark config; prefix=None gives the raw fs.azure.* keys for core-site.xml or hadoopConfiguration().set().

Arguments another auth_type uses are ignored, as in azr. An empty environment variable counts as unset.

Parameters

auth_type: AuthType = "refresh_token"

How Hadoop authenticates:

  • "refresh_token": a delegated user (RefreshTokenBasedTokenProvider). Uses refresh_token; client_id and tenant_id, when passed, win over the token’s own.
  • "client_secret": a service principal (ClientCredsTokenProvider). client_id, tenant_id and client_secret default to $AZURE_CLIENT_ID, $AZURE_TENANT_ID and $AZURE_CLIENT_SECRET.
  • "workload_identity": Kubernetes workload identity (WorkloadIdentityTokenProvider), with client_id, tenant_id and token_file from $AZURE_CLIENT_ID, $AZURE_TENANT_ID and $AZURE_FEDERATED_TOKEN_FILE, which the AKS webhook sets. Needs Hadoop 3.4.1+ (Spark 4.x, Databricks 14.3+ LTS, Synapse 3.4+).
  • "managed_identity": IMDS (MsiTokenProvider). client_id (else $AZURE_CLIENT_ID) selects a user-assigned identity; without one the key is left out and Hadoop uses the system-assigned identity. tenant_id defaults to $AZURE_TENANT_ID, else "common".
  • "shared_key": the storage account key; needs storage_account and account_key.
storage_account: str | None = None

Scope every key to this account’s DFS host. A short name is expanded with the DFS suffix of the cloud authority_host names (public, US Government or China); a name with a dot is used as is. Default: keys for every account.

tenant_id: str | None = None

The tenant (GUID or domain) whose token endpoint Hadoop calls.

client_id: str | None = None

The client (application) ID: the token’s client for "refresh_token", the service principal or identity otherwise.

client_secret: str | None = None

The service principal’s secret, for "client_secret".

refresh_token: str | SupportsRefreshToken | None = None

For "refresh_token": a string, a SupportsRefreshToken such as refresh_token_from_az_cli(), or None for $AZURE_REFRESH_TOKEN. A SupportsRefreshToken supplies its own client ID and tenant; a string takes $AZURE_CLIENT_ID, else AZURE_CLI_CLIENT_ID, and $AZURE_TENANT_ID, else "common".

account_key: str | None = None

The storage account key, for "shared_key".

token_file: str | None = None

The federated token file, for "workload_identity".

authority_host: str | None = None

The Entra authority host; default $AZURE_AUTHORITY_HOST, else login.microsoftonline.com. May carry a scheme and trailing slash.

prefix: str | None = "spark.hadoop"
Prepended to every key with a dot; None for the raw Hadoop keys.

Returns

SparkStorageConf
A dict[str, str] whose repr and str redact the secrets.

Raises

ValueError

An unknown auth_type, an empty argument, a tenant ID that is not valid, or a value auth_type needs that is neither passed nor in its environment variable.

TypeError

An argument is not a string.

RefreshTokenNotFoundError
auth_type="refresh_token" with no refresh_token and no $AZURE_REFRESH_TOKEN.

Notes

Neither PySpark nor the Azure SDK builds these keys; Hadoop’s ABFS driver redeems the token itself. Two details carried over from azr: RefreshTokenBasedTokenProvider reads oauth2.refresh.token.endpoint, not oauth2.client.endpoint, and the MSI authority needs its trailing /.

Examples

from pyspark.sql import SparkSession
from dazapiz import refresh_token_from_az_cli, spark_storage_conf

conf = spark_storage_conf(refresh_token=refresh_token_from_az_cli())
spark = SparkSession.builder.config(map=conf).getOrCreate()
spark.read.parquet("abfss://data@myaccount.dfs.core.windows.net/sales/")

A service principal, for one account only:

conf = spark_storage_conf("client_secret", storage_account="myaccount")