In this section, we will explore how to run Apache Spark on Microsoft Azure. Azure provides a robust platform for deploying and managing Spark applications, offering various services and tools to simplify the process. By the end of this module, you will be able to set up and run Spark applications on Azure, leveraging its cloud capabilities for scalable and efficient data processing.

Key Concepts

  1. Azure HDInsight: A fully-managed cloud service that makes it easy to process big data using popular open-source frameworks such as Apache Hadoop, Apache Spark, Apache Hive, and Apache Kafka.
  2. Azure Databricks: An Apache Spark-based analytics platform optimized for the Microsoft Azure cloud services platform.
  3. Azure Synapse Analytics: A limitless analytics service that brings together big data and data warehousing.

Setting Up Spark on Azure HDInsight

Step-by-Step Guide

  1. Create an HDInsight Cluster:

    • Navigate to the Azure portal.
    • Click on "Create a resource" and search for "HDInsight".
    • Click "Create" and fill in the necessary details such as subscription, resource group, cluster name, and region.
    • Choose the cluster type as "Spark".
    • Configure the cluster size and other settings as per your requirements.
    • Review and create the cluster.
  2. Accessing the Cluster:

    • Once the cluster is created, go to the HDInsight cluster dashboard.
    • Use the "Cluster Dashboard" link to access the Ambari management interface.
    • From Ambari, you can manage and monitor your Spark cluster.
  3. Submitting Spark Jobs:

    • Use the Ambari interface or SSH into the cluster to submit Spark jobs.
    • Example command to submit a Spark job:
      spark-submit --class org.apache.spark.examples.SparkPi --master yarn --deploy-mode cluster /usr/hdp/current/spark2-client/examples/jars/spark-examples_2.11-2.3.0.jar 10
      

Setting Up Spark on Azure Databricks

Step-by-Step Guide

  1. Create an Azure Databricks Workspace:

    • Navigate to the Azure portal.
    • Click on "Create a resource" and search for "Azure Databricks".
    • Click "Create" and fill in the necessary details such as subscription, resource group, workspace name, and region.
    • Click "Create" to deploy the workspace.
  2. Create a Databricks Cluster:

    • Go to the Azure Databricks workspace you created.
    • Click on "Clusters" in the left-hand menu.
    • Click "Create Cluster" and configure the cluster settings such as cluster name, cluster mode, and worker nodes.
    • Click "Create Cluster" to start the cluster.
  3. Running Spark Jobs:

    • Once the cluster is running, click on "Workspace" in the left-hand menu.
    • Create a new notebook by clicking on the "Create" button and selecting "Notebook".
    • Choose a language (e.g., Scala, Python) and attach the notebook to your cluster.
    • Write and run Spark code in the notebook. For example:
      from pyspark.sql import SparkSession
      
      spark = SparkSession.builder.appName("example").getOrCreate()
      data = [("Alice", 34), ("Bob", 45), ("Cathy", 29)]
      df = spark.createDataFrame(data, ["Name", "Age"])
      df.show()
      

Setting Up Spark on Azure Synapse Analytics

Step-by-Step Guide

  1. Create an Azure Synapse Workspace:

    • Navigate to the Azure portal.
    • Click on "Create a resource" and search for "Azure Synapse Analytics".
    • Click "Create" and fill in the necessary details such as subscription, resource group, workspace name, and region.
    • Click "Review + create" and then "Create" to deploy the workspace.
  2. Create a Spark Pool:

    • Go to the Azure Synapse workspace you created.
    • Click on "Manage" in the left-hand menu.
    • Under "Apache Spark pools", click "New".
    • Configure the Spark pool settings such as pool name, node size, and number of nodes.
    • Click "Create" to create the Spark pool.
  3. Running Spark Jobs:

    • Go to the "Develop" section in the left-hand menu.
    • Create a new notebook by clicking on the "New" button and selecting "Notebook".
    • Attach the notebook to your Spark pool.
    • Write and run Spark code in the notebook. For example:
      from pyspark.sql import SparkSession
      
      spark = SparkSession.builder.appName("example").getOrCreate()
      data = [("Alice", 34), ("Bob", 45), ("Cathy", 29)]
      df = spark.createDataFrame(data, ["Name", "Age"])
      df.show()
      

Practical Exercise

Exercise: Running a Spark Job on Azure Databricks

  1. Create a Databricks Cluster:

    • Follow the steps outlined above to create an Azure Databricks workspace and cluster.
  2. Create a Notebook:

    • Create a new notebook in your Databricks workspace.
  3. Write and Run Spark Code:

    • In the notebook, write the following Spark code to read a CSV file from Azure Blob Storage and perform a simple transformation:
      from pyspark.sql import SparkSession
      
      spark = SparkSession.builder.appName("AzureExample").getOrCreate()
      
      # Replace with your Azure Blob Storage account details
      storage_account_name = "your_storage_account_name"
      storage_account_access_key = "your_storage_account_access_key"
      container_name = "your_container_name"
      file_path = "your_file_path.csv"
      
      spark.conf.set("fs.azure.account.key." + storage_account_name + ".blob.core.windows.net", storage_account_access_key)
      
      df = spark.read.csv(f"wasbs://{container_name}@{storage_account_name}.blob.core.windows.net/{file_path}", header=True, inferSchema=True)
      df.show()
      
      # Perform a simple transformation
      df_filtered = df.filter(df['Age'] > 30)
      df_filtered.show()
      
  4. Run the Notebook:

    • Attach the notebook to your cluster and run the cells to execute the Spark job.

Solution

The provided code reads a CSV file from Azure Blob Storage, displays its contents, and then filters the rows where the 'Age' column is greater than 30. Ensure you replace the placeholder values with your actual Azure Blob Storage account details.

Summary

In this module, we covered how to run Apache Spark on Azure using different services such as HDInsight, Databricks, and Synapse Analytics. We walked through the steps to set up clusters, create notebooks, and run Spark jobs. By leveraging Azure's cloud capabilities, you can efficiently manage and scale your Spark applications.

© Copyright 2024. All rights reserved