Share via


PowerShell kullanarak HDInsight üzerinde Apache Hadoop ile MapReduce işlerini çalıştırma

Bu belge, HDInsight kümesindeki hadoop'ta MapReduce işi çalıştırmak için Azure PowerShell kullanma örneği sağlar.

Önkoşullar

MapReduce işi çalıştırma

Azure PowerShell, HDInsight üzerinde MapReduce işlerini uzaktan çalıştırmanıza olanak sağlayan cmdlet'ler sağlar. PowerShell dahili olarak HDInsight kümesinde çalışan WebHCat'e (eski adı Templeton) REST çağrıları yapar.

Aşağıdaki cmdlet'ler, MapReduce işlerini uzak bir HDInsight kümesinde çalıştırırken kullanılır.

Cmdlet Açıklama
Connect-AzAccount Azure aboneliğinizde Azure PowerShell kimliğini doğrular.
New-AzHDInsightMapReduceJobDefinition Belirtilen MapReduce bilgilerini kullanarak yeni bir iş tanımı oluşturur.
Start-AzHDInsightJob İş tanımını HDInsight'a gönderir ve işi başlatır. bir nesnesi döndürülür.
Wait-AzHDInsightJob İşin durumunu denetlemek için iş nesnesini kullanır. İş tamamlanana veya bekleme süresi aşılana kadar bekler.
Get-AzHDInsightJobOutput İşin çıkışını almak için kullanılır.

Aşağıdaki adımlar, HDInsight kümenizde bir iş çalıştırmak için bu cmdlet'lerin nasıl kullanılacağını gösterir.

  1. Düzenleyici kullanarak aşağıdaki kodu mapreducejob.ps1olarak kaydedin.

    # 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. Yeni bir Azure PowerShell komut istemi açın. Dizinleri mapreducejob.ps1 dosyasının konumuna değiştirin ve betiği çalıştırmak için aşağıdaki komutu kullanın:

    .\mapreducejob.ps1
    

    Betiği çalıştırdığınızda HDInsight kümesinin adı ve küme oturum açma bilgileri istenir. Azure aboneliğinizde kimlik doğrulaması yapmanız da istenebilir.

  3. İş tamamlandığında, aşağıdaki metne benzer bir çıkış alırsınız:

    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
    

    Bu çıkış, işin başarıyla tamamlandığını gösterir.

    Not

    ExitCode 0 dışında bir değerse bkz. Sorun giderme.

    Bu örnek ayrıca indirilen dosyaları betiği çalıştırdığınız dizindeki bir output.txt dosyasına depolar.

Çıkışı görüntüle

İş tarafından oluşturulan sözcükleri ve sayıları görmek için output.txt dosyasını bir metin düzenleyicisinde açın.

Not

MapReduce işinin çıkış dosyaları sabittir. Bu nedenle bu örneği yeniden çalıştırırsanız çıkış dosyasının adını değiştirmeniz gerekir.

Sorun giderme

İş tamamlandığında hiçbir bilgi döndürülmezse, işin hatalarını görüntüleyin. Bu işin hata bilgilerini görüntülemek için mapreducejob.ps1 dosyasının sonuna aşağıdaki komutu ekleyin. Ardından dosyayı kaydedin ve betiği yeniden çalıştırın.

# 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

Bu cmdlet, iş çalıştırılırken STDERR'ye yazılan bilgileri döndürür.

Sonraki adımlar

Gördüğünüz gibi Azure PowerShell BIR HDInsight kümesinde MapReduce işlerini çalıştırmak, iş durumunu izlemek ve çıkışı almak için kolay bir yol sağlar. HDInsight üzerinde Hadoop ile çalışmanın diğer yolları hakkında bilgi için: