apache sgoop 导入数据到 oracle、导出数据到 oracle 实现

业务场景:是在oracle 数据库和  hive 数据库中 ,有多个相同结构的表,要求数据从2个库定时双向同步。

(导出时可以只导出部分字段,则此时 hive 库和 oracle 库中表结构可以并非完全一致)

1. 写一个文本文档,把要导入的表名和库名先编辑好,格式如 oracle_table_list_append.txt :

wate.BUSI_xxx
wate.xxx_xxx_INFO
wate.xxx_USER_xxx
... 

# wate 是 oracle 数据库名, BUSI_xxx 是表名。
# 可写多行,脚本执行时读取每个一行,循环导入每个表的数据。

编写sgoop  import 脚本并执行即可把数据从 oracle 数据库导入到 hive 数据库中。

current=`date -d "yesterday" +%Y-%m-%d`     #获取当前时间
#current=`date -d "yesterday 20190110" +%Y-%m-%d`     #获取指定日期的前一天

begin=${current}" 00:00:00"

sed -i '/^$/d' /opt/xxx/sqoop/conf/oracle_table_list_append.txt
cat /opt/xxx/sqoop/conf/oracle_table_list_append.txt|while read line
do
    OLD_IFS="$IFS"
    IFS="."
    arr=($line)
    db_name=${arr[0]}
    table_name=${arr[1]}
    echo "####begin to sqoop from oracle "${db_name}.${table_name} "......"

    sqoop import \
    --connect jdbc:oracle:thin:@192.168.xx.xx:1521/${db_name} \
    --username 数据库名 \
    --password 数据库密码 \
    --table ${table_name} \
    --where "creationtime >=to_date('${begin}','yyyy-mm-dd hh24:mi:ss') or xxxtime>=to_date('${begin}','yyyy-mm-dd hh24:mi:ss') or dxxtime>=to_date('${begin}','yyyy-mm-dd hh24:mi:ss') " \
    --hive-drop-import-delims \
    --null-string '\\N' \
    --null-non-string '\\N' \
    --fields-terminated-by '\007' \
    --lines-terminated-by '\n' \
    --hive-import \
    --hive-table hive数据库名.${table_name} \
    -m 10

    rc=$?
    if [ $rc -ne 0 ]
        then
            echo "#### sqoop from oracle "${db_name}.${table_name} " failed......"
           # echo "#### sqoop from oracle "${db_name}.${table_name} " failed......"|mail -s "failed crontab_${current}" [email protected]
        else
            echo "####sqoop from oracle "${db_name}.${table_name} " successed......"
    fi

done
current=`date "+%Y-%m-%d %H:%M:%S"`     #获取当前时间
end_timeStamp=`date -d "$current" +%s`
du_time=$[end_timeStamp-begin_timeStamp]
echo "sqoop total time used:" $du_time "s" 
--where 此参数是条件过滤,全表导入,可不加此参数。

2.  编写sgoop  export 脚本并执行,则可把数据从 hive 库 导出到 oracle 数据库。 

current_datetime=`date "+%Y-%m-%d %H:%M:%S"`
echo "Start Timing Task:"$current_datetime
current=`date "+%Y-%m-%d %H:%M:%S"`     #获取当前时间
begin_timeStamp=`date -d "$current" +%s`


table_name="cxx_xxx_day"
day=`date -d -1days "+%Y%m%d"` #昨天(此表以天分区,每次只导一天的数据)

    echo "####开始导出"${day}"的数据 into oracle oracle数据库名."${table_name} "......"
    sqoop export \
    --connect "jdbc:oracle:thin:@192.168.xxx.xxx:1521/oracle数据库连接名" \
    --username oracle数据库名 \
    --password oracle数据库密码 \
    --table ${table_name} \
    --export-dir "/user/hive/xxx/hivexxx.db/xxx_xxx_day/day=${day}" \
    --columns xxx_no,datatime,xxx_value,xxx_status,xxx_voage,staxx_str,stxxxs_hex,fy_id \
    --input-fields-terminated-by '\007' \
    --input-lines-terminated-by '\n' \
    --input-null-string '\\N' \
    --input-null-non-string '\\N' \
    --m 4
    rc=$?
    if [ $rc -ne 0 ]
        then
	    echo "Timing Task Error:"
            echo "####sqoop from hive into oracle "${table_name} " failed......"
        else
            echo "####sqoop from hive into oracle "${table_name} " successed......"
    fi
current=`date "+%Y-%m-%d %H:%M:%S"`     #获取当前时间
end_timeStamp=`date -d "$current" +%s`
du_time=$[end_timeStamp-begin_timeStamp]
echo "sqoop total time used:" $du_time "s"
--export-dir 指定HDFS上的文件路径
--columns 指定要导出的列,可以只导出部分列

 

3. 官网说明文档:http://sqoop.apache.org/docs/1.4.7/SqoopUserGuide.html

要查更多参数和用法可以直接查询官方文档。

import说明文档地址:http://sqoop.apache.org/docs/1.4.7/SqoopUserGuide.html#_literal_sqoop_import_literal

export说明文档地址:http://sqoop.apache.org/docs/1.4.7/SqoopUserGuide.html#_literal_sqoop_export_literal

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章