快速入門:使用ARM樣本在 Azure HDInsight 中建立Apache Spark 叢集

在本快速入門中,您會使用 Azure Resource Manager 範本 (ARM 範本) 在 Azure HDInsight 中建立 Apache Spark 叢集。 接著,您會建立 Jupyter Notebook 檔案,並用它來對 Apache Hive 數據表執行 Spark SQL 查詢。 Azure HDInsight 是企業受控、全方位、開放原始碼的分析服務。 適用於 HDInsight 的 Apache Spark 架構可使用記憶體內部處理來快速數據分析和叢集運算。 Jupyter Notebook 可讓您與數據互動、將程式代碼與 Markdown 文字結合,以及執行簡單的視覺效果。

如果您同時使用多個叢集,您會想要建立虛擬網路,而如果您使用 Spark 叢集,您也會想要使用 Hive Warehouse 連線 or。 如需詳細資訊,請參閱規劃 Azure HDInsight 的虛擬網路,以及整合 Apache Spark 和 Apache Hive 與 Hive Warehouse 連線 or

Azure Resource Manager 範本是 JavaScript 物件表示法 (JSON) 檔案,可定義專案的基礎結構和組態。 範本使用宣告式語法。 您不需要撰寫程式設計命令順序來建立部署,即可描述預定的部署。

如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。 範本會在 Azure 入口網站中開啟。

Button to deploy the Resource Manager template to Azure.

必要條件

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

檢閱範本

