pyspark-huggingface is a Spark Data Source for seamlessly accessing 🤗 Hugging Face Datasets as Spark DataFrames. It enables streaming datasets from the Hub, applying projection and predicate filters, and saving Spark DataFrames back to Hugging Face as Parquet files with fast, deduplicated uploads. It supports authentication via `huggingface-cli login` or tokens, and is compatible with Spark 4 (with auto-import) as well as backporting functionality for Spark 3.5, 3.4, and 3.3. The current version is 2.1.0 and it is actively maintained.
pip install pyspark-huggingfaceVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes a Spark session and demonstrates how to read a public Hugging Face dataset (stanfordnlp/imdb) into a PySpark DataFrame. It also shows where to configure authentication for private datasets or write operations. For Spark 3.x users, an explicit `import pyspark_huggingface` might be necessary.
Add `import pyspark_huggingface` at the beginning of your Spark application when using PySpark 3.x.
Authenticate using `huggingface-cli login` in your environment or pass your Hugging Face token directly via the `.option("token", "hf_xxxx")` to the `spark.read` or `spark.write` calls, or set the `HF_TOKEN` environment variable.Use `.option("filters", '[("column_name", ">", value)]')` and `.option("columns", '["col1", "col2"]')` when loading the dataset. Example: `.option("filters", '[("language_score", ">", 0.99)]').option("columns", '["text", "language_score"]')`.Set the `HF_HUB_CACHE` environment variable to a path with sufficient available disk space *before* importing any `transformers` or `datasets` libraries. For example: `os.environ['HF_HUB_CACHE'] = "/path/to/large/storage"`.
Ensure your `datasets` library (a common underlying dependency) is updated to version 2.15.0 or later, as this version includes improvements to handle such rate limits. `pip install --upgrade datasets`.