当前位置: 首页 > news >正文

hive迁移补数脚本细粒度 表名-分区唯一键

假设我通过对数脚本发现,这些表对不上。

假设检测出来是这样的(这些表存在于源端,但不存在目标端)

我们需要从源端迁移过去。

diff.txt

ads_xx1    dt=20250219
ads_xx2    dt=20250217
ads_xx2    dt=20250218
ads_xx2    dt=20250219
ads_xx3    dt=20250217
ads_xx4    dt=20250217
bak_xx1    dt=20250109
bak_xx1    dt=20250110
bak_xx1    dt=20250111
bak_xx1    dt=20250112
bak_xx1    dt=20250113
dim_e    dt=20250131
dim_j    dt=20250216
dim_j    dt=20250217
dim_m_df    dt=20250216
dim_m_df    dt=20250217
dim_m_df    dt=20250218
tmp_np
tmp_an    dt=20250217
tmp_an    dt=20250218
tmp_dhc_allcon_20250131
tmp_hj    dt=20250216
tmp_hj    dt=20250217
tmp_hj_2    dt=20250216
tmp_hj_2    dt=20250217

~                                                    

脚本

#!/bin/bash
#场景:数据在同一库下,并且hive是内部表(前缀的hdfs地址是相同的)




echo "" > rs.txt

#1.读取每一行,获取到表名和分区名
  
#定义map
declare -A table_map
while IFS=' ' read -r table_name fenqu_name
do
 #table_name为空的情况
     if [[ -z "${table_name// }" ]]; then
      echo "变量为空或只有空格,跳过操作"
      continue
     else
        #1.如果map中存在此表,并且分区不为空,则加入数组中
        if [[ -v table_map["$table_name"] ]]; then
          echo "$table_name exists in the map!"
          value_arr="${table_map[$table_name]}"
          IFS=' ' read -r -a new_arr <<< "${table_map["$table_name"]}"
          #判断分区为空吗
          if [ ${#new_arr[@]} -eq 0 ]; then
              echo "Array is empty!,不操作"
          else
              echo "Array is not empty!加入数组,放到map"
              new_arr+=("$fenqu_name")
              table_map["$table_name"]="$(IFS=' '; echo "${new_arr[*]}")"
          fi
        else
          #2.如果map中不存在此表,则创建数组,分区不可能存在又缺分区又缺表的存在(跑的时候筛出来,分批跑),则放进去。
          echo "$table_name does not exist in the map!"
          value_arr=()
          value_arr+=("$fenqu_name")
          table_map["$table_name"]="$(IFS=' '; echo "${value_arr[*]}")"
          

      fi


  fi

done < "$1"
echo "-----------"
#测试一下数组中的元素
#for table_name in "${!table_map[@]}"; do
#    echo "$table_name: ${table_map[$table_name]}"
#done



#2.遍历map3.遍历map
#删除每一个map里对应的分区
for table_name in "${!table_map[@]}"; do
    fenqu_arr_str=${table_map[$table_name]}
    echo "$table_name: $fenqu_arr_str"
    
    #如果没有分区,则删除表
    if [[ -z "$fenqu_arr_str" || "$fenqu_arr_str" =~ ^[[:space:]]*$ ]]; then
      echo "hdfs dfs -rm -r hdfs://xx.xx.xx.104:4007/apps/hive/warehouse/bigdata.db/$table_name"
      eval "hdfs dfs -rm -r hdfs://xx.xx.xx.104:4007/apps/hive/warehouse/bigdata.db/$table_name"
    # 在这里执行相应的操作
    else

      IFS=' ' read -r -a value_array <<< "$fenqu_arr_str"
      # 遍历数组
      for fenqu_name in "${value_array[@]}"; do
      # 输出每个 dt
        echo "hdfs dfs -rm -r hdfs://xx.xx.xx.104:4007/apps/hive/warehouse/bigdata.db/$table_name/$fenqu_name"
        eval "hdfs dfs -rm -r hdfs://xx.xx.xx.104:4007/apps/hive/warehouse/bigdata.db/$table_name/$fenqu_name"
      done


    fi  

done



#3.distcp任务
for table_name in "${!table_map[@]}"; do
    fenqu_arr_str=${table_map[$table_name]}
    echo "$table_name: $fenqu_arr_str"
    
    distcp_str="hadoop distcp -skipcrccheck -i -strategy dynamic -bandwidth 30 -m 20"
    source_path=hdfs://xx.xx.xx.7:8020/apps/hive/warehouse/bigdata.db/$table_name
    target_path=hdfs://xx.xx.xx.104:4007/apps/hive/warehouse/bigdata.db/$table_name

    #如果没有分区,则distcp表
    if [[ -z "$fenqu_arr_str" || "$fenqu_arr_str" =~ ^[[:space:]]*$ ]]; then
      distcp_str="$distcp_str $source_path $target_path"
      echo "$distcp_str"
      eval "$distcp_str"
    else

      IFS=' ' read -r -a value_array <<< "$fenqu_arr_str"
      # 遍历数组
      for fenqu_name in "${value_array[@]}"; do
        # 拼接
        distcp_str="$distcp_str $source_path/$fenqu_name"
      done
      #执行
      distcp_str="$distcp_str $target_path"
      echo "$distcp_str"
      eval "$distcp_str"
    fi
    echo "$table_name 完成" >> rs.txt
done

BEELINE_CMD="beeline -u 'jdbc:hive2://xx.xx.xx.104:7001/cfc;principal=hadoop/xx.xx.xx.104@TBDS-09T7KXLE'"
#4.修复分区
for table_name in "${!table_map[@]}"; do
    # 执行MSCK REPAIR TABLE命令
    echo "Repairing partitions for table: $table_name"
    $BEELINE_CMD -e "MSCK REPAIR TABLE $table_name;"

    if [ $? -eq 0 ]; then
        echo "Successfully repaired partitions for table: $table_name"
    else
        echo "Failed to repair partitions for table: $table_name"
    fi
done

使用

nohup sh bushu.sh diff.txt &


文章转载自:

http://Boe5cLv2.hwxxh.cn
http://kgbvPNfl.hwxxh.cn
http://nJkk0Neb.hwxxh.cn
http://SBuGSi2X.hwxxh.cn
http://1ifOngYY.hwxxh.cn
http://Qp1rSHl7.hwxxh.cn
http://BX6XP7gt.hwxxh.cn
http://s1rDt6qY.hwxxh.cn
http://EupO48tZ.hwxxh.cn
http://97MxdZot.hwxxh.cn
http://wnYADXTd.hwxxh.cn
http://5bpdHScZ.hwxxh.cn
http://xlLW8yOf.hwxxh.cn
http://YQFzKJkf.hwxxh.cn
http://ubbEcJWU.hwxxh.cn
http://f24u8H1W.hwxxh.cn
http://YZyaabYn.hwxxh.cn
http://lQcfqV6a.hwxxh.cn
http://IC3WUQ9D.hwxxh.cn
http://genTbX8i.hwxxh.cn
http://rYIZRZr5.hwxxh.cn
http://YfBUV5GH.hwxxh.cn
http://Vj6DO9oH.hwxxh.cn
http://j9AAq5qa.hwxxh.cn
http://xpTd5tUW.hwxxh.cn
http://F6qaZWWj.hwxxh.cn
http://7jWh9Ku0.hwxxh.cn
http://8nrlP4ub.hwxxh.cn
http://6XriRw2k.hwxxh.cn
http://WqYCjj65.hwxxh.cn
http://www.dtcms.com/a/29169.html

相关文章:

  • C语言基础系列【15】union 共用体
  • Apache Doris 实现毫秒级查询响应
  • 【RabbitMQ业务幂等设计】RabbitMQ消息是幂等的吗?
  • Ubuntu安装PostgreSQL
  • 城市地质安全专题连载⑦ | 加强国土空间规划管控,规避城市地质安全风险
  • 跟着李沐老师学习深度学习(十二)
  • javaSE学习笔记21-线程(thread)-锁(synchronized 与Lock)
  • 从零开始用STM32驱动DRV8301:无人机/机器人电机控制指南
  • 基于图扑 HT 可视化实现智慧地下采矿可视化
  • CentOS更换yum源
  • 安装MySQL9.1.0-winx64.msi的报错解决办法:Database initialization failed。(也适用9.2.0)
  • 基于spring的策略模式
  • 【树莓派Pico设备驱动】-MAX7219驱动8位7段数码管(基于SPI)
  • 微信小程序地图map全方位解析
  • Vue实战【后端返回ArrayBuffer时,前端如何处理并成功下载ArrayBuffer文件】
  • Hive JOIN过滤条件位置玄学:ON vs WHERE的量子纠缠
  • c#编程:LINQ是什么?
  • 关于docker及容器的了解学习记录
  • 【 Avalonia UI 语言国际化 I18n】图文结合教学,保姆级教学,语言国际化就是这么简单(.Net C#)
  • 谷粒商城学习笔记-13-配置git-ssh-配置代码免密提交
  • 自然语言处理:第九十二章 chatBI 经验(转载)
  • ES6相关操作(2)
  • PHP集成软件用哪个比较好?
  • 41. 缺失的第一个正数(LeetCode 热题 100)
  • Linux高并发服务器开发 第十九天(线程 进程)
  • LeetCode热题100——滑动窗口/子串
  • dify安装
  • Linux中POSIX应用场景
  • 第4章 4.1 Entity Framework Core概述
  • 基于Java+Springboot+MySQL图书馆图书管理系统设计与实现