分享一个使用get_hash_value比对数据脚本

使用get_hash_value获取每个字段的值,再sum起来比对,如果表有lob字段,则会先排除掉lob字段再比对其它字段

这个脚本有两个问题:

1.如果字段所有的值长度加起来超过4000会报错,比对不了,这种情况一般比较少

2.二进制数据不支持,例如blob

脚本需要修改:

1.DS_CONNECT 和DT_CONNECT配置为需要比对的源端和目标端的连接串

2.COMP_TYPE如果是1则是比对用户,你将需要比对的用户写到COMP_USER中

如果是2则是比对指定表,你需要手工将 用户名.表名 写入到tab.txt中

3.FLAG配置一下比对的标识符,随便写,标识比对的哪个库

4.配置一下NLS_LANG防止有中文表或者中文字段

其它:

比对的结果会在当前目录下,tab.info.xxx是详细结果 tab.err.xxx是比对不一致的表 tab.ok.xxx是比对一致的表,上次比对的结果保存在history目录下

如果比对过程中出现报错,可以检查.c.done和.c.done.bak这两个个隐藏文件

.hash.sql和.hash.sql.bak是具体执行的sql语句

bash 复制代码
#!/bin/bash
. ~/.bash_profile

##### ERROR:: integer expression expected : table no columns without unsupport datatype columns
##### ERROR:: ORA-01489                   : column values concat larger then 40000, unsupport

export WORKDIR=$(cd `dirname $0`/; pwd)                 

##modify
##USER and PWD
export FLAG=wzjj_lob
export DS_CONNECT=xx/xxxx@source
export DT_CONNECT=xx/xxxx@target
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK

###COMP_TYPE:1-user 2-table
###if COMP_TYPE=1,please modify COMP_USER
###if COMP_TYPE=2,plase write OWNER.TABLE_NAME to $TABLE_FILE
export COMP_TYPE=1
export COMP_USER="('BUSINESS_QRY','TEST')"
export TABLE_FILE=$WORKDIR/tab.txt


####init tab.txt
if [ $COMP_TYPE -eq 1 ]
then

sqlplus -S $DS_CONNECT <<EOF >$WORKDIR/tab.txt
set pagesize 0 feedback off verify off heading off echo off long 9999 longchunksize 9999 line 5000
select owner||'.'||table_name from dba_tables where owner in $COMP_USER
/
EOF
fi



########
LOGFILE_OPERTIME=`date +%Y%m%d%H%M`

ALL_ROW=`cat $TABLE_FILE |wc -l`

####check oracle version
VERSION=`sqlplus -S $DS_CONNECT <<EOF
set pagesize 0 feedback off verify off heading off echo off long 9999 longchunksize 9999 line 5000
select substr(value,1,instr(value,'.')-1) from  v\\\$parameter where name='compatible'
/
EOF
`
#echo $VERSION

if [ $VERSION -gt 11 ]
then
	TSQL=TSQL12C
else
	TSQL=TSQL11G
fi

#####################################################################################

TSQL11G(){
EXEC_SQL=`sqlplus -S $DS_CONNECT <<EOF
set pagesize 0 feedback off verify off heading off echo off long 9999 longchunksize 9999 line 5000
select 'select nvl(sum(dbms_utility.get_hash_value('||replace(to_char(
wm_concat(
decode(data_type,'LONG','','LONG RAW','','BLOB','','CLOB','','NCLOB','''0''','"'||COLUMN_NAME||'"')
)),',','||')
||',0,power(2,30))),0) from "'||owner||'"."'||table_name||'";'
from dba_tab_columns where owner||'.'||table_name='$1'  group by owner,table_name
/
EOF
`
echo "$EXEC_SQL" >$WORKDIR/.hash.sql
}

TSQL12C(){
EXEC_SQL=`sqlplus -S $DS_CONNECT <<EOF
set pagesize 0 feedback off verify off heading off echo off long 9999 longchunksize 9999 line 5000
select 'select nvl(sum(dbms_utility.get_hash_value('||to_char(listagg(
decode(data_type,'LONG','','LONG RAW','','BLOB','','CLOB','','NCLOB','''0''','"'||COLUMN_NAME||'"')
,'||'))
||',0,power(2,30))),0) from "'||owner||'"."'||table_name||'";'
from dba_tab_columns where owner||'.'||table_name='$1'  group by owner,table_name
/
EOF
`
echo "$EXEC_SQL" >$WORKDIR/.hash.sql
}

SOURCE_COUNT(){
SOURCE_NUM=`sqlplus -S $DS_CONNECT <<EOF
set pagesize 0 feedback off verify off heading off echo off numf 999999999999999999999999999999999
@$WORKDIR/.hash.sql
EOF
`
echo s $SOURCE_NUM >> $WORKDIR/.c.done
}

TARGET_COUNT(){
TARGET_NUM=`sqlplus -S $DT_CONNECT <<EOF
set pagesize 0 feedback off verify off heading off echo off numf 9999999999999999999999999999999999
@$WORKDIR/.hash.sql
EOF
`
echo t $TARGET_NUM >> $WORKDIR/.c.done
}

#######################################################################################################
##init file & backup file



mkdir -p $WORKDIR/history
mv -f $WORKDIR/tab.info* $WORKDIR/history 2>/dev/null
mv -f $WORKDIR/tab.err* $WORKDIR/history 2>/dev/null
mv -f $WORKDIR/tab.ok* $WORKDIR/history 2>/dev/null

date
> $WORKDIR/tab.ok.$LOGFILE_OPERTIME
> $WORKDIR/tab.err.$LOGFILE_OPERTIME
> $WORKDIR/.c.done.bak
> $WORKDIR/.c.done
> $WORKDIR/.hash.sql.bak

printf "%-12s %-10s %-40s %-18s %-18s %-6s %-20s %-20s\n"  "ID" "FLAG" "OWNER.TABLE" "DS_HASH"  "DT_HASH" "STATUS" "START_TIME" "END_TIME" |tee -a $WORKDIR/tab.info.$LOGFILE_OPERTIME
echo "------------------------------------------------------------------------------------------------------------------------------------------------------------" |tee -a $WORKDIR/tab.info.$LOGFILE_OPERTIME
ID=1

##########################################################for 
while read TABLEINFO 
do
	
	cat $WORKDIR/.c.done >>$WORKDIR/.c.done.bak  2>/dev/null
	cat $WORKDIR/.hash.sql >>$WORKDIR/.hash.sql.bak 2>/dev/null
	> $WORKDIR/.c.done
	
	$TSQL $TABLEINFO
	
	START_OPERTIME=`date +"%Y-%m-%d:%H:%M:%S"`
	SOURCE_COUNT $TABLEINFO &
	TARGET_COUNT $TABLEINFO &
	
#	ALL_ROW=`echo $TABLE_INFO|awk '{print NF}'`
	
	while true
	do
	if [ `cat $WORKDIR/.c.done |wc -l ` -eq 2 ]
	then
		SOURCE_NUM=`cat $WORKDIR/.c.done |awk '$1=="s" {print $2}'`
		TARGET_NUM=`cat $WORKDIR/.c.done |awk '$1=="t" {print $2}'`
		END_OPERTIME=`date +"%Y-%m-%d:%H:%M:%S"`
		if [ $SOURCE_NUM -ne $TARGET_NUM ]
		then
			COMP_STATUS=2
			echo $TABLEINFO >> $WORKDIR/tab.err.$LOGFILE_OPERTIME
		else
			COMP_STATUS=1
			echo $TABLEINFO >> $WORKDIR/tab.ok.$LOGFILE_OPERTIME
		fi
	
		printf "%-12s %-10s %-40s %-18s %-18s %-6s %-20s %-20s\n" [$ID/$ALL_ROW] $FLAG $TABLEINFO $SOURCE_NUM $TARGET_NUM $COMP_STATUS $START_OPERTIME $END_OPERTIME |tee -a $WORKDIR/tab.info.$LOGFILE_OPERTIME
		break
	else
		sleep 0.1
	fi
	done
	ID=$(($ID+1))

done <$TABLE_FILE
date

脚本执行完结果如下:

相关推荐
橘子味的茶二17 小时前
ffmpeg内存模型
ffmpeg
TPCloud17 小时前
windows 11编译安装ffmpeg(包含ffplay)
windows·ffmpeg·源码安装·mysys
runing_an_min1 天前
ffmpeg视频滤镜:缓入缓出-fade
ffmpeg·音视频·fade·缓出·缓入
ssslar2 天前
FFMPEG录屏(22)--- Linux 下基于X11枚举所有显示屏,并获取大小和截图等信息
linux·运维·ffmpeg
MonkeyKing_sunyuhua2 天前
FFmpeg 怎么裁剪m4a的音频,从一个时间点开始,裁剪15秒钟的视频
ffmpeg·音视频
DO_Community2 天前
教程:FFmpeg结合GPU实现720p至4K视频转换
ffmpeg·音视频
x66ccff2 天前
使用NVIDIA GPU加速FFmpeg视频压制:完全指南
ffmpeg·音视频
冷眼Σ(-᷅_-᷄๑)3 天前
如何使用ffmpeg命令行进行录屏
ffmpeg
Lary_Rock3 天前
ubuntu22.04 安装ffmpeg
ffmpeg
m0_623171553 天前
Windows搭建流媒体服务并使用ffmpeg推流播放rtsp和rtmp流
windows·ffmpeg