使用 PowerShell 搭配執行 MapReduce 工作與 HDInsight 上的 Apache Hadoop

本文件提供使用 Azure PowerShell 在 HDInsight 叢集的 Hadoop 中執行 MapReduce 工作的範例。

必要條件

執行 MapReduce 作業

Azure PowerShell 提供 Cmdlet ,可讓您從遠端在 HDInsight 上執行 MapReduce 工作。 在內部,PowerShell 會對 HDInsight 叢集上執行的 WebHCat (先前稱為 Templeton) 發出 REST 呼叫。

在遠端 HDInsight 叢集中執行 MapReduce 工作時,會使用下列 Cmdlet。

指令程式 描述
Connect-AzAccount 向您的 Azure 訂用帳戶驗證 Azure PowerShell。
New-AzHDInsightMapReduceJobDefinition 使用指定的 MapReduce 資訊來建立新的「作業定義」
Start-AzHDInsightJob 將作業定義傳送給 HDInsight,並啟動作業。 系統會傳回「作業」物件。
Wait-AzHDInsightJob 使用作業物件來檢查作業的狀態。 它會等到工作完成,或等到等候時間超過。
Get-AzHDInsightJobOutput 用來擷取作業的輸出。

下列步驟示範如何使用這些 Cmdlet,在您的 HDInsight 叢集中執行工作。

  1. 使用編輯器,將下列程式碼儲存為 mapreducejob.ps1

    # Login to your Azure subscription
    $context = Get-AzContext
    if ($context -eq $null) 
    {
        Connect-AzAccount
    }
    $context
    
    # Get cluster info
    $clusterName = Read-Host -Prompt "Enter the HDInsight cluster name"
    $creds=Get-Credential -Message "Enter the login for the cluster"
    
    #Get the cluster info so we can get the resource group, storage, etc.
    $clusterInfo = Get-AzHDInsightCluster -ClusterName $clusterName
    $resourceGroup = $clusterInfo.ResourceGroup
    $storageAccountName=$clusterInfo.DefaultStorageAccount.split('.')[0]
    $container=$clusterInfo.DefaultStorageContainer
    #NOTE: This assumes that the storage account is in the same resource
    #      group as the cluster. If it is not, change the
    #      --ResourceGroupName parameter to the group that contains storage.
    $storageAccountKey=(Get-AzStorageAccountKey `
        -Name $storageAccountName `
    -ResourceGroupName $resourceGroup)[0].Value
    
    #Create a storage context
    $context = New-AzStorageContext `
        -StorageAccountName $storageAccountName `
        -StorageAccountKey $storageAccountKey
    
    #Define the MapReduce job
    #NOTE: If using an HDInsight 2.0 cluster, use hadoop-examples.jar instead.
    # -JarFile = the JAR containing the MapReduce application
    # -ClassName = the class of the application
    # -Arguments = The input file, and the output directory
    $wordCountJobDefinition = New-AzHDInsightMapReduceJobDefinition `
        -JarFile "/example/jars/hadoop-mapreduce-examples.jar" `
        -ClassName "wordcount" `
        -Arguments `
            "/example/data/gutenberg/davinci.txt", `
            "/example/data/WordCountOutput"
    
    #Submit the job to the cluster
    Write-Host "Start the MapReduce job..." -ForegroundColor Green
    $wordCountJob = Start-AzHDInsightJob `
        -ClusterName $clusterName `
        -JobDefinition $wordCountJobDefinition `
        -HttpCredential $creds
    
    #Wait for the job to complete
    Write-Host "Wait for the job to complete..." -ForegroundColor Green
    Wait-AzHDInsightJob `
        -ClusterName $clusterName `
        -JobId $wordCountJob.JobId `
        -HttpCredential $creds
    # Download the output
    Get-AzStorageBlobContent `
        -Blob 'example/data/WordCountOutput/part-r-00000' `
        -Container $container `
        -Destination output.txt `
        -Context $context
    # Print the output of the job.
    Get-AzHDInsightJobOutput `
        -Clustername $clusterName `
        -JobId $wordCountJob.JobId `
        -HttpCredential $creds
    
  2. 開啟新的 Azure PowerShell 命令提示字元。 將目錄變更至 mapreducejob.ps1 檔案的位置,然後使用下列命令來執行指令碼:

    .\mapreducejob.ps1
    

    執行指令碼後,系統會提示您輸入 HDInsight 叢集名稱和叢集登入。 系統可能也會提示您驗證 Azure 訂用帳戶。

  3. 在作業完成時,您會收到類似下列文字的輸出:

    Cluster         : CLUSTERNAME
    ExitCode        : 0
    Name            : wordcount
    PercentComplete : map 100% reduce 100%
    Query           :
    State           : Completed
    StatusDirectory : f1ed2028-afe8-402f-a24b-13cc17858097
    SubmissionTime  : 12/5/2014 8:34:09 PM
    JobId           : job_1415949758166_0071
    

    此輸出表示工作已順利完成。

    注意

    如果 ExitCode 的值不是 0,請參閱 疑難排解

    此範例也會將下載的檔案儲存到您執行指令碼所在目錄中的 output.txt 檔案。

檢視輸出

若要查看作業所產生的單字和計數,請使用文字編輯器開啟 output.txt 檔案。

注意

MapReduce 工作的輸出檔是固定不變的。 因此,如果您重新執行此範例,則需要變更輸出檔的名稱。

疑難排解

如果作業完成時未傳回任何資訊,請檢視作業的錯誤。 若要檢視這項工作的錯誤資訊,請新增下列命令至 mapreducejob.ps1 檔案的結尾。 然後儲存檔案,並重新執行指令碼。

# Print the output of the WordCount job.
Write-Host "Display the standard output ..." -ForegroundColor Green
Get-AzHDInsightJobOutput `
        -Clustername $clusterName `
        -JobId $wordCountJob.JobId `
        -HttpCredential $creds `
        -DisplayOutputType StandardError

這個 Cmdlet 會傳回作業執行時寫入到 STDERR 的資訊。

下一步

如您所見,Azure PowerShell 提供簡單的方法,在 HDInsight 叢集上執行 MapReduce 工作、監視工作狀態,以及擷取輸出。 如需您可以在 HDInsight 上使用 Hadoop 之其他方式的詳細資訊: