Skip to content

Create submit_pyspark_job_to_driver_node_group_cluster.py #13513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Supriya-Koppa
Copy link

Sample code to submit a PySpark job to a Dataproc driver node group cluster.

Description

Fixes #

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

Sample code to submit a PySpark job to a Dataproc driver node group cluster.
@Supriya-Koppa Supriya-Koppa requested review from a team as code owners July 22, 2025 07:40
Copy link

snippet-bot bot commented Jul 22, 2025

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Jul 22, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @Supriya-Koppa, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request adds a new, self-contained Python sample to the repository. Its primary purpose is to provide a clear and executable example for users looking to programmatically submit PySpark jobs to Dataproc clusters, specifically those configured with driver node groups, and to illustrate how to manage the job's resource allocation and retrieve its output.

Highlights

  • New Sample Code: Introduced a new Python sample, submit_pyspark_job_to_driver_node_group_cluster.py, demonstrating how to submit a PySpark job to a Google Cloud Dataproc cluster that utilizes a driver node group.
  • Dataproc Job Submission: The sample showcases the use of the google.cloud.dataproc_v1 client library to configure and submit a PySpark job, including specifying DriverSchedulingConfig for memory and vcores.
  • Output Retrieval: The code includes logic to retrieve and print the job's output from the Google Cloud Storage bucket where Dataproc stores job results.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a new Python sample for submitting a PySpark job to a Dataproc driver node group cluster. The code is a good starting point. My review includes suggestions for improving the script's usability by implementing command-line argument parsing, enhancing robustness with proper error handling, and increasing clarity and consistency through documentation and style adjustments. Addressing these points will make the sample more robust and user-friendly.

Comment on lines 77 to 79
my_project_id = "your_cluster" # <-- REPLACE THIS
my_region = "us-central1" # <-- REPLACE THIS
my_cluster_name = "your-node-group-cluster" # <-- REPLACE THIS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The script uses hardcoded placeholder values, which requires users to manually edit the file before running it. To make the sample easier to use and consistent with other samples in the repository, it should parse command-line arguments.

Additionally, the placeholder value for my_project_id is "your_cluster", which is confusing. It should be something like "your-project-id".

Please consider replacing the current implementation with one that uses sys.argv for argument parsing.

if __name__ == "__main__":
    import sys

    if len(sys.argv) != 4:
        sys.exit(
            f"Usage: python {sys.argv[0]} <project_id> <region> <cluster_name>"
        )

    project_id = sys.argv[1]
    region = sys.argv[2]
    cluster_name = sys.argv[3]
    submit_job(project_id, region, cluster_name)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected the placeholder value for my_project_id.

I intend the main function to be as is.

Comment on lines +22 to +24
# python submit_pyspark_job_to_driver_node_group_cluster.py \
# --project_id <PROJECT_ID> --region <REGION> \
# --cluster_name <CLUSTER_NAME>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Usage example suggests named arguments (--project_id), but other samples in this directory (e.g., submit_job.py) use positional arguments. To maintain consistency across samples, consider updating this to reflect positional arguments.

Suggested change
# python submit_pyspark_job_to_driver_node_group_cluster.py \
# --project_id <PROJECT_ID> --region <REGION> \
# --cluster_name <CLUSTER_NAME>
# python submit_pyspark_job_to_driver_node_group_cluster.py \
# <PROJECT_ID> <REGION> <CLUSTER_NAME>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


import re


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per PEP 8, there should be only one blank line separating logical import groups (in this case, standard library re from third-party google.cloud). Please remove the extra blank line here to improve code style consistency.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 46 to 47
# Create the job config. 'main_jar_file_uri' can also be a
# Google Cloud Storage URL.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This comment mentions 'main_jar_file_uri', which is relevant for Spark jobs (Java/Scala). Since this is a PySpark job, the comment should refer to 'main_python_file_uri' to avoid confusion.

    # Create the job config. 'main_python_file_uri' can also be a

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Supriya-Koppa
Copy link
Author

hi @glasnt could you please review this PR?

it is similar to #13423

Thanks!

Copy link
Contributor

@glasnt glasnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your first submission to the python-docs-samples repo! 🎉

There are some changes required to this file, including:

  • moving it to the correct sample location for this product: dataproc/snippets/
  • including testing (you should be able to reference tests from similar samples in the dataproc folder as a starting point)

@@ -0,0 +1,91 @@
#!/usr/bin/env python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs to be moved to the dataproc/snippets folder.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please clarify that the existing "submit_spark_job_to_driver_node_group_cluster.py" sample is different to this "submit_pyspark_job_to_driver_node_group_cluster.py` sample.

Comment on lines +52 to +53
# Create the job config. 'main_python_file_uri' can also be a
# Google Cloud Storage URL.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the value already is a Storage URL. This comment should be updated.

job = {
"placement": {"cluster_name": cluster_name},
"pyspark_job": {
"main_python_file_uri": "gs://dataproc-examples/pyspark/hello-world/hello-world.py"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this object come from? Is there open source code for this object?

Comment on lines +87 to +91
my_project_id = "your_project_id" # <-- REPLACE THIS
my_region = "your_region" # <-- REPLACE THIS
my_cluster_name = "your_node_group_cluster" # <-- REPLACE THIS

submit_job(my_project_id, my_region, my_cluster_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this main method to copy the functionality from the spark job, which will remove the "REPLACE THIS" values. This can also help if you use command-line testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants