Jalankan pekerjaan MapReduce dengan Apache Hadoop di Microsoft Azure HDInsight menggunakan PowerShell

Dokumen ini memberikan contoh penggunaan Azure PowerShell untuk menjalankan pekerjaan MapReduce di kluster Hadoop di Microsoft Azure HDInsight.

Prasyarat

Menjalankan pekerjaan MapReduce

Azure PowerShell menyediakan cmdlet yang memungkinkan Anda menjalankan pekerjaan MapReduce dari jarak jauh di Microsoft Azure HDInsight. Secara internal, PowerShell melakukan panggilan REST ke WebHCat (sebelumnya disebut Templeton) yang berjalan di kluster Microsoft Azure HDInsight.

Cmdlet berikut digunakan saat menjalankan pekerjaan MapReduce di kluster Microsoft Azure HDInsight jarak jauh.

Cmdlet Deskripsi
Connect-AzAccount Mengautentikasi Azure PowerShell ke langganan Azure Anda.
New-AzHDInsightMapReduceJobDefinition Membuat definisi pekerjaan baru dengan menggunakan informasi MapReduce yang ditentukan.
Start-AzHDInsightJob Mengirim definisi pekerjaan ke Microsoft Azure HDInsight dan memulai pekerjaan. Objek pekerjaan dikembalikan.
Wait-AzHDInsightJob Menggunakan objek pekerjaan untuk memeriksa status pekerjaan. Itu menunggu sampai pekerjaan selesai atau waktu tunggu terlampaui.
Get-AzHDInsightJobOutput Digunakan untuk mengambil output pekerjaan.

Langkah-langkah berikut menunjukkan cara menggunakan cmdlet ini untuk menjalankan pekerjaan di kluster Microsoft Azure HDInsight Anda.

  1. Dengan menggunakan editor, simpan kode berikut sebagai 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. Buka perintah Azure PowerShell baru. Ubah direktori ke lokasi file mapreducejob.ps1, lalu gunakan perintah berikut untuk menjalankan skrip:

    .\mapreducejob.ps1
    

    Saat Anda menjalankan skrip, Anda akan diminta untuk nama kluster Microsoft Azure HDInsight dan masuk ke kluster. Anda mungkin juga akan diminta untuk mengautentikasi langganan Azure Anda.

  3. Saat pekerjaannya selesai, Anda akan menerima output yang mirip dengan teks berikut ini:

    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
    

    Output ini menunjukkan bahwa pekerjaannya sudah berhasil diselesaikan.

    Catatan

    Jika ExitCode adalah nilai selain 0, lihat Pemecahan Masalah.

    Contoh ini juga menyimpan file yang diunduh ke file output.txt di direktori tempat Anda menjalankan skrip.

Melihat output

Untuk melihat kata dan hitungan yang dihasilkan oleh pekerjaan, buka file output.txt di editor teks.

Catatan

File output dari tugas MapReduce tidak dapat diubah. Jadi, jika Anda menjalankan kembali sampel ini, Anda perlu mengubah nama file output.

Pemecahan Masalah

Jika tidak ada informasi yang dikembalikan saat pekerjaan selesai, lihat kesalahan untuk pekerjaan tersebut. Untuk melihat informasi kesalahan untuk pekerjaan ini, tambahkan perintah berikut di akhir file mapreducejob.ps1. Kemudian simpan file tersebut dan jalankan kembali skripnya.

# 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 ini mengembalikan informasi yang ditulis ke STDERR saat pekerjaan berjalan.

Langkah berikutnya

Seperti yang Anda lihat, Microsoft Azure PowerShell menyediakan cara mudah untuk menjalankan pekerjaan MapReduce pada kluster Microsoft Azure HDInsight, memantau status pekerjaan, dan mengambil hasilnya. Untuk informasi tentang cara lain Anda dapat bekerja dengan Hadoop di Microsoft Azure HDInsight: