1. 程式人生 > >oracle匯入匯出&hive互動

oracle匯入匯出&hive互動

#!/bin/ksh
# ./oracle_hive.sh -s srcname -p oo|oh
#  sh /home/oracle/bin/oracle_hive.sh -s LTE_DT_RAWDATA_GRID10 -p oo
export NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
if [ $# -ne 4 ]; then
    echo "please input param: $0 -s tabname -p oo|oh"
    exit -1
fi    
# get paramsfi
while [ $# -ne 0 ] ; do
		if [ x$1 == "x-s" ] ; then
				shift
				tabname=$1
				shift
		elif [[ x$1 == "x-p" ]]; then
		    shift
		    synctype=$1
		    shift
		else
		   # print_trace "Unrecorgnized prarmter $1, ignore. "
		    shift
		fi
done
currenttimestamp=$(date +%Y%m%d%H%M%S)
procdate=$(date +%Y%m%d)
monthid=`expr substr ${procdate} 1 6 `
LOG_PATH="/opt/datasync/log/${procdate}"
if [ ! -d "$LOG_PATH" ]; then
		mkdir -p $LOG_PATH
fi
function WRITE_LOG(){
		log_info=$1
		echo "[`date +%F\ %T`]:     $log_info" >> ${LOG_PATH}/${tabname}.${procdate}.log
		echo $log_info
}
function oracle_export(){
		# v_LTE_PROVINCE
		tname=$1
		sqlplus -S zhejiang/zj*2016@//192.168.1.12:1521/fast <$LOG_PATH/$tname.desc
		colbysplit=$(sed -n '3,$p' $LOG_PATH/$tname.ddl |awk '{print $1}'|sed '$d'|awk '{for(i=0;++i<=NF;)a[i]=a[i]?a[i] "||'\'','\''||" $i:$i}END{for(i=0;i++$LOG_PATH/ods_xnlc_${tname}_${procdate}.log
		if [ $? -ne 0 ];
		then
				WRITE_LOG "export ${tname} fail!!!"
				return 1
		else
		    grep -i "selected" $LOG_PATH/ods_xnlc_${tname}_${procdate}.log >> ${LOG_PATH}/${tname}.${procdate}.log
				return 0
		fi
#rm -rf $LOG_PATH/tname1.ddl
#rm -rf $LOG_PATH/$tname.sql
}

function oracle_export_myself(){
		# v_LTE_PROVINCE
		tname=$1
		sqlplus -S sys/oracle@//192.168.130.138:1521/orcl as sysdba <$LOG_PATH/$tname.desc
		colbysplit=$(sed -n '3,$p' $LOG_PATH/$tname.ddl |awk '{print $1}'|sed '$d'|awk '{for(i=0;++i<=NF;)a[i]=a[i]?a[i] "||'\'','\''||" $i:$i}END{for(i=0;i++$LOG_PATH/ods_xnlc_${tname}_${procdate}.log
		if [ $? -ne 0 ];
		then
				WRITE_LOG "export ${tname} fail!!!"
				return 1
		else
				return 0
		fi
#rm -rf $LOG_PATH/tname1.ddl
#rm -rf $LOG_PATH/$tname.sql
}
function oracle_load(){
		tabname=$1
		col=$(cat $LOG_PATH/$tabname.desc|awk '{print $1}'|tr -s '\n' ','|sed 's/,$/\n/g')
		colandtype=$(cat $LOG_PATH/$tabname.desc|tr -s '\n' ','|sed 's/,$/\n/g')
		WRITE_LOG "col is : $col"
		WRITE_LOG "colandtype is : $colandtype"
		sqlplus -S sys/oracle@//192.168.130.138:1521/orcl as sysdba <$LOG_PATH/$tabname.ctl <>$LOG_PATH/hive_$tabname.hql
		n=0
		while read line
		do
				n=n+1
				if [[ "$line" =~ VARCHAR2 ]]; then
						echo $line|tr -s "VARCHAR2" "VARCHAR"  >>$LOG_PATH/hive_$tabname.hql
				elif [[ "$line" =~ NUMBER\(\d{1,3},\d{1,3}\) ]]; then
						echo $line|tr -s "NUMBER" "DECIMAL"  >>$LOG_PATH/hive_$tabname.hql
				elif [[ "$line" =~ NUMBER$ ]]; then
						echo $line|tr -s "NUMBER" "BIGINT"  >>$LOG_PATH/hive_$tabname.hql
				else
						echo $line >>$LOG_PATH/hive_$tabname.hql
				fi
				if [ $n -eq $lastline ]; then
						echo ") row format delimited fields terminated by ',';" >>$LOG_PATH/hive_$tabname.hql
				else
						echo -n "," >>$LOG_PATH/hive_$tabname.hql
				fi
				echo -n "," >>$LOG_PATH/hive_$tabname.hql
		done < $LOG_PATH/$tabname.desc
		hive -f $LOG_PATH/hive_$tabname.hql
		if [ $? -ne 0 ];
		then
				WRITE_LOG "create hive table ${tname} fail!!!"
				return 1
		else
		 		WRITE_LOG "create hive table ${tname} success!!!"
				return 0
		fi
}

WRITE_LOG "PROCESS START !"
WRITE_LOG "start export $tabname:"
oracle_export $tabname
if [ $? -ne 0 ]; then
		exit -1
fi
WRITE_LOG "export ${tabname} END!!!"
if [ $synctype == "oo" ]; then
		WRITE_LOG "oracle LOAD START:"
		oracle_load $tabname
		if [ $? -ne 0 ]; then
				exit -1
		fi
		WRITE_LOG "oracle LOAD END:"
elif [ $synctype == "oh" ]; then
		WRITE_LOG "start create hive table:"
		create_hive_table_byoracle $tabname
		if [ $? -ne 0 ]; then
				exit -1
		fi
		WRITE_LOG "create hive table END:"
		WRITE_LOG "start load hive table $tabname:"
		hive_load $tabname
		WRITE_LOG "end load hive table $tabname"
else
		write_log "undefined type and exit...."
		exit -1
fi