PySpark Data Sources
Custom Apache Spark data sources using the Python Data Source API (Spark 4.0+). Learn by example and build your own data sources.
Quick Start
Installation
pip install pyspark-data-sources
# Install with specific extras
pip install pyspark-data-sources[faker] # For FakeDataSource
pip install pyspark-data-sources[all] # All optional dependencies
Requirements
- Apache Spark 4.0+ or Databricks Runtime 15.4 LTS+
- Python 3.9-3.12
Basic Usage
from pyspark.sql import SparkSession
from pyspark_datasources import FakeDataSource
# Create Spark session
spark = SparkSession.builder.appName("datasource-demo").getOrCreate()
# Register the data source
spark.dataSource.register(FakeDataSource)
# Read batch data
df = spark.read.format("fake").option("numRows", 5).load()
df.show()
# +--------------+----------+-------+------------+
# | name| date|zipcode| state|
# +--------------+----------+-------+------------+
# | Pam Mitchell|1988-10-20| 23788| Tennessee|
# |Melissa Turner|1996-06-14| 30851| Nevada|
# | Brian Ramsey|2021-08-21| 55277| Washington|
# | Caitlin Reed|1983-06-22| 89813|Pennsylvania|
# | Douglas James|2007-01-18| 46226| Alabama|
# +--------------+----------+-------+------------+
# Stream data
stream = spark.readStream.format("fake").load()
query = stream.writeStream.format("console").start()
Available Data Sources
Sources (Read)
| Data Source | Type | Description | Dependency |
|---|---|---|---|
fake |
Batch/Stream | Generate synthetic test data using Faker | [faker] |
github |
Batch | Read GitHub pull requests | None |
googlesheets |
Batch | Read public Google Sheets | None |
huggingface |
Batch | Load Hugging Face datasets | [huggingface] |
stock |
Batch | Fetch stock market data (Alpha Vantage) | None |
opensky |
Batch/Stream | Live flight tracking data | None |
kaggle |
Batch | Load Kaggle datasets | [kaggle] |
arrow |
Batch | Read Apache Arrow files | [arrow] |
robinhood |
Batch | Read cryptocurrency market data from Robinhood API | [robinhood] |
jsonplaceholder |
Batch | Read JSON data for testing | None |
weather |
Batch | Read current weather data (OpenWeatherMap) | None |
Sinks (Write)
| Data Source | Type | Description | Dependency |
|---|---|---|---|
lance |
Batch Write | Write Lance vector format | [lance] |
salesforce |
Stream Write | Write to Salesforce objects | [salesforce] |
meta_capi |
Batch/Stream Write | Write to Meta Conversions API | None |
📚 See detailed examples for all data sources →
Example: Generate Fake Data
from pyspark_datasources import FakeDataSource
spark.dataSource.register(FakeDataSource)
# Generate synthetic data with custom schema
df = spark.read.format("fake") \
.schema("name string, email string, company string") \
.option("numRows", 5) \
.load()
df.show(truncate=False)
# +------------------+-------------------------+-----------------+
# |name |email |company |
# +------------------+-------------------------+-----------------+
# |Christine Sampson |johnsonjeremy@example.com|Hernandez-Nguyen |
# |Yolanda Brown |williamlowe@example.net |Miller-Hernandez |
# +------------------+-------------------------+-----------------+
Building Your Own Data Source
Here's a minimal example to get started:
from pyspark.sql.datasource import DataSource, DataSourceReader
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
class MyCustomDataSource(DataSource):
def name(self):
return "mycustom"
def schema(self):
return StructType([
StructField("id", IntegerType()),
StructField("name", StringType())
])
def reader(self, schema):
return MyCustomReader(self.options, schema)
class MyCustomReader(DataSourceReader):
def __init__(self, options, schema):
self.options = options
self.schema = schema
def read(self, partition):
# Your data reading logic here
for i in range(10):
yield (i, f"name_{i}")
# Register and use
spark.dataSource.register(MyCustomDataSource)
df = spark.read.format("mycustom").load()
📖 Complete guide with advanced patterns →
Documentation
- 📚 Data Sources Guide - Detailed examples for each data source
- 🔧 Building Data Sources - Complete tutorial with advanced patterns
- 📖 API Reference - Full API specification and method signatures
- 💻 Development Guide - Contributing and development setup
Requirements
- Apache Spark 4.0+ or Databricks Runtime 15.4 LTS+
- Python 3.9-3.12
Contributing
We welcome contributions! See our Development Guide for details.
Resources
Community Tools
- Polymo - Declarative REST API ingestion. Medium article
Release files for pyspark-data-sources 0.1.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyspark_data_sources-0.1.11.tar.gz | 37.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyspark_data_sources-0.1.11-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 85.2 kB
Release files / pyspark_data_sources-0.1.11.tar.gz
| Download URL | pyspark_data_sources-0.1.11.tar.gz |
|---|---|
| Size | 37.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a50ad178989d35880979617c2c5e5edddd473e8e4e41a8a7666c01fb2e7bcda2
|
|
BLAKE2b-256 checksum How to use checksums |
76dfa5182bc688d94c588fe843ba4b62c74f3fda6d3d0e744ef1eeea06f9d6aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.4
|
Release files / pyspark_data_sources-0.1.11-py3-none-any.whl
| Download URL | pyspark_data_sources-0.1.11-py3-none-any.whl |
|---|---|
| Size | 48.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
057bdab17edfdff2b742c55b80ec7b28ccb642ef4c66c63a141111185edc007c
|
|
BLAKE2b-256 checksum How to use checksums |
af23effd4f138e14ad8b4777ed78ab70d6d271a17ef4630b3d39e95663903d7b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.4
|