مشاركة عبر


قم بتشغيل وظائف MapReduce مع Apache Hadoop على HDInsight باستخدام PowerShell

يوفر هذا المستند مثالًا على استخدام Azure PowerShell لتشغيل مهمة MapReduce في Hadoop على مجموعة HDInsight.

المتطلبات الأساسية

قم بتشغيل مهمة MapReduce

يوفر Azure PowerShell أوامر cmdlets التي تتيح لك تشغيل مهام MapReduce عن بُعد على HDInsight. داخليًا، يجري PowerShell مكالمات REST إلى WebHCat (المعروف سابقًا باسم Templeton) الذي يعمل على مجموعة HDInsight.

أوامر cmdlets التالية المستخدمة عند تشغيل مهام MapReduce في مجموعة HDInsight بعيدة.

Cmdlet ‏‏الوصف
Connect-AzAccount يصادق Azure PowerShell على اشتراكك في Azure.
جديد- AzHDInsightMapReduceJobDefinition يقوم بإنشاء تعريف وظيفة جديد باستخدام معلومات MapReduce المحددة.
بدء- AzHDInsightJob يرسل تعريف الوظيفة إلى HDInsight ويبدأ المهمة. تم إرجاع كائن وظيفة .
انتظر- AzHDInsightJob يستخدم كائن الوظيفة للتحقق من حالة الوظيفة. ينتظر حتى تكتمل المهمة أو يتم تجاوز وقت الانتظار.
Get-AzHDInsightJobOutput تستخدم لاسترداد مخرجات الوظيفة.

توضح الخطوات التالية كيفية استخدام أوامر cmdlets هذه لتشغيل مهمة في مجموعة 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 طريقة سهلة لتشغيل مهام MapReduce على مجموعة HDInsight ومراقبة حالة المهمة واسترداد المخرجات. للحصول على معلومات حول الطرق الأخرى التي يمكنك من خلالها العمل مع Hadoop على HDInsight: