大数据开发之Hive篇14-Hive归档(Archiving)

备注:
Hive 版本 2.1.1

一.Hive归档简介

由于HDFS的设计,文件系统中的文件数量直接影响namenode中的内存消耗。虽然对于小型集群来说通常不是问题,但当有5000w -1亿个文件时,单个机器上的内存使用可能会达到可访问内存的极限。在这种情况下,尽可能少的文件是有利的。

使用Hadoop Archives是减少分区中文件数量的一种方法。Hive内置了将现有分区中的文件转换为Hadoop Archive (HAR)的支持,这样一个曾经包含100个文件的分区可以只占用~3个文件(取决于设置)。但是,由于从HAR读取数据的额外开销,查询可能会变慢。

注意归档并不压缩文件,HAR类似于Unix tar命令。

在使用存档之前,应该配置3个设置:

hive> set hive.archive.enabled=true;
hive> set hive.archive.har.parentdir.settable=true;
hive> set har.partfile.size=1099511627776;

hive.archive.enabled 参数决定是否可以启用归档。
hive.archive.har.parentdir.settable 通知Hive在创建存档时是否可以设置父目录。在最新版本的Hadoop中,-p选项可以指定归档文件的根目录。例如,如果/dir1/dir2/file以/dir1作为父目录进行归档,那么生成的归档文件将包含目录结构dir2/file。在较老版本的Hadoop(2011年之前)中,这个选项是不可用的,因此必须配置Hive以适应这个限制。
har.partfile 大小控制组成存档的文件的大小。该归档文件将包含size_of_partition/ha.partfile 大小文件,四舍五入。较高的值意味着更少的文件,但由于映射器的数量减少,将导致更长的归档时间。

二.Hive 归档操作

语法:

ALTER TABLE table_name ARCHIVE PARTITION (partition_col = partition_col_value, partition_col = partiton_col_value, ...)

测试数据准备:

CREATE TABLE ods_fact_sale_partion(
  id bigint, 
  prod_name string, 
  sale_nums int)
partitioned by (sale_date string)
STORED AS ORC;

insert into ods_fact_sale_partion partition(sale_date='2010-04-12') 
select id,prod_name,sale_nums,sale_date from ods_fact_sale;

测试代码:

扫描二维码关注公众号,回复: 12470294 查看本文章
-- 归档
alter table ods_fact_sale_partion  archive partition(sale_date='2010-04-12');
-- 恢复归档文件
alter table ods_fact_sale_partion  unarchive partition(sale_date='2010-04-12');

测试记录:
可以看到 归档和恢复归档的操作是非常快的,如果name node压力大,可以将部分历史数据进行归档,需要访问的时候再恢复。

hive> 
    > use test;
OK
Time taken: 1.221 seconds
hive> alter table ods_fact_sale_partion  archive partition(sale_date='2010-04-12');
FAILED: SemanticException [Error 10107]: Archiving methods are currently disabled. Please see the Hive wiki for more information about enabling archiving
hive> 
    > set hive.archive.enabled=true;
hive> set hive.archive.har.parentdir.settable=true;
hive> set har.partfile.size=1099511627776;
hive> alter table ods_fact_sale_partion  archive partition(sale_date='2010-04-12');
intermediate.archived is hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12_INTERMEDIATE_ARCHIVED
intermediate.original is hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12_INTERMEDIATE_ORIGINAL
Creating data.har for hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12
in hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12/.hive-staging_hive_2020-12-29_15-22-05_826_6334418983758563403-1/-ext-10000/partlevel
Please wait... (this may take a while)
20/12/29 15:22:08 INFO client.ConfiguredRMFailoverProxyProvider: Failing over to rm69
Moving hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12/.hive-staging_hive_2020-12-29_15-22-05_826_6334418983758563403-1/-ext-10000/partlevel to hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12_INTERMEDIATE_ARCHIVED
Moving hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12 to hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12_INTERMEDIATE_ORIGINAL
Moving hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12_INTERMEDIATE_ARCHIVED to hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12
OK
Time taken: 29.204 seconds
hive> 
    > alter table ods_fact_sale_partion  unarchive partition(sale_date='2010-04-12');
Copying har://hdfs-nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12/data.har to hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12/.hive-staging_hive_2020-12-29_15-22-45_194_6492345677316543421-1/-ext-10000
Succefully Copied har://hdfs-nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12/data.har to hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12/.hive-staging_hive_2020-12-29_15-22-45_194_6492345677316543421-1/-ext-10000
Moving hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12/.hive-staging_hive_2020-12-29_15-22-45_194_6492345677316543421-1/-ext-10000 to hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12_INTERMEDIATE_EXTRACTED
Moving hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12 to hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12_INTERMEDIATE_ARCHIVED
Moving hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12_INTERMEDIATE_EXTRACTED to hdfs://nameservice1/user/hive/warehouse/test.db/ods_fact_sale_partion/sale_date=2010-04-12
OK
Time taken: 4.119 seconds
hive> 
    > 

参考

1.https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Archiving

猜你喜欢

转载自blog.csdn.net/u010520724/article/details/112303071