PowerShell を使用して Apache Hive クエリを実行する

このドキュメントでは、Azure PowerShell を使用して HDInsight クラスター上の Apache Hadoop で Apache Hive クエリを実行する例を示します。

Note

このドキュメントには、例で使用される HiveQL ステートメントで何が実行されるかに関する詳細は含まれていません。 この例で使用される HiveQL については、HDInsight での Apache Hive と Apache Hadoop の使用に関するページを参照してください。

前提条件

Hive クエリを実行する

Azure PowerShell では、HDInsight で Hive クエリをリモートに実行できる コマンドレット が提供されます。 内部的には、コマンドレットは HDInsight クラスター上の WebHCat への REST 呼び出しを行います。

リモート HDInsight クラスターで Hive クエリを実行するときに次のコマンドレットを使用します。

  • Connect-AzAccount: Azure サブスクリプションに対して Azure PowerShell を認証します。
  • New-AzHDInsightHiveJobDefinition: 指定された HiveQL ステートメントを使用して、"ジョブ定義" を作成します。
  • Start-AzHDInsightJob: ジョブ定義を HDInsight に送信し、ジョブを開始します。 "ジョブ" オブジェクトが返されます。
  • Wait-AzHDInsightJob: ジョブ オブジェクトを使用して、ジョブの状態を確認します。 ジョブの完了を待機するか、待機時間が上限に達します。
  • Get-AzHDInsightJobOutput: ジョブの出力を取得する場合に使用します。
  • Invoke-AzHDInsightHiveJob: HiveQL ステートメントを実行する場合に使用します。 このコマンドレットはクエリの完了をブロックし、その結果を返します。
  • Use-AzHDInsightCluster: Invoke-AzHDInsightHiveJob コマンドに現在のクラスターを使用するように設定します。

これらのコマンドレットを使用して、HDInsight クラスターでジョブを実行するための手順を以下に示します。

  1. エディターを使用して、次のコードを hivejob.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"
    
    #HiveQL
    #Note: set hive.execution.engine=tez; is not required for
    #      Linux-based HDInsight
    $queryString = "set hive.execution.engine=tez;" +
                "DROP TABLE log4jLogs;" +
                "CREATE EXTERNAL TABLE log4jLogs(t1 string, t2 string, t3 string, t4 string, t5 string, t6 string, t7 string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' STORED AS TEXTFILE LOCATION 'wasbs:///example/data/';" +
                "SELECT * FROM log4jLogs WHERE t4 = '[ERROR]';"
    
    #Create an HDInsight Hive job definition
    $hiveJobDefinition = New-AzHDInsightHiveJobDefinition -Query $queryString 
    
    #Submit the job to the cluster
    Write-Host "Start the Hive job..." -ForegroundColor Green
    
    $hiveJob = Start-AzHDInsightJob -ClusterName $clusterName -JobDefinition $hiveJobDefinition -ClusterCredential $creds
    
    #Wait for the Hive job to complete
    Write-Host "Wait for the job to complete..." -ForegroundColor Green
    Wait-AzHDInsightJob -ClusterName $clusterName -JobId $hiveJob.JobId -ClusterCredential $creds
    
    # Print the output
    Write-Host "Display the standard output..." -ForegroundColor Green
    Get-AzHDInsightJobOutput `
        -Clustername $clusterName `
        -JobId $hiveJob.JobId `
        -HttpCredential $creds
    
  2. Azure PowerShell コマンド プロンプトを開きます。 ディレクトリを hivejob.ps1 ファイルの場所に変更し、次のコマンドを使用してスクリプトを実行します。

    .\hivejob.ps1
    

    スクリプトの実行時に、クラスター名と HTTPS またはクラスター管理者アカウントの資格情報を入力するように求められます。 Azure サブスクリプションへのサインインを求められる場合もあります。

  3. ジョブが完了すると、次のテキストのような情報が返されます。

    Display the standard output...
    2012-02-03      18:35:34        SampleClass0    [ERROR] incorrect       id
    2012-02-03      18:55:54        SampleClass1    [ERROR] incorrect       id
    2012-02-03      19:25:27        SampleClass4    [ERROR] incorrect       id
    
  4. 前述のように、Invoke-Hive を使用してクエリを実行し、応答を待機できます。 次のスクリプトを使用して、Invoke-Hive の動作を確認します。

    # 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"
    
    # Set the cluster to use
    Use-AzHDInsightCluster -ClusterName $clusterName -HttpCredential $creds
    
    $queryString = "set hive.execution.engine=tez;" +
                "DROP TABLE log4jLogs;" +
                "CREATE EXTERNAL TABLE log4jLogs(t1 string, t2 string, t3 string, t4 string, t5 string, t6 string, t7 string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' STORED AS TEXTFILE LOCATION '/example/data/';" +
                "SELECT * FROM log4jLogs WHERE t4 = '[ERROR]';"
    Invoke-AzHDInsightHiveJob `
        -StatusFolder "statusout" `
        -Query $queryString
    

    出力は次のテキストのようになります。

    2012-02-03    18:35:34    SampleClass0    [ERROR]    incorrect    id
    2012-02-03    18:55:54    SampleClass1    [ERROR]    incorrect    id
    2012-02-03    19:25:27    SampleClass4    [ERROR]    incorrect    id
    

    Note

    より長い HiveQL クエリの場合は、Azure PowerShell の Here-Strings コマンドレットや HiveQL スクリプト ファイルを使用できます。 次のスニペットでは、Invoke-Hive コマンドレットを使用して HiveQL スクリプト ファイルを実行する方法を示します。 HiveQL スクリプト ファイルは、wasbs:// にアップロードする必要があります。

    Invoke-AzHDInsightHiveJob -File "wasbs://<ContainerName>@<StorageAccountName>/<Path>/query.hql"

    Here-Strings の詳細については、「HERE-STRINGS」を参照してください。

トラブルシューティング

ジョブが完了しても情報が返されない場合は、エラー ログを調べてください。 このジョブに関するエラーを表示するには、以下を hivejob.ps1 ファイルの末尾に追加して保存し、再実行します。

# Print the output of the Hive job.
Get-AzHDInsightJobOutput `
        -Clustername $clusterName `
        -JobId $job.JobId `
        -HttpCredential $creds `
        -DisplayOutputType StandardError

このコマンドレットは、ジョブ処理中に STDERR に書き込まれた情報を返します。

まとめ

このように、Azure PowerShell を使用すると、HDInsight クラスターで簡単に Hive クエリを実行し、ジョブ ステータスを監視し、出力を取得できます。

次のステップ

HDInsight での Hive に関する全般的な情報

HDInsight での Hadoop のその他の使用方法に関する情報