sparkmagic

repository·master·Indexed 23 days ago

https://github.com/jupyter-incubator/sparkmagic

A set of tools for interactively working with remote Spark clusters from Jupyter notebooks via REST servers such as Livy, Lighter, or Ilum. It supports multi-language Spark execution (Python, Scala, R), automatic visualization of SQL queries, and seamless integration between local Python environments and remote Spark sessions.

Tokens
7K
Snippets
25
Records
38
Agent score
68%

What's inside sparkmagic

  1. Overview of Sparkmagic

    master

    Sparkmagic is a set of tools for interactively working with remote Spark clusters in Jupyter notebooks. It interacts with remote Spark clusters through a REST server.

    Supported server implementations:

    • Livy: For running interactive sessions on Yarn.
    • Lighter: For running interactive sessions on Yarn or Kubernetes (supports PySpark sessions only).
    • Ilum: For running interactive sessions on Yarn or Kubernetes.

    Key features include:

    • Running Spark code in multiple languages via Livy.
    • Automatic creation of SparkContext (sc) and HiveContext (sqlContext).
    • Using %%sql magic for SparkSQL queries.
    • Automatic visualization of SQL queries in PySpark, Spark, and SparkR kernels.
    • Accessing Spark application info and logs via %%info magic.
    • Capturing SQL query output as Pandas dataframes.
    • Sending local files or dataframes to a remote cluster.
  2. License terms for sparkmagic

    master

    The sparkmagic project is licensed under the Modified BSD License (also known as New, Revised, or 3-Clause BSD).

    Key conditions for redistribution:

    • Source code: Must retain the original copyright notice, the list of conditions, and the disclaimer.
    • Binary form: Must reproduce the copyright notice, the list of conditions, and the disclaimer in the documentation and/or other materials provided with the distribution.
    • Endorsement: The name of the Jupyter Development Team or its contributors may not be used to endorse or promote derivative products without specific prior written permission.

    Disclaimer: The software is provided "as is" without warranty of any kind.

  3. How Sparkmagic architecture works

    master

    Sparkmagic uses Livy (a REST server for Spark) to execute user code remotely.

    Workflow:

    1. User code is sent to the remote Spark driver via Livy.
    2. Sparkmagic automatically collects the output as plain text or a JSON document.
    3. The library parses this data to display results as formatted text or Pandas dataframes on the client side.

    Key Concepts:

    • Remote Execution: No Spark components are required on the Jupyter server itself.
    • Multi-language: Supports Python, Python3, Scala, and R kernels.
    • Multiple Endpoints: A single notebook can interact with different remote clusters using different endpoints.
    • Data Serialization: Because code runs on a remote driver, all structured data must be serialized to JSON and parsed by Sparkmagic. For client-side data manipulation, you must use Python in %%local mode.
  4. Install Sparkmagic for Jupyter Notebook 7.x / JupyterLab 3.x

    master

    Follow these steps to install Sparkmagic in modern Jupyter environments:

    1. Install the library:
      pip install sparkmagic
    2. Ensure ipywidgets is installed:
      pip install ipywidgets
    3. (Optional) Install wrapper kernels. First, find the installation path using pip show sparkmagic, cd to that directory, and run:
      jupyter-kernelspec install sparkmagic/kernels/sparkkernel
      jupyter-kernelspec install sparkmagic/kernels/pysparkkernel
      jupyter-kernelspec install sparkmagic/kernels/sparkrkernel
    4. (Optional) Configure settings in ~/.sparkmagic/config.json.
    5. (Optional) Enable the server extension to allow programmatic cluster changes:
      jupyter server extension enable --py sparkmagic
    pip install sparkmagic
    pip install ipywidgets
    # After cd-ing to sparkmagic installation path:
    jupyter-kernelspec install sparkmagic/kernels/sparkkernel
    jupyter-kernelspec install sparkmagic/kernels/pysparkkernel
    jupyter-kernelspec install sparkmagic/kernels/sparkrkernel
    # Enable extension:
    jupyter server extension enable --py sparkmagic
  5. Implement and Register a Custom Authenticator

    master

    To implement a custom authentication mechanism, create a subclass of Authenticator and override the Authenticator.__call__(request) method to attach HTTP Authentication to the request object.

    If your authenticator requires additional attributes (like username and password), you must also override __hash__, __eq__, update_with_widget_values, and get_widgets.

    Registering the Authenticator

    After installing your custom authenticator package, register it in ~/.sparkmagic/config.json under the authenticators key:

    {
        "authenticators": {
            "Custom_Auth": "customauthenticator.customauthenticator.CustomAuthenticator"
        }
    }

    Alternatively, you can modify sparkmagic/utils/configuration.py to return your class in the authenticators() method.

    {
        "authenticators": {
            "Kerberos": "sparkmagic.auth.kerberos.Kerberos",
            "None": "sparkmagic.auth.customauth.Authenticator",
            "Basic_Access": "sparkmagic.auth.basic.Basic",
            "Custom_Auth": "customauthenticator.customauthenticator.CustomAuthenticator"
        }
    }
  6. Install Sparkmagic for development

    master

    To set up a local development environment for Sparkmagic, follow these steps:

    1. Clone the repository:
      git clone https://github.com/jupyter-incubator/sparkmagic
    2. Install the local packages in editable mode:
      pip install -e hdijupyterutils 
      pip install -e autovizwidget
      pip install -e sparkmagic

    Alternative: Using Poetry If you prefer Poetry, use the following commands:

    poetry install
    # If numpy or pandas installation fails, run:
    # poetry run pip install numpy pandas
    # then re-run:
    # poetry install

    Running Tests Run unit tests using pytest:

    # Standard install
    pytest
    
    # Poetry install
    poetry run pytest
    git clone https://github.com/jupyter-incubator/sparkmagic
    pip install -e hdijupyterutils 
    pip install -e autovizwidget
    pip install -e sparkmagic
  7. Install Sparkmagic for Jupyter Notebook 5.2 or earlier / JupyterLab 1 or 2

    master

    Follow these steps for legacy Jupyter environments:

    1. Install the library:
      pip install sparkmagic
    2. Enable widgetsnbextension:
      jupyter nbextension enable --py --sys-prefix widgetsnbextension
    3. (JupyterLab 1 or 2 only) Install the labextension:
      jupyter labextension install "@jupyter-widgets/jupyterlab-manager"
    4. (Optional) Install wrapper kernels. Find the installation path using pip show sparkmagic, cd to that directory, and run:
      jupyter-kernelspec install sparkmagic/kernels/sparkkernel
      jupyter-kernelspec install sparkmagic/kernels/pysparkkernel
      jupyter-kernelspec install sparkmagic/kernels/sparkrkernel
    5. (Optional) Configure settings in ~/.sparkmagic/config.json.
    6. (Optional) Enable the server extension:
      jupyter serverextension enable --py sparkmagic
    pip install sparkmagic
    jupyter nbextension enable --py --sys-prefix widgetsnbextension
    # For JupyterLab 1/2:
    jupyter labextension install "@jupyter-widgets/jupyterlab-manager"
    # Install kernels (after cd to sparkmagic path):
    jupyter-kernelspec install sparkmagic/kernels/sparkkernel
    jupyter-kernelspec install sparkmagic/kernels/pysparkkernel
    jupyter-kernelspec install sparkmagic/kernels/sparkrkernel
    # Enable extension:
    jupyter serverextension enable --py sparkmagic
  8. Spin up a Sparkmagic stack using Docker Compose

    master

    For testing or development, you can use the provided docker-compose.yml to launch a full stack including a Jupyter notebook and a Livy server backed by a local-mode Spark instance.

    1. Ensure Docker and Docker Compose are installed.
    2. Build and start the services:
      docker compose build
      docker compose up
    3. Access Jupyter at http://localhost:8888.
    4. Configure a sparkmagic endpoint at http://spark:8998 within the notebook to launch Scala or Python sessions.

    To shut down, use Ctrl-C and optionally run docker compose down to remove containers.

    docker compose build
    docker compose up
  9. Develop Sparkmagic locally in Docker

    master

    If you are developing Sparkmagic and want to test changes in the Docker container without publishing to PyPI, set the dev_mode build argument to true in docker-compose.yml and rebuild.

    This installs your local versions of autovizwidget, hdijupyterutils, and sparkmagic using the editable flag (-e). This allows you to make edits directly to the libraries within the Jupyterlab docker service for real-time debugging.

    Note: You must re-run docker compose build before spinning up services for changes to take effect.

  10. Configure Custom HTTP Session Adapters

    master

    You can use custom HTTP transport adapters to control request behavior (e.g., adding custom headers, implementing mTLS, or retries) for specific domains.

    Add the http_session_config section to your ~/.sparkmagic/config.json to map a URL prefix to a custom adapter class.

    {
      "http_session_config": {
        "adapters": [
          {
            "prefix": "http://",
            "adapter": "customadapter.customadapter.CustomaAapter"
          }
        ]
      }
    }