SparkDatasetV2¶
SparkDatasetV2 loads and saves data using Apache Spark DataFrames with improved support for Spark Connect, Databricks Connect, and automatic Pandas DataFrame conversion.
kedro_datasets.spark.SparkDatasetV2 ¶
SparkDatasetV2(
*,
filepath,
file_format="parquet",
load_args=None,
save_args=None,
version=None,
credentials=None,
metadata=None
)
Bases: AbstractVersionedDataset
SparkDatasetV2 loads and saves Spark dataframes.
Examples:
Using the YAML API:
weather:
type: spark.SparkDatasetV2
filepath: s3a://your_bucket/data/01_raw/weather/*
file_format: csv
load_args:
header: True
inferSchema: True
save_args:
sep: '|'
header: True
weather_with_schema:
type: spark.SparkDatasetV2
filepath: s3a://your_bucket/data/01_raw/weather/*
file_format: csv
load_args:
header: True
schema:
filepath: path/to/schema.json
save_args:
sep: '|'
header: True
weather_cleaned:
type: spark.SparkDatasetV2
filepath: data/02_intermediate/data.parquet
file_format: parquet
# Databricks with Unity Catalog
unity_data:
type: spark.SparkDatasetV2
filepath: /Volumes/catalog/schema/volume/data.parquet
# Databricks with DBFS
dbfs_data:
type: spark.SparkDatasetV2
filepath: /dbfs/mnt/data/output.parquet
Using the Python API:
>>> import tempfile
>>> from pyspark.sql import Row, SparkSession
>>> from pyspark.sql.types import IntegerType, StringType, StructField, StructType
>>>
>>> schema = StructType(
... [StructField("name", StringType(), True), StructField("age", IntegerType(), True)]
... )
>>> data = [("Alex", 31), ("Bob", 12), ("Clarke", 65), ("Dave", 29)]
>>> spark_df = SparkSession.builder.getOrCreate().createDataFrame(data, schema)
>>>
>>> with tempfile.TemporaryDirectory() as tmp_dir:
... filepath = f"{tmp_dir}/test_data"
... dataset = SparkDatasetV2(filepath=filepath)
... dataset.save(spark_df)
... reloaded = dataset.load()
... assert Row(name="Bob", age=12) in reloaded.take(4)
You can also save Pandas DataFrames directly they will be automatically converted to Spark DataFrames:
>>> import pandas as pd
>>> pandas_df = pd.DataFrame({"name": ["Alex", "Bob"], "age": [31, 12]})
>>> dataset.save(pandas_df) # Automatically converts to Spark DataFrame
Parameters:
-
filepath(str) –Filepath in POSIX format to a Spark dataframe. Supports: - Local paths:
data/output.parquetor/absolute/path/data.parquet- S3:s3://bucket/pathors3a://bucket/path- GCS:gs://bucket/path- Azure:abfs://container@account.dfs.core.windows.net/path- Databricks DBFS:/dbfs/pathordbfs:/path- Unity Catalog:/Volumes/catalog/schema/volume/path -
file_format(str, default:'parquet') –File format used during load and save operations. These are formats supported by the running SparkContext include parquet, csv, delta. For a list of supported formats please refer to Apache Spark documentation at https://spark.apache.org/docs/latest/sql-programming-guide.html
-
load_args(dict[str, Any] | None, default:None) –Load args passed to Spark DataFrameReader load method. It is dependent on the selected file format. You can find a list of read options for each supported format in Spark DataFrame read documentation: https://spark.apache.org/docs/latest/api/python/getting_started/quickstart_df.html
-
save_args(dict[str, Any] | None, default:None) –Save args passed to Spark DataFrame write options. Similar to load_args this is dependent on the selected file format. You can pass
modeandpartitionByto specify your overwrite mode and partitioning respectively. You can find a list of options for each format in Spark DataFrame write documentation: https://spark.apache.org/docs/latest/api/python/getting_started/quickstart_df.html -
version(Version | None, default:None) –If specified, should be an instance of
kedro.io.core.Version. If itsloadattribute is None, the latest version will be loaded. If itssaveattribute is None, save version will be autogenerated. -
credentials(dict[str, Any] | None, default:None) –Credentials to access cloud storage, such as
key,secretfor S3,tokenfor GCS, oraccount_keyfor Azure. Structure depends on the cloud provider. -
metadata(dict[str, Any] | None, default:None) –Any arbitrary metadata. This is ignored by Kedro, but may be consumed by users or external plugins.
Source code in kedro_datasets/spark/spark_dataset_v2.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
_load_args
instance-attribute
¶
_load_args = {
None: DEFAULT_LOAD_ARGS,
None: (
deepcopy(load_args) if load_args is not None else {}
),
}
_save_args
instance-attribute
¶
_save_args = {
None: DEFAULT_SAVE_ARGS,
None: (
deepcopy(save_args) if save_args is not None else {}
),
}
_spark_path
property
¶
_spark_path
Get the Spark-compatible path for this dataset.
Returns:
-
Path formatted for Spark (e.g., 's3a–//bucket/path', 'file:///path').
_describe ¶
_describe()
Describe the dataset configuration.
Returns:
-
dict[str, Any]–Dictionary with dataset configuration details.
Source code in kedro_datasets/spark/spark_dataset_v2.py
324 325 326 327 328 329 330 331 332 333 334 335 336 | |
_exists ¶
_exists()
Check if the dataset exists.
Returns:
-
bool–True if the dataset exists, False otherwise.
Source code in kedro_datasets/spark/spark_dataset_v2.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
_get_filesystem_ops ¶
_get_filesystem_ops(protocol, filepath, credentials)
Get filesystem operations for exists and glob.
Parameters:
-
protocol(str) –Filesystem protocol.
-
filepath(str) –Original filepath.
-
credentials(dict[str, Any]) –Credentials for filesystem access.
Returns:
-
tuple–Tuple of (exists_function, glob_function).
Source code in kedro_datasets/spark/spark_dataset_v2.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
_handle_delta_format ¶
_handle_delta_format()
Handle delta-specific configurations.
Source code in kedro_datasets/spark/spark_dataset_v2.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
_load ¶
_load()
Loads data from filepath.
Returns:
-
DataFrame–Data from filepath as pyspark dataframe.
Source code in kedro_datasets/spark/spark_dataset_v2.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
_save ¶
_save(data)
Saves pyspark dataframe.
Parameters:
-
data(DataFrame | DataFrame) –PySpark DataFrame or Pandas DataFrame to save. Pandas DataFrames will be automatically converted to Spark.
Source code in kedro_datasets/spark/spark_dataset_v2.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |