你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 PowerShell 运行 Apache Hive 查询

本文档举例说明如何使用 Azure PowerShell 在 Apache Hadoop on HDInsight 群集中运行 Apache Hive 查询。

注意

本文档未详细描述示例中使用的 HiveQL 语句的作用。 有关此示例中使用的 HiveQL 的信息,请参阅将 Apache Hive 与 HDInsight 上的 Apache Hadoop 配合使用

必备条件

运行 Hive 查询

Azure PowerShell 提供 cmdlet,可在 HDInsight 上远程运行 Hive 查询。 cmdlet 在内部对 HDInsight 群集上的 WebHCat 进行 REST 调用。

在远程 HDInsight 群集上运行 Hive 查询时,将使用以下 Cmdlet:

  • Connect-AzAccount:在 Azure 订阅中进行 Azure PowerShell 身份验证。
  • New-AzHDInsightHiveJobDefinition:使用指定的 HiveQL 语句创建“作业定义” 。
  • Start-AzHDInsightJob:将作业定义发送到 HDInsight 并启动作业。 将返回作业对象 。
  • Wait-AzHDInsightJob:使用作业对象来检查作业的状态。 它等到作业完成或超出等待时间。
  • Get-AzHDInsightJobOutput:用于检索作业的输出。
  • Invoke-AzHDInsightHiveJob:用于运行 HiveQL 语句。 此 cmdlet 将阻止查询完成,然后返回结果。
  • Use-AzHDInsightCluster:设置要用于 Invoke-AzHDInsightHiveJob 命令的当前群集。

以下步骤演示了如何使用这些 Cmdlet 在 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
    

    注意

    对于较长的 HiveQL 查询,可以使用 Azure PowerShell Here-Strings cmdlet 或 HiveQL 脚本文件。 以下代码段显示了如何使用 Invoke-Hive cmdlet 来运行 HiveQL 脚本文件。 HiveQL 脚本文件必须上传到 wasb://。

    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

作业处理期间,此 cmdlet 返回写入到 STDERR 中的信息。

总结

如你所见,Azure PowerShell 提供了简单的方法让你在 HDInsight 群集上运行 Hive 查询,监视作业状态,以及检索输出。

后续步骤

有关 HDInsight 中的 Hive 的一般信息:

有关 HDInsight 上的 Hadoop 的其他使用方法的信息: