问题描述
需要将ods库中所有表的bigint和boolean类型修改成string类型
700多个表 ,根本不可能手动一个一个去修改
所以写了一个脚本
问题解决
#! /bin/bash
task=$1
cat $task | while read table
do
# 获取当前表的字段名称和字段类型
hive -e"
use ods;
desc ${table};
">${task}_temp.txt
# 遍历temp.txt 如果是bigint 或者是boolean就修改类型为string
cat ${task}_temp.txt | while read line
do
# 取出字段类型和字段名称
column=$(echo $line | awk '{print $1}')
c_type=$(echo $line | awk '{print $2}')
# 判断字段类型并修改
if [[ ${c_type} == 'bigint' || ${c_type} == 'boolean' ]];then
hive -e"
use ods;
alter table ${table} change column ${column} ${column} string;
"
fi
done
echo ${table}完成
done
如果表特别多,比如我这里700多个,可以首先将文件按照行分割成多个小文件,然后同时执行
#分割文件
split -l 100 文件名 分割后的文件前缀
#例如
split -l 100 table.txt table
# 结果
tableaa tableab tableac tablead tableae tableaf tableag tableah
写一个启动脚本,这里手法有点垃圾,主要文件少,文件多的话可以写一个循环,我直接就yy,pppppppp…了
#! /bin/bash
nohup sh ch_hive_columns_type_plus.sh ./task/tableaa &
nohup sh ch_hive_columns_type_plus.sh ./task/tableab &
nohup sh ch_hive_columns_type_plus.sh ./task/tableac &
nohup sh ch_hive_columns_type_plus.sh ./task/tablead &
nohup sh ch_hive_columns_type_plus.sh ./task/tableae &
nohup sh ch_hive_columns_type_plus.sh ./task/tableaf &
nohup sh ch_hive_columns_type_plus.sh ./task/tableag &
nohup sh ch_hive_columns_type_plus.sh ./task/tableah &