我想对hive表应用存档和清除机制,包括内部和外部表以及分区和非分区表。
我有一个用visit_date分区的site_visitors表。我想归档site_visitors表数据,其中用户在过去一年中没有访问过我的网站。同时,我不希望将这些存档数据保存在同一个表目录中。我可以把数据归档到某个特定的地方。
发布于 2017-12-12 00:42:50
您可以在HDFS目录中的分区上处理它,下面是您可以实现的方法之一。
您的内部表/主表将位于hdfs之上,目录将类似于下面的hdfs:namenonde/user/hive/warehouse/schema.db/site_visitors/visit_date=2017-01-01 hdfs:namenonde/user/hive/warehouse/schema.db/site_visitors/visit_date=2017-01-02 hdfs:namenonde/user/hive/warehouse/schema.db/site_visitors/visit_date=2017-01-03
您可以在HDFS之上创建一个存档表,或者如果您只是想归档数据,您可以将分区转储到HDFS中的其他位置。无论哪种方式,您的HDFS位置将如下所示。
hdfs:namenonde/hdfs_location/site_visitors/visit_date=2017-01-01 hdfs:namenonde/hdfs_location/site_visitors/visit_date=2017-01-02 hdfs:namenonde/hdfs_location/site_visitors/visit_date=2017-01-03
您可以运行UNIX脚本或javascript,或使用环境中使用的任何其他语言,根据分区日期将文件从一个HDFS位置移动到另一个归档hdfs位置。
您还可以使用下面的方法,您可以将数据加载到存档表中,并将数据放入原始表中。
#!bin/bash
ARCHIVE=$1
now=$(date +%Y-%m-%d)
StartDate=$now
#archive_dt will give a date based on the ARCHIVE date and that be will used for alterations and loading
archive_dt=$(date --date="${now} - ${ARCHIVE} day" +%Y-%m-%d)
EndDate=$archive_dt
#You can use hive or beeline or impala to insert the data into archive table, i'm using beeline for my example
beeline -u ${CONN_URL} -e "insert into table ${SCHEMA}.archive_table partition (visit_date) select * from ${SCHEMA}.${TABLE_NAME} where visit_date < ${archive_dt}"
#After the data been loaded to the archive table i can drop the partitions in original table
beeline -u ${CONN_URL} -e "ALTER TABLE ${SCHEMA}.main_table DROP PARTITION(visit_date < ${archive_dt})"
#Repair the tables to sync the metadata after alterations
beeline -u ${CONN_URL} -e "MSCK REPAIR TABLE ${SCHEMA}.main_table; MSCK REPAIR TABLE archiveSchema.archive_table"
https://stackoverflow.com/questions/47755972
复制相似问题