【问题标题】:Create Hive Table on Multiple CSV blobs of Azure Blob Storage在 Azure Blob 存储的多个 CSV Blob 上创建 Hive 表
【发布时间】:2014-01-06 03:46:04
【问题描述】:

我有一个 Azure Blob 存储。在一个名为 DATA 的容器中,我以下列方式拥有 CSV blob -

现在我已经使用 HDInsight 创建了一个 Hadoop 集群。

作为下一部分,我想创建 Hive 表以进行查询。在这里我有一些具体的问题。

1) 如何在单个查询中将所有 BLOBS 加载到 Hive 表?

对于单个 BLOB,我可以使用以下查询。但是如何在单个查询中对 MULTIPLE Blob 执行此操作?

# Use the external table option. 
$queryString = "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 'wasb://$containerName@$storageAccountName.blob.core.windows.net/example/data/';" +
                "SELECT t4 AS sev, COUNT(*) AS cnt FROM log4jLogs WHERE t4 = '[ERROR]' GROUP BY t4;"

2) LOAD DATA 和 EXTERNAL TABLE 在创建 Hive Table 时的主要区别是什么?

任何输入都会有所帮助。

###################### UPDATE1 #################### #

我遵循了以下建议,但无法使其适用于 ONE BLOB

我的 BLOB 是 CSV。我通过powershell从本地上传到blob存储。此 Blob 存储和容器获得了 HDInsight 的默认示例。 Blob 数据如下所示。

  • 1,Rami,Vemula,29
  • 2,杰克,阿斯顿,33

我的 Hive 查询 -

# Provide Windows Azure subscription name, and the Azure Storage account and container that is used for the default HDInsight file system.
$subscriptionName = "Rami"
$storageAccountName = "storagename"
$containerName = "containername"


# Provide HDInsight cluster name Where you want to run the Hive job
$clusterName = "clustername"


# Use the external table option. 
$queryString = "DROP TABLE mylogss;" +
                "CREATE EXTERNAL TABLE mylogss(t1 string, t2 string, t3 string, t4 string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION 'wasb://$containerName@$storageAccountName.blob.core.windows.net/blobrami/';" +
                "SELECT COUNT(*) AS cnt FROM mylogss;"


# Create a Hive job definition 
$hiveJobDefinition = New-AzureHDInsightHiveJobDefinition -Query $queryString


# Submit the job to the cluster 
Select-AzureSubscription $subscriptionName
$hiveJob = Start-AzureHDInsightJob -Cluster $clusterName -JobDefinition $hiveJobDefinition



# Wait for the Hive job to complete
Wait-AzureHDInsightJob -Job $hiveJob -WaitTimeoutInSeconds 3600

结果 -

所以最后我无法获得任何输出。它以代码 1 退出。我不确定我做错了什么。

【问题讨论】:

    标签: csv azure hadoop


    【解决方案1】:

    外部表是 hive 仅管理元数据的表(架构,...)。 您在其中加载数据的常规 Hive 表具有其元数据和由 HIVE 管理的数据。

    如果您删除外部表,您不会丢失数据。

    对于 HDInsight,我通常使用外部表,因为我可以在集群关闭时继续在 blob 存储 (wasb) 中添加数据(因此我不为此付费)。当我针对该数据重新启动集群时,我只需要运行创建外部表的配置单元脚本就可以通过配置单元访问它们。没有数据加载。

    这是一个示例脚本:

    set hive.exec.dynamic.partition=true;
    set hive.exec.dynamic.partition.mode=nonstrict;
    
    create external table IF NOT EXISTS raw_tweets ( json_response string ) partitioned by (dt string) stored as textfile;
    alter table raw_tweets add if not exists partition(dt='2013-03-06') location 'wasb://mycontainer@mystorageaccount.blob.core.windows.net/data/socialvilles/2013-3-6';
    alter table raw_tweets add if not exists partition(dt='2013-03-07') location 'wasb://mycontainer@mystorageaccount.blob.core.windows.net/data/socialvilles/2013-3-7';
    alter table raw_tweets add if not exists partition(dt='2013-03-08') location 'wasb://mycontainer@mystorageaccount.blob.core.windows.net/data/socialvilles/2013-3-8';
    alter table raw_tweets add if not exists partition(dt='2013-03-09') location 'wasb://mycontainer@mystorageaccount.blob.core.windows.net/data/socialvilles/2013-3-9';
    alter table raw_tweets add if not exists partition(dt='2013-03-10') location 'wasb://mycontainer@mystorageaccount.blob.core.windows.net/data/socialvilles/2013-3-10';
    alter table raw_tweets add if not exists partition(dt='2013-03-11') location 'wasb://mycontainer@mystorageaccount.blob.core.windows.net/data/socialvilles/2013-3-11';
    alter table raw_tweets add if not exists partition(dt='2013-03-12') location 'wasb://mycontainer@mystorageaccount.blob.core.windows.net/data/socialvilles/2013-3-12';
    
    create external table IF NOT EXISTS tweets2 (
        id string,
        lang string,
        json_response string)
    partitioned by (dt string)
    row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile 
    location '/wasbwork/tweets2';
    
    insert overwrite table tweets2
    partition (dt)
    select 
        get_json_object(json_response, '$.id_str') as id,
        get_json_object(json_response, '$.user.lang') as lang,
        json_response, 
        dt
        FROM raw_tweets
        where (length(json_response) > 500);
    

    然后,您可以删除 HDInsight 群集并在位于 yourdefaultblobstorage.blob.core.windows.net/yourclustercontainer/wasbwork/tweets2 的 blob 存储中找到结果

    如果您想使用常规的 HIVE 表,我建议在 Azure SQL 数据库中使用 Hive 和 Oozie 元存储创建 HDInsight 集群(创建集群时有一个选项),以便 Hive 记住它的存储位置它的数据。

    【讨论】:

    • 您好 benjguin,请您查看问题更新。我尝试了一些将我的 blob 上传到存储的代码。不幸的是,我没有得到任何结果。
    • 您可以使用以下命令在 HDInsight 中获取作业的输出:Get-AzureHDInsightJobOutput -JobId $hiveJob.JobId -StandardError -StandardOutput ` -TaskSummary -Cluster $clusterName
    • 您还可以通过 Invoke-AzureHDInsightHiveJob 使用 PowerShell 调用 Hive,它会自动向您发送结果。您也可以 RDP 到头节点,例如将 HQL 文件存储到 C:\1.hql,然后在桌面上打开命令提示符 cd ..\hive\bin, hive -v -fc: \1.hql
    • 也看看你的存储文件夹。例如,我已经为 (...)/somefolder/*.xxx 创建了一个外部表,它创建了一个名为 *.xxx 的 blob。显然,这不是我所期望的,这在之后产生了错误。
    • 谢谢@benjguin,我会尝试一下您的所有建议,并会尽快回复您。
    【解决方案2】:

    将多个 CSV blob 加载到 hive 表中可以通过以下简单步骤实现。

    首先,我们需要稍微改变数据在容器中的组织方式。我已经使用 'data/csv/filename' 进行了以下格式化

    然后我们可以使用以下配置单元查询一次性加载所有 CSV blob。不需要任何迭代。

    # Provide Windows Azure subscription name, and the Azure Storage account and container that is used for the default HDInsight file system.
    $subscriptionName = "***"
    $storageAccountName = "***"
    $containerName = "***"
    $clusterName = "***"
    
    
    # Use the external table option. 
    $queryString = "DROP TABLE logs;" +
                   "CREATE EXTERNAL TABLE logs(t1 string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION " + 
                   "'wasb://$containerName@$storageAccountName.blob.core.windows.net/data/csv';" 
    
    
    # Create a Hive job definition 
    $hiveJobDefinition = New-AzureHDInsightHiveJobDefinition -Query $queryString
    
    
    # Submit the job to the cluster 
    Select-AzureSubscription $subscriptionName
    $hiveJob = Start-AzureHDInsightJob -Cluster $clusterName -JobDefinition $hiveJobDefinition
    
    
    # Wait for the Hive job to complete
    Wait-AzureHDInsightJob -Job $hiveJob -WaitTimeoutInSeconds 36000
    
    # Get Output
    Get-AzureHDInsightJobOutput -Cluster $clusterName -JobId $hiveJob.JobId -StandardOutput -StandardError
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-12
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 2020-08-01
      相关资源
      最近更新 更多