Skip to main content

Dataproc client library for Spark Connect

Project description

Dataproc Spark Connect Client

A wrapper of the Apache Spark Connect client with additional functionalities that allow applications to communicate with a remote Dataproc Spark Session using the Spark Connect protocol without requiring additional steps.

Install

pip install dataproc_spark_connect

Uninstall

pip uninstall dataproc_spark_connect

Setup

This client requires permissions to manage Dataproc Sessions and Session Templates.

If you are running the client outside of Google Cloud, you need to provide authentication credentials. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to your Application Credentials file.

You can specify the project and region either via environment variables or directly in your code using the builder API:

  • Environment variables: GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_REGION
  • Builder API: .projectId() and .location() methods (recommended)

Usage

  1. Install the latest version of Dataproc Spark Connect:

    pip install -U dataproc-spark-connect
    
  2. Add the required imports into your PySpark application or notebook and start a Spark session using the fluent API:

    from google.cloud.dataproc_spark_connect import DataprocSparkSession
    spark = DataprocSparkSession.builder.getOrCreate()
    
  3. You can configure Spark properties using the .config() method:

    from google.cloud.dataproc_spark_connect import DataprocSparkSession
    spark = DataprocSparkSession.builder.config('spark.executor.memory', '4g').config('spark.executor.cores', '2').getOrCreate()
    
  4. For advanced configuration, you can use the Session class to customize settings like subnetwork or other environment configurations:

    from google.cloud.dataproc_spark_connect import DataprocSparkSession
    from google.cloud.dataproc_v1 import Session
    session_config = Session()
    session_config.environment_config.execution_config.subnetwork_uri = '<subnet>'
    session_config.runtime_config.version = '3.0'
    spark = DataprocSparkSession.builder.projectId('my-project').location('us-central1').dataprocSessionConfig(session_config).getOrCreate()
    

Reusing Named Sessions Across Notebooks

Named sessions allow you to share a single Spark session across multiple notebooks, improving efficiency by avoiding repeated session startup times and reducing costs.

To create or connect to a named session:

  1. Create a session with a custom ID in your first notebook:

    from google.cloud.dataproc_spark_connect import DataprocSparkSession
    session_id = 'my-ml-pipeline-session'
    spark = DataprocSparkSession.builder.dataprocSessionId(session_id).getOrCreate()
    df = spark.createDataFrame([(1, 'data')], ['id', 'value'])
    df.show()
    
  2. Reuse the same session in another notebook by specifying the same session ID:

    from google.cloud.dataproc_spark_connect import DataprocSparkSession
    session_id = 'my-ml-pipeline-session'
    spark = DataprocSparkSession.builder.dataprocSessionId(session_id).getOrCreate()
    df = spark.createDataFrame([(2, 'more-data')], ['id', 'value'])
    df.show()
    
  3. Session IDs must be 4-63 characters long, start with a lowercase letter, contain only lowercase letters, numbers, and hyphens, and not end with a hyphen.

  4. Named sessions persist until explicitly terminated or reach their configured TTL.

  5. A session with a given ID that is in a TERMINATED state cannot be reused. It must be deleted before a new session with the same ID can be created.

Using Spark SQL Magic Commands (Jupyter Notebooks)

The package supports the sparksql-magic library for executing Spark SQL queries directly in Jupyter notebooks.

Installation: To use magic commands, install the required dependencies manually:

pip install dataproc-spark-connect
pip install IPython sparksql-magic
  1. Load the magic extension:

    %load_ext sparksql_magic
    
  2. Configure default settings (optional):

    %config SparkSql.limit=20
    
  3. Execute SQL queries:

    %%sparksql
    SELECT * FROM your_table
    
  4. Advanced usage with options:

    # Cache results and create a view
    %%sparksql --cache --view result_view df
    SELECT * FROM your_table WHERE condition = true
    

Available options:

  • --cache / -c: Cache the DataFrame
  • --eager / -e: Cache with eager loading
  • --view VIEW / -v VIEW: Create a temporary view
  • --limit N / -l N: Override default row display limit
  • variable_name: Store result in a variable

See sparksql-magic for more examples.

Note: Magic commands are optional. If you only need basic DataprocSparkSession functionality without Jupyter magic support, install only the base package:

pip install dataproc-spark-connect

Developing

For development instructions see guide.

Contributing

We'd love to accept your patches and contributions to this project. There are just a few small guidelines you need to follow.

Contributor License Agreement

Contributions to this project must be accompanied by a Contributor License Agreement. You (or your employer) retain the copyright to your contribution; this simply gives us permission to use and redistribute your contributions as part of the project. Head over to https://cla.developers.google.com to see your current agreements on file or to sign a new one.

You generally only need to submit a CLA once, so if you've already submitted one (even if it was for a different project), you probably don't need to do it again.

Code reviews

All submissions, including submissions by project members, require review. We use GitHub pull requests for this purpose. Consult GitHub Help for more information on using pull requests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dataproc_spark_connect-1.0.2.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dataproc_spark_connect-1.0.2-py2.py3-none-any.whl (30.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file dataproc_spark_connect-1.0.2.tar.gz.

File metadata

  • Download URL: dataproc_spark_connect-1.0.2.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for dataproc_spark_connect-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e553bcc9faccd7d462ccacacfe12379e7c444bb092d6337b3356de17634764bf
MD5 43162a48d2e4b7f670c53ceaa78b9462
BLAKE2b-256 de9a2c52694aa23171b0f3db861edf795147df9b55d3bd9e91f04f4151b3bd94

See more details on using hashes here.

Provenance

The following attestation bundles were made for dataproc_spark_connect-1.0.2.tar.gz:

Publisher: google-cloud-sdk-py@oss-exit-gate-prod.iam.gserviceaccount.com

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.
  • Statement: Publication detail:
    • Token Issuer: https://accounts.google.com
    • Service Account: google-cloud-sdk-py@oss-exit-gate-prod.iam.gserviceaccount.com

File details

Details for the file dataproc_spark_connect-1.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dataproc_spark_connect-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 662551c5bcec7de9b5d8639749e67a06387281f94e36e02951f7cd859005aaab
MD5 248288067e08d04f2f1171e9832f1d72
BLAKE2b-256 7d26a5c7679fc4ec4919fbc8b198bfc868c49a4fd5be0ca9d95c31e7651c9b08

See more details on using hashes here.

Provenance

The following attestation bundles were made for dataproc_spark_connect-1.0.2-py2.py3-none-any.whl:

Publisher: google-cloud-sdk-py@oss-exit-gate-prod.iam.gserviceaccount.com

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.
  • Statement: Publication detail:
    • Token Issuer: https://accounts.google.com
    • Service Account: google-cloud-sdk-py@oss-exit-gate-prod.iam.gserviceaccount.com

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page