shell批量执行hive语句

需求:将hive里数据按月按天处理后导入另一个表中:


#!/usr/bin/env bash 
year=$1
m=$2
len=${#m}
if [ $len == 1 ] 
then
  month="0"$m
else 
  month=$m
fi

d=$3
day=0
for i in $(seq 1 $d)
do
  length=${#i}
  if [ $length == 1 ] 
  then
    day="0"$i
  else
    day=$i
  fi  
  #echo $length,$i,$day
  insert_sql="set mapreduce.job.queuename=bi; insert into my_schema.daily_slot_info  select i.ymd , i.adslot_id as slot_id , imp, clk  from  ( select concat(year,month,day) as ymd, adslot_id, count(zid) as imp  from your.imp_t t  where year='$year' and month='$month' and day='$day' and vendor_id= 9  group by concat(year,month,day), adslot_id  ) i  left join ( select  concat(year,month,day) as ymd , adslot_id, count(zid) as clk   from your.clk_t t  where year='$year' and month='$month' and day='$day' and vendor_id = 9  group by concat(year,month,day), adslot_id   ) c  on i.ymd = c.ymd and  i.adslot_id = c.adslot_id ;"

  echo $insert_sql
  hive -e "$insert_sql"
  exit_code=$?
  if [ $exit_code -eq 0 ];then
      echo "$month $day successful!"
  else
      echo "error at $month $day"
      exit $exit_code
  fi
done