linux:shell:执行hive查询并将返回值赋值给shell变量(hive -e / hive -f)

while循环的使用,

if循环的使用,

执行hive语句并赋值给shell变量 

grep过滤无效字符的使用

#!/bin/bash
start_date=$1
end_date=$2
company=$3

#while循环的使用,注意[] 这两个符号内侧都要留有空格
while [ "$start_date" -le "$end_date" ];
do
  current_date=`date -d "$start_date" +%Y-%m-%d`
  echo $company
  echo "current_date====$current_date"

  #这里切记要注意赋值给变量时,=号左右两侧不要有空格!!同时执行hive语句,这里使用反引号给引起来(数字1按键旁边那个符号),以下是直接在本脚本中执行hive语句
  counts0=`hive -e "set hive.cli.print.header=false;select case when count(1) > 0 then 1 else 0 end count from dwb.mid_organizationsnapshot_1 where effectivedate='${current_date}' and company='${company}';" | grep -v WARN`
  echo $counts0
  #if循环的使用 ,注意[] 这两个符号内侧都要留有空格
  if [ $counts0 != 1 ];then
  echo "***********************************************"
  echo "* $company"
  echo "* ${current_date} IS  NULL THEN INSERT INTO TABLE"
  echo "***********************************************"
  #以下是执行sql文件,文件中含有sql语句
  hive -hiveconf company=$company -hiveconf current_date=$current_date -f /var/lib/hadoop-hdfs/spride_sqoop_beijing/bi_table/tang/qt/organizationSnapshotsql.sql
  fi  
  

  counts1=`hive -e "set hive.cli.print.header=false;select case when count(1) > 0 then 1 else 0 end count from dwb.mid_organizationsnapshot_1 where effectivedate='${current_date}' and company='${company}';" | grep -v WARN`
 
  echo "${current_date}====${counts1}"
  #if循环的使用 ,注意[] 这两个符号内侧都要留有空格
  if [ $counts1 == 1 ];then
  echo "${current_date} IS NOT NULL"
  start_date=$(date -d "$start_date+1days" +%Y%m%d)
  else
  echo "${current_date} IS  NULL"
  fi  
done

 

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值