本快速入門中使用的範本是來自 Azure 快速入門範本

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.5.6.12127",
      "templateHash": "4742950082151195489"
    }
  },
  "parameters": {
    "clusterName": {
      "type": "string",
      "metadata": {
        "description": "The name of the HDInsight cluster to create."
      }
    },
    "clusterLoginUserName": {
      "type": "string",
      "maxLength": 20,
      "minLength": 2,
      "metadata": {
        "description": "These credentials can be used to submit jobs to the cluster and to log into cluster dashboards. The username must consist of digits, upper or lowercase letters, and/or the following special characters: (!#$%&'()-^_`{}~)."
      }
    },
    "clusterLoginPassword": {
      "type": "secureString",
      "minLength": 10,
      "metadata": {
        "description": "The password must be at least 10 characters in length and must contain at least one digit, one upper case letter, one lower case letter, and one non-alphanumeric character except (single-quote, double-quote, backslash, right-bracket, full-stop). Also, the password must not contain 3 consecutive characters from the cluster username or SSH username."
      }
    },
    "sshUserName": {
      "type": "string",
      "minLength": 2,
      "metadata": {
        "description": "These credentials can be used to remotely access the cluster. The sshUserName can only consit of digits, upper or lowercase letters, and/or the following special characters (%&'^_`{}~). Also, it cannot be the same as the cluster login username or a reserved word"
      }
    },
    "sshPassword": {
      "type": "secureString",
      "maxLength": 72,
      "minLength": 6,
      "metadata": {
        "description": "SSH password must be 6-72 characters long and must contain at least one digit, one upper case letter, and one lower case letter.  It must not contain any 3 consecutive characters from the cluster login name"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "headNodeVirtualMachineSize": {
      "type": "string",
      "defaultValue": "Standard_E8_v3",
      "allowedValues": [
        "Standard_A4_v2",
        "Standard_A8_v2",
        "Standard_E2_v3",
        "Standard_E4_v3",
        "Standard_E8_v3",
        "Standard_E16_v3",
        "Standard_E20_v3",
        "Standard_E32_v3",
        "Standard_E48_v3"
      ],
      "metadata": {
        "description": "This is the headnode Azure Virtual Machine size, and will affect the cost. If you don't know, just leave the default value."
      }
    },
    "workerNodeVirtualMachineSize": {
      "type": "string",
      "defaultValue": "Standard_E8_v3",
      "allowedValues": [
        "Standard_A4_v2",
        "Standard_A8_v2",
        "Standard_E2_v3",
        "Standard_E4_v3",
        "Standard_E8_v3",
        "Standard_E16_v3",
        "Standard_E20_v3",
        "Standard_E32_v3",
        "Standard_E48_v3"
      ],
      "metadata": {
        "description": "This is the workernode Azure Virtual Machine size, and will affect the cost. If you don't know, just leave the default value."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-08-01",
      "name": "[format('storage{0}', uniqueString(resourceGroup().id))]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2"
    },
    {
      "type": "Microsoft.HDInsight/clusters",
      "apiVersion": "2021-06-01",
      "name": "[parameters('clusterName')]",
      "location": "[parameters('location')]",
      "properties": {
        "clusterVersion": "4.0",
        "osType": "Linux",
        "tier": "Standard",
        "clusterDefinition": {
          "kind": "spark",
          "configurations": {
            "gateway": {
              "restAuthCredential.isEnabled": true,
              "restAuthCredential.username": "[parameters('clusterLoginUserName')]",
              "restAuthCredential.password": "[parameters('clusterLoginPassword')]"
            }
          }
        },
        "storageProfile": {
          "storageaccounts": [
            {
              "name": "[replace(replace(reference(resourceId('Microsoft.Storage/storageAccounts', format('storage{0}', uniqueString(resourceGroup().id)))).primaryEndpoints.blob, 'https://', ''), '/', '')]",
              "isDefault": true,
              "container": "[parameters('clusterName')]",
              "key": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', format('storage{0}', uniqueString(resourceGroup().id))), '2021-08-01').keys[0].value]"
            }
          ]
        },
        "computeProfile": {
          "roles": [
            {
              "name": "headnode",
              "targetInstanceCount": 2,
              "hardwareProfile": {
                "vmSize": "[parameters('headNodeVirtualMachineSize')]"
              },
              "osProfile": {
                "linuxOperatingSystemProfile": {
                  "username": "[parameters('sshUserName')]",
                  "password": "[parameters('sshPassword')]"
                }
              }
            },
            {
              "name": "workernode",
              "targetInstanceCount": 2,
              "hardwareProfile": {
                "vmSize": "[parameters('workerNodeVirtualMachineSize')]"
              },
              "osProfile": {
                "linuxOperatingSystemProfile": {
                  "username": "[parameters('sshUserName')]",
                  "password": "[parameters('sshPassword')]"
                }
              }
            }
          ]
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', format('storage{0}', uniqueString(resourceGroup().id)))]"
      ]
    }
  ],
  "outputs": {
    "storage": {
      "type": "object",
      "value": "[reference(resourceId('Microsoft.Storage/storageAccounts', format('storage{0}', uniqueString(resourceGroup().id))))]"
    },
    "cluster": {
      "type": "object",
      "value": "[reference(resourceId('Microsoft.HDInsight/clusters', parameters('clusterName')))]"
    }
  }
}

範本中定義了兩個 Azure 資源:

部署範本

  1. 選取下方的 [部署至 Azure] 按鈕來登入 Azure,並開啟 ARM 範本。

    Button to deploy the Resource Manager template to Azure.

  2. 輸入或選取下列值:

    屬性 描述
    訂用帳戶 從下拉式清單中,選取用於此叢集的 Azure 訂用帳戶。
    資源群組 從下拉式清單中選取現有資源群組,或選取 [新建]。
    Location 此值會以資源群組所用的位置來自動填入。
    叢集名稱 輸入全域唯一名稱。 針對此範本,請只使用小寫字母和數字。
    叢集登入使用者名稱 提供使用者名稱,預設值為 admin
    叢集登入密碼 提供密碼。 密碼長度至少必須為 10 個字元,且必須包含至少一個數字、一個大寫字母及一個小寫字母、一個非英數字元 (除了字元 ' ` ")。
    SSH 使用者名稱 提供使用者名稱,預設值為 sshuser
    SSH 密碼 請提供密碼。

    Create Spark cluster in HDInsight using Azure Resource Manager template.

  3. 檢閱條款及條件。 然後選取 [我同意上方所述的條款及條件],然後選取 [購買]。 您會收到一則通知,內容指出您的部署正在進行中。 大約需要 20 分鐘的時間來建立叢集。

如果您遇到建立 HDInsight 叢集的問題,可能是您沒有適當的許可權可以這麼做。 如需詳細資訊,請參閱 訪問控制需求

檢閱已部署的資源

叢集建立好之後,您會收到部署成功通知,內有 [移至資源] 連結。 [資源群組] 頁面會列出新的 HDInsight 叢集以及與叢集相關聯的預設儲存體。 每個叢集都有 Azure 儲存體Azure Data Lake 儲存體 Gen1Azure Data Lake Storage Gen2相依性。 也稱為預設儲存體帳戶。 HDInsight 叢集及其預設記憶體帳戶必須共置在相同的 Azure 區域中。 刪除叢集並不會刪除記憶體帳戶相依性。 也稱為預設儲存體帳戶。 HDInsight 叢集及其預設儲存體帳戶必須共置於相同的 Azure 區域中。 刪除叢集並不會刪除儲存體帳戶。

建立 Jupyter 筆記本檔案

Jupyter Notebook 是支援各種程式設計語言的互動式筆記本環境。 您可以使用 Jupyter Notebook 檔案來與您的數據互動、將程式代碼與 Markdown 文字結合,以及執行簡單的視覺效果。

  1. 開啟 Azure 入口網站

  2. 選取 [HDInsight 叢集],然後選取您建立的叢集。

    Open HDInsight cluster in the Azure portal.

  3. 從入口網站,在 [叢集儀錶板] 區段中,選取 [ Jupyter Notebook]。 出現提示時,輸入叢集的叢集登入認證。

    Open Jupyter Notebook to run interactive Spark SQL query.

  4. 選取 [新增>PySpark] 以建立筆記本。

    Create a Jupyter Notebook file to run interactive Spark SQL query.

    系統會建立並開啟名為 Untitled(Untitled.pynb) 的新筆記本。

執行 Apache Spark SQL 語句

SQL (結構化查詢語言 (SQL)) 是查詢和轉換資料的最常用且廣泛使用的語言。 Spark SQL 函式是 Apache Spark 的擴充功能,可使用熟悉的 SQL 語法來處理結構化數據。

  1. 確認核心已就緒。 當您在筆記本中的核心名稱旁邊看到空心圓圈時,核心便已準備就緒。 實心圓表示核心忙碌中。

    Kernel status alt-text=“核心狀態。” border=“true”::

    當您第一次啟動筆記本時,核心會在背景中執行一些工作。 等候核心準備就緒。

  2. 將下列程式代碼貼到空白儲存格中,然後按 SHIFT + ENTER 以執行程式代碼。 命令會列出叢集上的 Hive 資料表:

    %%sql
    SHOW TABLES
    

    當您搭配 HDInsight 叢集使用 Jupyter Notebook 檔案時,您會收到一個預設 spark 會話,可用來使用 Spark SQL 執行 Hive 查詢。 %%sql 告知 Jupyter Notebook 使用預設 spark 會話來執行 Hive 查詢。 查詢會從Hive數據表 (hivesampletable) 擷取前10個數據列,該數據表預設隨附於所有HDInsight叢集。 第一次提交查詢時,Jupyter 會建立筆記本的Spark應用程式。 完成大約需要30秒的時間。 一旦 Spark 應用程式準備就緒,查詢就會在大約一秒內執行,併產生結果。 輸出如下所示:

    Apache Hive query in HDInsight. y in HDInsight“ border=”true“:::

    每次您在 Jupyter 中執行查詢時,網頁瀏覽器視窗標題都會顯示 [忙碌] 狀態以及筆記本標題。 您也會在右上角的 PySpark 文字旁看到一個實心圓。

  3. 執行另一個查詢以查看 中的數據 hivesampletable

    %%sql
    SELECT * FROM hivesampletable LIMIT 10
    

    畫面應該重新整理以顯示查詢輸出。

    Hive query output in HDInsight. Insight“ border=”true“:::

  4. 從筆記本上的 [ 檔案 ] 功能表中,選取 [關閉] 和 [ 停止]。 關閉筆記本會釋放叢集資源,包括 Spark 應用程式。

清除資源

完成此快速入門之後,您可以刪除叢集。 利用 HDInsight,您的資料會儲存在 Azure 儲存體中,以便您在未使用叢集時安全地刪除該叢集。 您也需支付 HDInsight 叢集的費用 (即使未使用該叢集)。 由於叢集費用是儲存體費用的許多倍,所以刪除未使用的叢集符合經濟效益。

在 Azure 入口網站中瀏覽至您的叢集,然後選取 [刪除]

Azure portal delete an HDInsight cluster. sight cluster“ border=”true“:::

您也可以選取資源群組名稱來開啟資源群組頁面,然後選取 [刪除資源群組]。 刪除資源群組時,會同時刪除 HDInsight 叢集及預設儲存體帳戶。

下一步

在本快速入門中,您已瞭解如何在 HDInsight 中建立 Apache Spark 叢集,並執行基本的 Spark SQL 查詢。 前進到下一個教學課程,瞭解如何使用 HDInsight 叢集在範例數據上執行互動式查詢。