这是一个完整的演示,描述了 2 个基本选项:
- 向表中添加分区。 HDFS 目录将自动创建。
- 将目录添加到 HDFS 并在表上应用 msck repair 以添加分区。
请注意,我对分区使用 DATE 类型。
日期文字的 ANSI/ISO 表示法是 date 'YYYY-MM-DD'
即使对于不支持 DATE 类型的旧版本,我也强烈建议避免使用除 YYYY-MM-DD 之外的任何日期格式,原因有两个:
1. 这是日期函数唯一支持的格式。
2. 这种格式允许正确的字母比较,例如-
'2017-01-22' > '2016-09-22' 但 '01-22-2017'
演示
重击
hdfs dfs -mkdir -p /hive/warehouse/sample_database/sample_table
蜂巢
create external table sample_table
(
i int
)
partitioned by (day date)
location '/hive/warehouse/sample_database/sample_table'
;
选项 1 - 更改表 ... 添加分区 ...
蜂巢
alter table sample_table add partition (day=date '2017-03-02');
alter table sample_table add partition (day=date '2017-03-03');
hive> show partitions sample_table;
OK
day=2017-03-02
day=2017-03-03
Time taken: 0.067 seconds, Fetched: 2 row(s)
hive> dfs -ls /hive/warehouse/sample_database/sample_table;
Found 2 items
... 2017-03-04 23:31 /hive/warehouse/sample_database/sample_table/day=2017-03-02
... 2017-03-04 23:31 /hive/warehouse/sample_database/sample_table/day=2017-03-03
hive>
选项 2 - hdfs dfs -mkdir ... + msck 修复表 ...
重击
hdfs dfs -mkdir /hive/warehouse/sample_database/sample_table/day=2017-03-02
hdfs dfs -mkdir /hive/warehouse/sample_database/sample_table/day=2017-03-03
蜂巢
msck repair
hive> show partitions sample_table;
OK
Time taken: 0.187 seconds
hive> msck repair table sample_table;
OK
Partitions not in metastore: sample_table:day=2017-03-02 sample_table:day=2017-03-03
Repair: Added partition to metastore sample_table:day=2017-03-02
Repair: Added partition to metastore sample_table:day=2017-03-03
Time taken: 0.143 seconds, Fetched: 3 row(s)
hive> show partitions sample_table;
OK
day=2017-03-02
day=2017-03-03
Time taken: 0.076 seconds, Fetched: 2 row(s)
hive> dfs -ls /hive/warehouse/sample_database/sample_table;
Found 2 items
... 2017-03-04 23:31 /hive/warehouse/sample_database/sample_table/day=2017-03-02
... 2017-03-04 23:31 /hive/warehouse/sample_database/sample_table/day=2017-03-03
hive>