gcp apache spark deployment quickstart

We can deploy a spark job without creating any cluster in GCP and this is a quickstart that we will be doing today with the followings commands:- 



# enabling services

gcloud services enable dataproc.googleapis.com bigquery.googleapis.com

# Creating the bucket

export BUCKET=$(gcloud config get-value project)-spark-demo
gcloud storage buckets create gs://$BUCKET --location=us-central1
bq mk --location=US wordcount_dataset



And this is the code that we will be using 




import sys
from pyspark.sql import SparkSession, functions as F

bucket = sys.argv[1]

spark = SparkSession.builder.appName("shakespeare-wordcount").getOrCreate()
spark.conf.set("temporaryGcsBucket", bucket)   # staging area for the BigQuery write

# 1. READ from BigQuery
words = (spark.read.format("bigquery")
         .option("table", "bigquery-public-data:samples.shakespeare")
         .load()
         .select("word", "word_count"))

# 2. TRANSFORM: lowercase, drop short words, add up counts
counts = (words
          .withColumn("word", F.lower("word"))
          .filter(F.length("word") > 3)
          .groupBy("word")
          .agg(F.sum("word_count").alias("word_count")))

counts.orderBy(F.desc("word_count")).show(10)   # prints the top 10 in the job output

# 3. WRITE to your own BigQuery table
(counts.write.format("bigquery")
    .option("table", "wordcount_dataset.wordcount_output")
    .mode("overwrite")
    .save())


And lets submit this job 





gcloud dataproc batches submit pyspark wordcount.py \
  --region=$REGION \
  --deps-bucket=$BUCKET \
  --properties=dataproc.tier=standard,spark.dynamicAllocation.maxExecutors=1 \
  -- $BUCKET



There are some permission that is required here for the default service account for our project. 


And after sumibtting the job. 



And output from the console.











Comments

Popular posts from this blog

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

android build issue with version compatibility with agp and hilt : An exception occurred applying plugin request [id: 'com.google.dagger.hilt.android', version: '2.52']