Data Engineering Expert

Local PySpark Testing with Databricks Connect & Devcontainers

Reading time
6 ​​min

TL;DR:

Installing Databricks Connect disrupts standard local testing because it replaces the native PySpark library—which is built to run Spark sessions directly on your machine—with a specialized version designed exclusively for remote connectivity. This incompatibility makes traditional local Spark unit testing impossible within the same environment. To maintain a functional test suite, you must shift your development architecture away from local Spark execution and toward a remote-session model, such as hosting a local spark master server via containers.

Testing serves as a cornerstone for any production-ready application, boosting reliability across local development as well as pipelines. Yet, the transfer to cloud-native ecosystems like Databricks can lead to unexpected difficulties. We discovered such a difficulty by the use of the Databricks-owned client library Databricks Connect.

Why? The integration of the library can break local testing workflows.

At Real I.S. AG, we are developing a data integration platform powered by Databricks. As part of our toolkit, we utilize Databricks Connect. However, we identified that this specific package requires special handling of running tests.

This post explores the technical nuances of this error, provides a deep dive into the mechanics of Databricks Connect, and outlines our solution for maintaining a seamless, working test environment.

Databricks Connect as glue between you and Databricks

Apache Spark is one of the leading distributed processing and analytics engines, being used in a variety of our projects. The PySpark package refers to the standard, native way of interacting with Spark using Python. Under the hood, it installs in its package Java dependencies to create local SparkSessions directly on your development machine. This approach is powerful for local development and testing, as it allows you to run Spark computations without connecting to a remote cluster:

A modern Databricks setup often utilizes the client library Databricks Connect. This package enables the integration of various IDE’s such as PyCharm or VSCode. This enables features like access to Databricks compute, debugging functionality, and more. Connect can be installed as usual as package:

Onboarding Databricks Connect

Attempting to use the native PySpark approach (creating a local SparkSession) in an environment configured for Databricks Connect will result in a RuntimeError, as Databricks Connect only supports remote sessions:

The reason for this: Databricks Connect will replace your existing PySpark package with a specialized version for Databricks. As such, it is designed for remote sessions only, removing the capability to host local sessions.

  • Digging into the Connect package clarifies this behaviour. Only remote configuration settings are evaluated. If those are missing, the above RuntimeError is raised by default.

Instead of running Spark locally, it must establish a connection from your IDE to a remote Spark cluster, e.g. a high-performance cluster hosted on Databricks.

  • For the sake of readability, we will continue with a setup working with remote sessions locally. If you are interested in the details, how and why Databricks Connect replaces certain packages such as PySpark, jump to “An excursion into Databricks Connect” below.

To connect to a Spark server, you can still use the remote session builder from the replaced PySpark package:

The implication for running local tests is that you have to provide a Spark server. Containers will simplify this drastically.

Devcontainers to the rescue

Devcontainers provide a modern approach to keeping the development environment reproducible and consistent. A typical approach is a single container, mirroring the runtime and libraries of the production system.

The requirement of a remote SparkSession can be solved by the introduction of a second devcontainer: an open-source Spark server, provided as Docker image.

Devcontainers with spark master

After configuring the SparkSession Builder to reference the container, a remote SparkSession can finally be established using Spark Connect.

Integrating pytest

A parameterized test using an agnostic Spark as fixture might be:

Using the multi devcontainer setup, the fixture can be constructed as follows:

Running pytest now leads to the desired output: A locally running SparkSession using Databricks Connect!

An excursion into the Databricks Connect Python package

TL,DR: Databricks-Connect will replace your native PySpark package with a specialized version for the Databricks platform, unable to host a local SparkSession. Both packages are mutually exclusive to each other.

Let’s take a look into the package of Databricks Connect itself:

Besides databricks.connect itself, two additional packages can be found: delta and pyspark. When further investigating into the wheel folder databricks_connect-16.1.7.dist-info (see https://packaging.python.org/en/latest/specifications/binary-distribution-format/#binary-distribution-format for more information), we identify that Databricks Connect takes priority over any prior installed delta/pyspark package and replaces them.

Minimal working example

Clone the example and run it yourself in the IDE of your choice: https://github.com/inovex/databricks-pyspark-local-testing-demo

Summary

Databricks Connect will break your ability to create a local Spark server using the PySpark package. However, you can still create remote sessions which are hosted on Databricks or otherwise available to you (such as a locally running Spark server). Using devcontainers, you are able to host both your development environment and a Spark server in a developer-friendly way. And continue to run your tests familiar to you.

The drawback of this approach is a limited set of functionality compared to the Databricks ecosystem. Some features may not work for your individual use case: the Spark Connect server (and thus the remote session) provides a limited subset of the full Databricks ecosystem’s functionality. Features that rely on deep integration with Databricks-specific services – such as special data types or other proprietary utilities – may not be fully available in this standalone environment. Developers must validate that this approach covers all essential features required for their individual use case.

Resources for further reading

Did you like this post?

Your email address will not be published. Required fields are marked *

inoNews

5 good reasons to subscribe to the inovex newsletter:

  • Exclusive insights and tips from our inovexperts
  • Information and updates on IT trend topics and offers
  • Discounts on trainings and event invitations
  • Free whitepapers and infosheets
  • Options for exchange and consulting

To the newsletter registration