> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Hands on with Dataproc

> Hands-on demo showing how to run a PySpark job on Google Cloud Dataproc to aggregate CSV orders from Cloud Storage and write results back to Cloud Storage

Welcome — in this hands-on demo you'll learn how to run a simple PySpark job on Google Cloud Dataproc that reads data from Cloud Storage, aggregates it, and writes results back to Cloud Storage. Dataproc is a fully managed, scalable service for running Apache Spark, Apache Hadoop, and related open-source data-engineering tools.

What you'll do

* Create a Cloud Storage bucket and upload input data plus a PySpark script.
* Create a Dataproc cluster (single-node or multi-node).
* Submit a PySpark job to the cluster that reads from Cloud Storage, aggregates totals per customer, and writes results back to Cloud Storage.
* Inspect the job using the Dataproc Console and Spark History Server, then verify output in Cloud Storage.

Prerequisites

* A GCP project with billing enabled.
* Permissions to create Storage buckets and Dataproc clusters (roles like Storage Admin and Dataproc Editor are helpful).
* Cloud SDK (`gcloud`) installed if you plan to submit jobs from the CLI.

## Create a Cloud Storage bucket

Open the GCP Console, search for “Storage”, and create a new bucket. Choose a globally unique name and a region (this demo uses `us-central1`). Configure the storage class and other options as needed, then create the bucket.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/gcp-create-bucket-us-central1-storage-price.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=adecbe78d4094de0c36ce1536424feae" alt="A screenshot of the Google Cloud Console &#x22;Create a bucket&#x22; page showing the selected location (us-central1 — Iowa), storage-class choices (Standard, Nearline, Coldline, Archive) and an estimated price ($0.020 per GB‑month). The UI also shows options for Autoclass or setting a default storage class." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/gcp-create-bucket-us-central1-storage-price.jpg" />
</Frame>

## Prepare the input data

Create a small CSV file named `orders.csv` with order data. Example contents:

```csv theme={null}
order_id,customer_id,amount
101,1,500.00
102,2,150.50
103,1,300.00
104,3,1200.00
105,2,50.00
```

Upload `orders.csv` to the bucket you created (e.g., `gs://<your-bucket>/orders.csv`).

## PySpark job (process\_orders.py)

Create a PySpark script `process_orders.py`. The script below reads a CSV from Cloud Storage, aggregates total spend per customer, and writes the result back to Cloud Storage as CSV.

```python theme={null}
import sys
from pyspark.sql import SparkSession
from pyspark.sql.functions import sum as _sum

if len(sys.argv) != 3:
    print("Usage: process_orders.py <input_gcs_path> <output_gcs_path>")
    sys.exit(-1)

input_path = sys.argv[1]
output_path = sys.argv[2]

spark = SparkSession.builder.appName("OrderAggregation").getOrCreate()

# Read CSV with header and schema inference
df = spark.read.option("header", "true").option("inferSchema", "true").csv(input_path)

# Aggregate: total spend per customer
result_df = df.groupBy("customer_id").agg(_sum("amount").alias("total_spend"))

# Write result to GCS (CSV, overwrite mode)
result_df.write.mode("overwrite").csv(output_path)

print("Job completed successfully.")
spark.stop()
```

Notes:

* The script expects two arguments: the input GCS path (CSV) and the output GCS folder path where Spark will write part files.
* It uses `SparkSession` to read the CSV, group by `customer_id`, sum `amount`, and write CSV output.

## Upload files to Cloud Storage

Upload both `orders.csv` and `process_orders.py` to your bucket. If you uploaded the wrong file name, delete and re-upload the corrected files.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/gcp-storage-buckets-dataproc-filter.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=22f2717f6c7196cb2bdb420b315336aa" alt="A screenshot of the Google Cloud Console Storage &#x22;Buckets&#x22; page showing several &#x22;dataproc&#x22; buckets with a filter dropdown open. Columns for location, default storage class, last modified date, and public access are visible." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/gcp-storage-buckets-dataproc-filter.jpg" />
</Frame>

Helpful file layout (example)

