第一步:备份数据:
如以下几个表:为假设
uoc_order_t
uoc_product_t
uoc_source_t
查询存储路径:使用语句:desc formatted uoc_order_t;
hive>
desc formatted intf.uoc_order_t;
desc formatted intf.uoc_product_t;
desc formatted intf.uoc_source_t;
以下内容为存储路径为以下内容
Location: hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_t
Location: hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_t
Location: hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t
备份数据:
hadoop dfs -cp hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_tbak
hadoop dfs -cp hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_tbak
hadoop dfs -cp hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_tbak
检查数据库
大小
hadoop dfs -du -h -s hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_tbak
hadoop dfs -du -h -s hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_tbak
hadoop dfs -du -h -s hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_tbak
记录建表分库信息:执行脚本:
创建一个文件table_list
并将三个表贴进去,一行一个表
uoc_order_t
uoc_product_t
uoc_source_t
------------------------------------------
#!/bin/bash
:>create_partition.sh
last_dir="hdfs://rtpha/dzqd/hive/warehouse/intf.db"
new_dir="hdfs://rtpha/dzqd/hive/warehouse/intf.db"
for table in $(cat table_list);do
hive -e "use intf; show create table $table "|grep 'hdfs://'>tmp_2
hdfs_path=$(cat tmp_2)
hdfs_path=$(echo $hdfs_path)
hdfs_path=$(echo $hdfs_path|sed "s/[\']//g")
hive -e "use intf; show partitions $table ">tmp_1
for line in $(cat tmp_1);do
if [ "x$line" != "x" ];then
i=0
for line_tmp in $(echo $line|tr '/' ' ');do
echo $line_tmp
line1=$(echo $line_tmp|awk -F\= '{print $1}')
line2=$(echo $line_tmp|awk -F\= '{print $2}')
if [ "x$i" = "x0" ];then
echo $line1
echo $line2
partition_str="$line1='$line2'"
else
partition_str="$partition_str,$line1='$line2'"
fi
((i=i+1))
done
echo $hdfs_path
hdfs_path=$(echo $hdfs_path| sed "s#$last_dir#$new_dir#g")
echo $hdfs_path
echo $partition_str
echo "alter table $table add partition($partition_str) location '$hdfs_path/$line';">>create_partition.sh
else
continue
fi
done
done
--------------------------------------------------------------------------------------------------------------------------------------------
删除表:
drop table XXXXX;
创建表:
贴入建表语句,注意新加语句在最后
删除原表hdfs文件
hadoop dfs -rmr hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t
将备份数据拷过来
hadoop dfs -cp hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_tbak hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t
添加分区:
进入hive 选择intf
hive> use intf;
将create_partition.sh 里面的语句贴入
完了后,查询分区
show partitions uoc_order_t;
查数据:
select * from uoc_order_t;
如以下几个表:为假设
uoc_order_t
uoc_product_t
uoc_source_t
查询存储路径:使用语句:desc formatted uoc_order_t;
hive>
desc formatted intf.uoc_order_t;
desc formatted intf.uoc_product_t;
desc formatted intf.uoc_source_t;
以下内容为存储路径为以下内容
Location: hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_t
Location: hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_t
Location: hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t
备份数据:
hadoop dfs -cp hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_tbak
hadoop dfs -cp hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_tbak
hadoop dfs -cp hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_tbak
检查数据库
大小
hadoop dfs -du -h -s hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_order_tbak
hadoop dfs -du -h -s hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_product_tbak
hadoop dfs -du -h -s hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_tbak
记录建表分库信息:执行脚本:
创建一个文件table_list
并将三个表贴进去,一行一个表
uoc_order_t
uoc_product_t
uoc_source_t
------------------------------------------
#!/bin/bash
:>create_partition.sh
last_dir="hdfs://rtpha/dzqd/hive/warehouse/intf.db"
new_dir="hdfs://rtpha/dzqd/hive/warehouse/intf.db"
for table in $(cat table_list);do
hive -e "use intf; show create table $table "|grep 'hdfs://'>tmp_2
hdfs_path=$(cat tmp_2)
hdfs_path=$(echo $hdfs_path)
hdfs_path=$(echo $hdfs_path|sed "s/[\']//g")
hive -e "use intf; show partitions $table ">tmp_1
for line in $(cat tmp_1);do
if [ "x$line" != "x" ];then
i=0
for line_tmp in $(echo $line|tr '/' ' ');do
echo $line_tmp
line1=$(echo $line_tmp|awk -F\= '{print $1}')
line2=$(echo $line_tmp|awk -F\= '{print $2}')
if [ "x$i" = "x0" ];then
echo $line1
echo $line2
partition_str="$line1='$line2'"
else
partition_str="$partition_str,$line1='$line2'"
fi
((i=i+1))
done
echo $hdfs_path
hdfs_path=$(echo $hdfs_path| sed "s#$last_dir#$new_dir#g")
echo $hdfs_path
echo $partition_str
echo "alter table $table add partition($partition_str) location '$hdfs_path/$line';">>create_partition.sh
else
continue
fi
done
done
--------------------------------------------------------------------------------------------------------------------------------------------
删除表:
drop table XXXXX;
创建表:
贴入建表语句,注意新加语句在最后
删除原表hdfs文件
hadoop dfs -rmr hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t
将备份数据拷过来
hadoop dfs -cp hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_tbak hdfs://rtpha/dzqd/hive/warehouse/intf.db/uoc_source_t
添加分区:
进入hive 选择intf
hive> use intf;
将create_partition.sh 里面的语句贴入
完了后,查询分区
show partitions uoc_order_t;
查数据:
select * from uoc_order_t;