【问题标题】:Hive partition with relational operator condition具有关系运算符条件的 Hive 分区
【发布时间】:2015-03-05 14:40:44
【问题描述】:

我想创建一个有 2 个分区的 hive 分区表。 一个分数小于 300,另一个分数大于 300。

create table parttab(id int,name string) partitioned by (score int) row format delimited fields terminated by '\t' stored as textfile;

load data local inpath '/data/hive/input' into table newtab partition (score<300);

load data local inpath '/data/hive/newinput' into table newtab partition (score>300);

但是,由于“>”和“

我之所以给出这种方式是因为在查询时 select * from parttab where score

如果我给那个分区命名,例如:

load data local inpath '/data/hive/input' into table newtab partition (score='lessthan300');

然后,在查询时,我必须记住分区的名称!! :(

select * from parttab where score='lessthan300';

这听起来不太好!那么,有没有更好的方式以优雅的方式对其进行分区呢?

【问题讨论】:

    标签: hadoop hive hdfs bigdata hiveql


    【解决方案1】:

    Hive 分区映射到特定值。如果您只有两个分区,那么为这两个范围设置特定值并不是一个糟糕的折衷方案。

    【讨论】:

      【解决方案2】:

      HIVE 不支持分区定义中的 。此外,hive 不会将分区列存储在基础数据中,而是仅保存在分区文件夹名称中。如果您有一些方法可以使用 来实现您所说的分区,这将导致 SCORE 字段的数据丢失,因为您将无法取回每条记录的实际 SCORE 值。 建议的方法是保持分数不变,并根据要求为分区创建一个新列,其值为“NEW”或“OLD”,并根据分数列得出此列值

      like if(score<300) then PART = "OLD" else PART = "NEW"
      

      【讨论】:

        【解决方案3】:

        我会这样做:

        1. 将原始数据加载到临时表中。
        2. 从临时表 (Parttab_temp) 中选择符合逻辑的数据(分数 300)和 INSERT 到 hive 表中。您必须根据所需条件运行几次INSERT INTO 查询。

        不要使用“加载数据”,而是使用INSERT INTO

        INSERT INTO TABLE Parttab PARTITION1
        INSERT INTO TABLE Parttab PARTITION (score)
        SELECT * from Parttab_temp where score < 300; (score) 
        SELECT * from Parttab_temp where score <= 300; (I have used <=33, so records containing exactly 300 are not missed).
        
        INSERT INTO TABLE Parttab PARTITION (score)
        
        SELECT * from Parttab_temp where score > 300;
        

        希望这会有所帮助!

        【讨论】:

          【解决方案4】:

          替代方案:要查找特定分区,您可以使用 hive shell 获取分区,然后使用 grep 提取特定分区。这对我很有效。

           hive -e 'show partitions db.tablename;' | grep 202101*
          
           hive -e 'show partitions db.tablename partition (type='abc');' | grep 202101*
          

          【讨论】:

            猜你喜欢
            • 2018-11-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-02
            • 2015-02-19
            相关资源
            最近更新 更多