| File           | Example GCS path                     | Purpose                                        |
| -------------- | ------------------------------------ | ---------------------------------------------- |
| Input data     | `gs://your-bucket/orders.csv`        | Raw CSV input for the Spark job                |
| PySpark script | `gs://your-bucket/process_orders.py` | Main job entrypoint                            |
| Job output     | `gs://your-bucket/output/`           | Spark writes `part-*` CSV files and `_SUCCESS` |

## Create a Dataproc cluster

Open Dataproc in the GCP Console (search for “Dataproc”), go to Clusters, and click Create cluster. For this demo you can choose:

* Standard cluster (1 master + workers) for distributed workloads.
* Single-node cluster (master-only) to save cost for small tests.

Select additional components (Hive, Jupyter, etc.) if needed and click Create. Cluster provisioning usually takes a few minutes.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/google-cloud-dataproc-create-cluster-gateway.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=05535519be2ae7ad35a5f1529f6c3fef" alt="A Google Cloud Console screenshot of the Dataproc &#x22;Create a Dataproc cluster on Compute Engine&#x22; setup page with the &#x22;Set up cluster&#x22; step selected. The Components pane shows &#x22;Enable component gateway&#x22; checked and a list of optional components (Jupyter, Zeppelin, Trino, etc.), with a blue &#x22;Create&#x22; button on the left." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/google-cloud-dataproc-create-cluster-gateway.jpg" />
</Frame>

Monitor cluster creation; when the cluster status becomes Running, open the cluster details.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/google-cloud-dataproc-demo-cluster-error.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=ab9587191d585036a162a453ca1c9158" alt="A screenshot of the Google Cloud Console Dataproc &#x22;Clusters&#x22; page. It shows one demo-cluster listed as Running in us-central1 with an error banner saying &#x22;Sorry, the server was not able to fulfill your request.&#x22;" width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/google-cloud-dataproc-demo-cluster-error.jpg" />
</Frame>

## Submit the PySpark job from the Console

From the Dataproc cluster details page, click Submit job. Set the job type to PySpark, provide a job name (for example `example-spark-job`), and set the main Python file path to your uploaded script (for example `gs://your-bucket/process_orders.py`). Provide the two required arguments, each on its own line:

Example arguments (each on a separate line):

```text theme={null}
gs://your-bucket/orders.csv
gs://your-bucket/output/
```

Then submit the job. The job will appear in the Jobs list; click it to view logs and status.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/google-cloud-console-dataproc-demo-cluster-pyspark.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=9eee6df1ab62748ef23fca675cc092d9" alt="A screenshot of the Google Cloud Console showing Dataproc cluster details on the left and a &#x22;Submit a job&#x22; form on the right prefilled for a PySpark job (job ID and main Python file path visible). The cluster is named &#x22;demo-cluster&#x22; and the form includes fields for additional Python files, JARs, and other job options." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/google-cloud-console-dataproc-demo-cluster-pyspark.jpg" />
</Frame>

While the job runs you can stream logs from the job details page to troubleshoot issues.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/google-cloud-dataproc-example-spark-job-running-insights-gemini.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=45be5e2b019a30c030ae187119c872cb" alt="A screenshot of the Google Cloud Console showing a Dataproc job details page for &#x22;example-spark-job&#x22; with status &#x22;Running,&#x22; an &#x22;Insights by Gemini&#x22; panel, and an Output area at the bottom." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/google-cloud-dataproc-example-spark-job-running-insights-gemini.jpg" />
</Frame>

## Inspect the job via Spark History Server and Console

From the cluster details, open Web Interfaces and launch the Spark History Server to inspect completed applications, stages, and executors. If the job has not finished you may initially see “No completed applications found.” After completion the History Server lists the application and stages for deeper debugging and performance analysis.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/gcp-dataproc-demo-cluster-web-interfaces.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=2e41c4f947303a892cd04e44b84dafbb" alt="A screenshot of the Google Cloud Console showing Dataproc cluster details for a cluster named &#x22;demo-cluster&#x22; with status &#x22;Running.&#x22; The Web Interfaces tab is open, listing SSH tunnel info and component gateway links like YARN ResourceManager, Spark History Server, and HDFS NameNode." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/gcp-dataproc-demo-cluster-web-interfaces.jpg" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/spark-history-server-no-completed-apps.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=b352100ca431bfdd685a04dc28232895" alt="A screenshot of an Apache Spark History Server web page showing the event log directory, last updated timestamp and client time zone. The page displays a prominent &#x22;No completed applications found!&#x22; message." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/spark-history-server-no-completed-apps.jpg" />
</Frame>

When the job completes successfully the Console shows a green Succeeded status. You can inspect logs to confirm processing details and check the output folder for result files.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/dataproc-spark-example-job-succeeded-insights.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=e31b1097c1c00438af66a13df6954687" alt="A Google Cloud Console Dataproc job details page showing a Spark job named &#x22;example-spark-job&#x22; with its Job UUID and a green &#x22;Succeeded&#x22; status. The Summary tab displays an &#x22;Insights by Gemini&#x22; preview and an Output panel with a note that Spark jobs take ~60 seconds to initialize." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/dataproc-spark-example-job-succeeded-insights.jpg" />
</Frame>

## Note: alternatives to console submission

<Callout icon="lightbulb" color="#1CB2FE">
  You can also submit Dataproc jobs using the `gcloud` CLI, Dataproc REST or client libraries (Python, Java), or orchestrate them via Airflow operators for production workflows. See the Dataproc docs for examples and best practices: [https://cloud.google.com/dataproc/docs/reference](https://cloud.google.com/dataproc/docs/reference)
</Callout>

Cost and cleanup warning

<Callout icon="warning" color="#FF6B6B">
  Dataproc clusters incur compute and networking costs while running. For demos, delete or stop clusters when not in use to avoid unexpected charges. Consider using single-node clusters or autoscaling for cost savings.
</Callout>

## Verify the output in Cloud Storage

After the job succeeds, refresh your Cloud Storage bucket. The output folder contains Spark’s CSV part files (for example `part-00000-*.csv`) and a `_SUCCESS` file indicating completed write.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/cOLw078gFDjw8fZP/images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/gcs-bucket-dataproc-demo-orders.jpg?fit=max&auto=format&n=cOLw078gFDjw8fZP&q=85&s=4ebec082dbb3ed693c4d381011f82b14" alt="A Google Cloud Console screenshot showing the details of a Storage bucket named &#x22;dataproc-demo-kodekloud-gcp-training&#x22; with location us-central1 and Standard storage class. The objects list shows files like orders.csv, an output/ folder, and process_orders.py." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/Data-Processing/Demo-Hands-on-with-Dataproc/gcs-bucket-dataproc-demo-orders.jpg" />
</Frame>

## Typical production pipeline considerations

In production you’ll commonly separate storage layers (raw, processed, analytics). A typical flow:

* Raw data ingested into a raw-data bucket.
* ETL/processing Spark jobs write to a processed bucket.
* Final aggregated results loaded into BigQuery or an analytics store for reporting.

Common production patterns include:

| Area           | Recommendation                                                            |
| -------------- | ------------------------------------------------------------------------- |
| Orchestration  | Use Airflow or Cloud Composer to schedule and retry jobs                  |
| Monitoring     | Send Dataproc/Cloud Logging logs to a central monitoring/alerting system  |
| Storage layout | Use separate buckets or prefixes for raw, processed, and analytics layers |
| Permissions    | Use least-privilege IAM roles for job/service accounts                    |

Links and references

* Dataproc: [https://cloud.google.com/dataproc](https://cloud.google.com/dataproc)
* Cloud Storage: [https://cloud.google.com/storage](https://cloud.google.com/storage)
* Apache Spark: [https://spark.apache.org/](https://spark.apache.org/)
* Spark History Server: [https://spark.apache.org/docs/latest/monitoring.html#spark-history-server](https://spark.apache.org/docs/latest/monitoring.html#spark-history-server)
* BigQuery: [https://cloud.google.com/bigquery](https://cloud.google.com/bigquery)

## Closing

This walkthrough showed how to create a bucket, prepare data and a PySpark job, spin up a Dataproc cluster, submit a job from the Console, inspect it, and verify results in Cloud Storage. Thanks for following along — try extending the script to write Parquet output or to load results into BigQuery for analytics.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/google-cloud-professional-data-engineer-certification/module/0883bfdc-7d2f-4371-910d-b996380ce4ac/lesson/5beb9b6a-81ea-49c6-92fd-30603cbb543c" />
</CardGroup>
