spark-python-data-source

Build custom PySpark data source connectors for batch and streaming reads and writes.

Updated Mar 24, 2026
One-click install
npx skills add https://github.com/AarushiShah/coding-agents-databricks-apps --skill spark-python-data-source-aarushishah
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: spark-python-data-source
Source: https://github.com/AarushiShah/coding-agents-databricks-apps/tree/main/.claude/skills/spark-python-data-source
Command: npx skills add https://github.com/AarushiShah/coding-agents-databricks-apps --skill spark-python-data-source-aarushishah

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pyspark, pytest, pytest-spark, requests, poetry, and includes references (resource) components.

What problem does it solve? Connecting Apache Spark to external systems without native support—databases, REST APIs, message queues—requires writing custom connectors, and doing it wrong leads to broken streaming offsets, failed retries, and unmaintainable code. ## Core Features & Use Cases - Batch & Streaming Connectors: Implement DataSource, DataSourceReader/Writer, and DataSourceStreamReader/Writer classes following a flat, single-level inheritance pattern. - Production Patterns: Reference guides cover partitioning strategies (time-based, token-range, ID-range), multi-method authentication, type conversion, error handling with retries and circuit breakers, and exactly-once streaming semantics. - Testing & Validation: Includes pytest patterns with mocked HTTP calls, Testcontainers integration tests, and a code review checklist enforcing simplicity. - Use Case: Build a streaming connector for RabbitMQ with at-least-once delivery, or a batch writer for a REST API with OAuth2 authentication and pagination. ## Quick Start Create a Spark data source for reading from MongoDB with sharding support using the PySpark DataSource API.

Frequently Asked Questions about spark-python-data-source

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I build a custom Spark data source in Python?

Implement a DataSource subclass with name, schema, reader, and writer methods, then register it with spark.dataSource.register. Readers extend DataSourceReader or DataSourceStreamReader, and writers extend DataSourceWriter or DataSourceStreamWriter, using a flat single-level inheritance structure.

How to implement a streaming reader with the PySpark DataSource API?

Extend DataSourceStreamReader and implement initialOffset, latestOffset, partitions(start, end), and commit. Offsets must be JSON-serializable, and partitions should be non-overlapping—typically by adjusting boundaries by one microsecond to prevent duplicate records between batches.

What partitioning strategies work for Spark custom data sources?

Use time-based partitions for temporal APIs, token-range partitioning for distributed databases like Cassandra, and ID-range partitioning for paginated APIs. Aim for 128MB to 1GB per partition and 2-4x executor cores for batch reads.

Does the PySpark DataSource API support exactly-once streaming writes?

Yes, through idempotent writes in DataSourceStreamWriter. Generate deterministic idempotency keys from batch ID, partition ID, and row content, then skip records already written, combined with commit and abort callbacks for batch lifecycle management.

How do I test a custom Spark data source without the external system?

Mock HTTP calls with unittest.mock.patch for unit tests against readers and writers. For integration tests, use Testcontainers to spin up real systems like PostgreSQL and verify end-to-end reads and writes with a local Spark session.

What authentication methods can a Spark data source support?

Support multiple methods in priority order: Databricks Unity Catalog credentials, cloud default credentials like managed identity, service principals, API keys, and username/password. Validate that at least one method is configured and never log sensitive values.