如何使用列的值范围对 Hive Table 进行分区

How to partition a Hive Table using range of values for a column

我有一个 Hive Table,有 2 个 columns.Employee ID 和 Salary。

数据如下所示。

Employee ID Salary
1   10000.08
2   20078.67
3   20056.45
4   30000.76
5   10045.14
6   43567.76

我想根据薪水创建分区 Column.For 薪水范围 10000 到 20000、20001 到 30000 的示例分区。

我该如何实现。

Hive 不支持范围分区,但您可以在数据加载期间计算范围。

  1. 创建 table 分区 salary_range:

    create table your_table
    (
     employee_id bigint,
     salary double
    )
    partitioned by (salary_range bigint)
    
  2. 插入工资范围计算用例:

    insert overwrite table your_table partition (salary_range)   
    select employee_id, salary,  
           case 
               when salary between 10000 and 20000 then 20000
               when salary between 20001 and 30000 then 30000 
               ...
               else ...
           end as salary_range 
    from some_table;