
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
In this lab, you will learn how to migrate Apache Spark code to Dataproc. You will follow a sequence of steps, progressively moving more of the job components over to Google Cloud services:
In this lab, you will learn how to:
You are migrating an existing Spark workload to Dataproc and then progressively modifying the Spark code to make use of Google Cloud-native features and services.
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00
), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
Before you begin your work on Google Cloud, you need to ensure that your project has the correct permissions within Identity and Access Management (IAM).
In the Google Cloud console, on the Navigation menu (), select IAM & Admin > IAM.
Confirm that the default compute Service Account {project-number}-compute@developer.gserviceaccount.com
is present and has the editor
role assigned. The account prefix is the project number, which you can find on Navigation menu > Cloud Overview > Dashboard.
editor
role, follow the steps below to assign the required role.729328892908
).{project-number}
with your project number.From the Navigation menu (), go to IAM & admin > IAM.
Edit the permissions for your Compute Engine default service account by locating the service agent under the IAM list and selecting the pencil icon. The service account will have the Domain @developer.gserviceaccount.com
.
Click add another role in the dialog and then select Cloud Storage > Storage Admin role.
Click Save.
You will create a new Dataproc cluster and then run an imported Jupyter notebook that uses the cluster's default local Hadoop Distributed File system (HDFS) to store source data and then process that data just as you would on any Hadoop cluster using Spark.
This demonstrates how many existing analytics workloads such as Jupyter notebooks containing Spark code require no changes when they are migrated to a Dataproc environment.
On the Google Cloud console title bar, type Dataproc in the Search field, then click Dataproc in the search results.
On the Dataproc page, click Pin next to Dataproc.
Click Create Cluster and click Create for Cluster on Compute Engine.
Enter sparktobq for Name.
For Region, select
In the Versioning section, click Change.
Select 2.1 (Debian 11, Hadoop 3.3, Spark 3.3).
In the Components section, select Enable component gateway for Component gateway.
For Optional components select Jupyter Notebook.
Click Configure nodes (optional). For Manager node, select Series: E2, Machine Type e2-standard-2 and set Primary disk size to 100.
For Worker nodes, select Series: E2, Machine Type e2-standard-2 and set Primary disk size to 100.
Click Create on the Dataproc creation wizard list.
The cluster should start in a few minutes. You can proceed to the next step without waiting for the Dataproc Cluster to fully deploy.
In the Cloud Shell, you clone the Git repository for the lab and copy the required notebook files to the Google Cloud Storage bucket used by Dataproc as the home directory for Jupyter notebooks.
As soon as the cluster has fully started up you can connect to the Web interfaces. Click the Refresh button to check, as it may be deployed fully by the time you reach this stage.
On the Dataproc Clusters page, wait for the cluster to finish starting and then click the name of your cluster to open the Cluster details page.
Click Web Interfaces.
Click the Jupyter link to open a new Jupyter tab in your browser. This opens the Jupyter home page.
Click the GCS folder link. Here you can see the contents of the /notebooks/jupyter directory in Google Cloud Storage that now includes the sample Jupyter notebooks used in this lab.
Click the 01_spark.ipynb notebook to open it.
Click Cell and then Run All to run all of the cells in the notebook.
Page back up to the top of the notebook and follow as the notebook completion runs each cell and outputs the results below them.
You can now step down through the cells and examine the code as it is processed so that you can see what the notebook is doing. In particular pay attention to where the data is saved and processed from.
The first code cell fetches the source data file, which is an extract from the KDD Cup competition from the Knowledge, Discovery, and Data (KDD) conference in 1999. The data relates to computer intrusion detection events.
In the second code cell, the source data is copied to the default (local) Hadoop file system.
In the third code cell, the command lists contents of the default directory in the cluster's HDFS file system.
The data are gzipped CSV files. In Spark, these can be read drectly using the textFile method and then parsed by splitting each row on commas.
The Python Spark code starts in cell In[4]
. In this cell Spark SQL is initialized and Spark is used to read in the source data as text and then returns the first five rows.
In cell In [5]
, each row is split using ,
as a delimiter and parsed using a prepared inline schema in the code:
In [6]
, a Spark SQL context is created. And a Spark dataframe using that context is created using the parsed input data from the previous stage. Row data can be selected and displayed using the dataframe's .show()
method to output a view summarizing a count of selected fields.The .show()
method produces an output table similar to this.
Output:
SparkSQL can also be used to query the parsed data stored in the Dataframe. In cell In [7]
a temporary table (connections
) is registered that is then referenced inside the subsequent SparkSQL SQL query statement.
You will see output similar to this truncated example when the query has finished.
Output:
And you can now also display this data visually using bar charts.
The last cell, In [8]
, uses the %matplotlib inline
Jupyter magic function to redirect matplotlib to render a graphic figure inline in the notebook instead of just dumping the data into a variable. This cell displays a bar chart using the attack_stats
query from the previous step.
The first part of the output should look like the following chart once all cells in the notebook have run successfully. You can scroll down in your notebook to see the complete output chart.
Taking this original 'Lift & Shift' sample notebook you will now create a copy that decouples the storage requirements for the job from the compute requirements. In this case all you have to do is replace the Hadoop file system calls with Google Storage calls by replacing hdfs://
storage references with gs://
references in the code and adjusting folder names as necessary.
You start by using the Cloud Shell to place a copy of the source data in a new Cloud Storage bucket.
Make sure that the last command completes and the file has been copied to your new storage bucket.
Switch back to the 01_spark Jupyter Notebook tab in your browser.
Click File and then select Make a Copy.
When the copy opens, click the 01_spark-Copy1 title and rename it to De-couple-storage
.
Open the Jupyter tab for 01_spark
.
Click File and then Save and checkpoint to save the notebook.
Click File and then Close and Halt to shutdown the notebook.
If you are prompted to confirm that you want to close the notebook, click Leave or OK.
You no longer need the cells that download and copy the data onto the cluster's internal HDFS file system so you will remove those first.
To delete a cell, you click in the cell to select it and then click the cut selected cells icon (the scissors) on the notebook toolbar.
In [1]
, In [2]
, and In [3]
) so that the notebook now starts with the section Reading in Data.You will now change the code in the first cell (still called In[4]
unless you have re-run the notebook) that defines the data file source location and reads in the source data. The cell currently contains the following code:
In [4]
with the following code. The only change here is create a variable to store a Cloud Storage bucket name and then to point the data_file
to the bucket we used to store the source data on Cloud Storage.When you have replaced the code the first cell will look similar to the following, with your lab project ID as the bucket name:
You will see exactly the same output as you did when the file was loaded and run from internal cluster storage. Moving the source data files to Cloud Storage only requires that you repoint your storage source reference from hdfs://
to gs://
.
You now create a standalone Python file, that can be deployed as a Dataproc Job, that will perform the same functions as this notebook. To do this you add magic commands to the Python cells in a copy of this notebook to write the cell contents out to a file. You will also add an input parameter handler to set the storage bucket location when the Python script is called to make the code more portable.
In the Jupyter Notebook menu click File and select Make a Copy.
When the copy opens, click the De-couple-storage-Copy1 and rename it to PySpark-analysis-file
.
Open the Jupyter tab for Decouple-storage.
Click File and then Save and checkpoint to save the notebook.
Click File and then Close and Halt to shutdown the notebook.
If you are prompted to confirm that you want to close the notebook, click Leave or OK.
Switch back to the PySpark-analysis-file Jupyter Notebook tab in your browser, if necessary.
Click the first cell at the top of the notebook.
Click Insert and select Insert Cell Above.
Paste the following library import and parameter handling code into this new first code cell:
The %%writefile spark_analysis.py
Jupyter magic command creates a new output file to contain your standalone python script. You will add a variation of this to the remaining cells to append the contents of each cell to the standalone script file.
This code also imports the matplotlib
module and explicitly sets the default plotting backend via matplotlib.use('agg')
so that the plotting code runs outside of a Jupyter notebook.
%%writefile -a spark_analysis.py
at the start of each Python code cell. These are the five cells labelled In [x].For example, the next cell should now look as follows.
Repeat this step, inserting %%writefile -a spark_analysis.py at the start of each code cell until you reach the end.
In the last cell, where the Pandas bar chart is plotted, remove the %matplotlib inline
magic command.
Make sure you have selected the last code cell in the notebook; then, in the menu bar, click Insert and select Insert Cell Below.
Paste the following code into the new cell.
You now test that the PySpark code runs successfully as a file by calling the local copy from inside the notebook, passing in a parameter to identify the storage bucket you created earlier that stores the input data for this job. The same bucket will be used to store the report data files produced by the script.
PySpark-analysis-file
notebook, add a new cell at the end of the notebook and paste in the following:This code assumes that you have followed the earlier instructions and created a Cloud Storage bucket using your lab Project ID as the Storage Bucket name. If you used a different name modify this code to set the BUCKET
variable to the name you used.
This lists the script output files that have been saved to your Cloud Storage bucket.
If the notebook successfuly creates and runs the Python file you should see output smilar to the following for the last two cells. This indicates that the script has run to completion saving the output to the Cloud Storage bucket you created earlier in the lab.
Press CTRL+X then Y to exit and save.
Make the script executable:
In the Cloud console tab, navigate to the Dataproc > Clusters page if it is not already open.
Click Jobs.
Click the name of the job that is listed. You can monitor progress here as well as from the Cloud shell. Wait for the job to complete successfully.
Navigate to your storage bucket and note that the output report, /sparktobq/report.png
has an updated time-stamp indicating that the stand-alone job has completed successfully.
The storage bucket used by this job for input and output data storage is the bucket that is used just the Project ID as the name.
Navigate back to the Dataproc > Clusters page.
Select the sparktobq cluster and click Delete. You don't need it any more.
Click Confirm.
Close both Jupyter tabs in your browser.
You can now convert the code to run as an AI Platform notebook using BigQuery, rather than SparkSQL. You will import data into BigQuery, as well as convert and run your queries against BigQuery rather than using SparkSQL.
In the Navigation menu on the Google Cloud console page, navigate to Vertex AI > Workbench.
Click Enable Notebooks API.
Click User-managed notebooks.
Click + Create New.
Enter sparktobq
for the Instance name.
For Region, select
Click Environment and then select Python 3 (with Intel MKL).
Click Create.
When the instance has started and the Open JupyterLab link has become active, click Open JupyterLab to open the JupyterLab notebook console.
In the Launcher pane, for Notebook, click Python 3.
Paste in the following code into the first cell to install the Google Cloud BigQuery Python module required to use inline BigQuery using the %%bigquery
cell magic keyword:
Press the Run icon to install the BigQuery module.
Click Kernel > Restart Kernel to import the new module into the kernel for this notebook.
Replace the contents of the first cell with the following code to import the source data from your storage bucket:
This loads and parses the rows of data in the CSV file and saves the data into the kdd_cup_raw
table.
This shows that the data has been successfully loaded but we have no column headers. You can add column headers by creating a second table, kdd_cup
, and using Create of Replace Table
.
This now returns the data with appropriate headers provided by the schema for the newly creaded kdd_cup
table.
You now convert the original Spark SQL data frame query that showed connection counts by protocol to a BigQuery SQL query.
You now recreate the main report query that is used to produce the attack stats chart. Take the time to note that the BigQuery version of the query is identical in syntax to the earlier SparkSQL version, the only difference is the reference to the source table.
For reference, this is what the original SparkSQL query definition looked like.
Wait for the report to finish and confirm that the report has been updated in Google Storage using the BigQuery table data.
In the Navigation menu, navigate to Cloud Storage > Buckets and open this in a new tab.
Browse to the sparktobq
folder. You should see that the timestamp on the report.png
file has been updated and now contains the output report generated using BigQuery.
In the Navigation menu, on the Google Cloud console page, navigate to Vertex AI > Workbench.
Click Enable Notebooks API.
Click + Create New.
Click Environment and then select Python 3 (with Intel MKL).
Enter sparktobq
for the Notebook name.
Click Create.
When the instance has started and the Open JupyterLab link has become active, click Open JupyterLab to open the JupyterLab notebook console.
You will clone the source code repository for the lab into the JupyterLab instance.
In the JupyterLab File menu, click New and then select Terminal.
In the JupyterLab terminal, enter the following command:
When the repository has been cloned, navigate to training-data-analyst > quests > sparktobq in the directory pane to the left of the terminal.
Click 05_functions_dw to open the notebook for the final part of the lab.
You will now use a pre-configured Jupyter notebook to create, deploy, and test a Cloud Run function that automatically loads and processes a data file from Cloud Storage using BigQuery (and the techniques covered in the earlier steps in this lab) to produce a report automatically when a datafile is uploaded to Cloud Storage.
This configures the notebook for your Qwiklabs project.
Page down to the cell below the title Test that the function endpoint works.
Replace the bucket name in the line BUCKET='cloud-training-demos-ml'
with the Project ID for your lab.
In the next cell Deploy the cloud function, replace <region>
with
On the menu bar, click Run and then click Run All Cells.
The initial deployment of the Cloud Run function will take a couple of minutes to complete.
On the Google Cloud console title bar, type Cloud Run functions in the Search field, then click Cloud Run functions in the search results.
Click the name of the function to open it. You can monitor the progress of the initial setup of the function from here.
Switch back to the Vertex AI Jupyter notebook and monitor progress until it completes.
The final cell will initially report an exception if it is run before the Cloud Run function has completed processing. Re-run this cell when the Cloud Run function has completed to see the output files.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one