栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Hive 循环插数脚本

Hive 循环插数脚本

说明

当前有一个日期分区表,分区字段为 ds ,我们需要通过循环每天日期实现把一段日期的数据插入另外一张表中,建表语句如下:

-- 建表语句
CREATE TABLE crm_user_info (
  cust_no         string COMMENT '客户编号',
  cust_name       string COMMENT '客户姓名',
  gender          string COMMENT '客户性别',
) COMMENT '客户信息表'
partitioned by (ds string)
stored as orc tblproperties ('orc.compress'='SNAPPY')
;

注意:此处可以通过查询分区的方式将多个分区一次性插入,该种方式仅为演示该脚本的实现

#!/bin/sh

begin_date=20211014
end_date=20220131

# shellcheck disable=SC2034
this_script_name=${0##*/}

exitCodeCheck() {
    if [ $1 -gt 0 ]; then
        echo '${this_script_name} fail exit_code=$1,reason: $2'
        exit $1
    else
        echo 'shell execute success'
    fi
}

# shellcheck disable=SC2034
for i in `seq 1 300`; do
  # shellcheck disable=SC2039
  if [[ ${begin_date} -gt ${end_date} ]]; then
    break
  fi
  echo "start run ${begin_date}"
  hive -e "
  INSERT OVERWRITE TABLE crm_user_info_temp partition(ds = '${begin_date}')
  SELECt * 
  FROM crm_user_info 
  WHERe ds = '${begin_date}'
  "
  result=$?
  exitCodeCheck $result
  begin_date=$(date -d "${begin_date} +1 day" +%Y%m%d)
done

echo ">>>>>>>>>>>>>>> Done at `date +"%F %H:%M:%S"` <<<<<<<<<<<<<<<"

exit 0
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/730997.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号