数据从关系型数据库到大数据平台再到关系型数据库

来源:互联网 发布:电脑扩音器软件 话筒 编辑:程序博客网 时间:2024/04/30 13:02
                                                                        关系型数据库到大数据平台到关系型数据库shell脚本
测试脚本
#!/bin/bash
yy=$(date -d 'yesterday' +'%Y')
mm=$(date -d 'yesterday' +'%m')
dd=$(date -d 'yesterday' +'%d')
建基础表分区目录
hdfs dfs -mkdir -p /ZYB/ORDER_INFO/yy=$yy/mm=$mm

数据增量导入到hdfs的shell脚本

采用了snappy压缩

#!/bin/bash
yy=$(date -d 'yesterday' +'%y')
YY=$(date -d 'yesterday' +'%Y')
mm=$(date -d 'yesterday' +'%m')
dd=$(date -d 'yesterday' +'%d')
hdfs dfs -mkdir -p /ZYB/CHECK_RECORD/yy=$yy/mm=$mm
sqoop import --connect jdbc:oracle:thin:@10.26.122.122:1521:ORCL --username read_bigdata --password HdmHz4i!hbkalv1pcbez --target-dir /ZYB/CHECK_RECORD/yy=${yy}/mm=${mm}/dd=${dd} --num-mappers 1 --table ZHIYOUBAO.CHECK_RECORD --hive-drop-import-delims --fields-terminated-by '\001' --lines-terminated-by '\n' --optionally-enclosed-by '\"' --where "to_char(CREATE_TIME,'yyyy-mm-dd')='${YY}-${mm}-${dd} '" --compress  --compression-codec org.apache.hadoop.io.compress.SnappyCodec
hive --database zhiyoubao -e "alter table CHECK_RECORD add partition (year=${yy},month=${mm},day=${dd}) location '/ZYB/CHECK_RECORD/yy=${yy}/mm=${mm}/dd=${dd}/';"
数据加载到基础表中(添加分区)
hive --database zhiyoubao -e "alter table ORDER_INFO add partition (years=$yy,months=$mm,days=$dd) location '/ZYB/ORDER_INFO/yy=$yy/mm=$mm/dd=$dd/';"
将基础表数据查询insert到目标表中
hive -e 'insert into table totallots.sd_everyday_order_info select substr(modify_time,0,10)date,sum(already_check_num)
checked_num ,sum(need_check_num)scheduled_num,sum(close_fee)everyday_gmv,sum(if(check_status="checked" or 
check_status="checking",close_fee,0))checked_fee,sum(return_fee)return_fee,count(id)order_num from zhiyoubao.order_info 
group by substr(modify_time,0,10) having substr(modify_time,0,10)="'${yy}-${mm}-${dd}'";'
增量导入到mysql
sqoop export --connect "jdbc:mysql://192.168.0.197:3306/dac_lots?useUnicode=true&characterEncoding=utf-8"  --username root 
--password root --table sd_everyday_order_info   --export-dir '/user/hive/warehouse/totallots.db/sd_everyday_order_info' 
--input-null-string "\\\\N" --input-null-non-string "\\\\N" --input-fields-terminated-by "\001" --input-lines-terminated-by 
"\\n" -m 1 --update-mode allowinsert --update-key statis_date

--------------

如果UPDATE语句修改没有行,这不被认为是错误; 导出将静默继续。 (实际上,这意味着基于更新的导出不会将新行插入数据库。)同样,如果使用--update-key指定的列不能唯一标识行,并且单个语句更新多个行,则 条件也未检测到。
参数--update-key也可以给出逗号分隔的列名列表。 在这种情况下,Sqoop将在更新任何现有记录之前匹配此列表中的所有键。
根据目标数据库,如果要更新行(如果它们已存在于数据库中)或插入行(如果尚未存在),则也可以使用allowinsert模式指定--update-mode参数。

--------------

导出控制参数:
   --batch指示要在批处理模式下执行的基础语句
   --call <arg>使用此存储填充表
  过程(每行一个调用)
   --clear-staging-table表明可以删除登台表中的任何数据
   --columns <col,col,col ...>要导出到表的列
   --direct使用直接导出快速路径
   --export-dir <dir>导出的HDFS源路径
-m, - num-mappers <n>使用'n'个映射任务并行导出
   --mapreduce-job-name <name>设置生成的mapreduce作业的名称
   --staging-table <table-name>中间登台表
   --table <table-name>要填充的表
   --update-key <key>按指定的键列更新记录
   --update-mode <mode>指定当在数据库中找到具有非匹配键的新行时如何执行更新
   --validate使用配置的验证程序验证副本
   --validation-failurehandler <validation-failurehandler> ValidationFa ilureHandler的完全限定类名
   --validation-threshold <validation-threshold>完全限定类名
               HCatalog参数:
   --hcatalog-database <arg> HCatalog数据库名称
   --hcatalog-home <hdir>覆盖$ HCAT_HOME
   --hcatalog-partition-keys <partition-key>设置要使用的分区键
 导入到hive
   --hcatalog-partition-values <partition-value>设置要使用的分区值
 导入到hive
   --hcatalog-table <arg> HCatalog表名
   --hive-home <dir>覆盖$ HIVE_HOME
   --hive-partition-key <partition-key>设置导入hive时使用的分区键
   --hive-partition-value <partition-value>设置要使用的分区值
 导入到hive
   --map-column-hive <arg>覆盖特定列的映射
hive类型。

---------------------------------

#!/bin/bash
#get the yesterday date
yesterday=`date -d "-1 day" +"%Y_%m_%d"`
#define the HADOOP_HOME  and  HIVE_HOME
HADOOP_HOME=/opt/modules/hadoop-2.5.0
HIVE_HOME=/opt/modules/hive-0.13.1-bin
LOG_DIR=/opt/datas/logs
FILE=access_$yesterday.log
HDFS_DIR=/log/source/$yesterday
JAR_PATH=$LOG_DIR/logclean.jar
ENTRANCE=org.apache.hadoop.log.project.LogClean
OUTPUT_DIR=/log/clean/date=$yesterday
HIVE_DB=log_case
HIVE_TB=use_tb
SQOOP_HOME=/opt/cdh-5.3.6/sqoop-1.4.5-cdh5.3.6
########################################
#  load the data into hdfs             #
########################################
#show the yesterday date
echo "[**yesterday is $yesterday**]"
#create the hdfs_path
$HADOOP_HOME/bin/hdfs dfs -rm -r $HDFS_DIR >/dev/null 2>&1
$HADOOP_HOME/bin/hdfs dfs -rm -r $OUTPUT_DIR >/dev/null 2>&1
$HADOOP_HOME/bin/hdfs dfs -mkdir $HDFS_DIR
#put the date to hdfs 
$HADOOP_HOME/bin/hdfs dfs -put $LOG_DIR/$FILE $HDFS_DIR
echo "[**the file $FILE is put to $HDFS_DIR**]"
########################################
#  clean the source data               #
########################################
$HADOOP_HOME/bin/yarn jar $JAR_PATH $ENTRANCE $HDFS_DIR $OUTPUT_DIR
echo "[**the file $FILE is cleaned**]"
######################################## 
#  load the cleaned data to hive       #
########################################
$HIVE_HOME/bin/hive -e "create database if not exists $HIVE_DB"
$HIVE_HOME/bin/hive -e "create external table if not exists $HIVE_DB.$HIVE_TB(ip string,time string,url string) partitioned by (date string) row format delimited fields terminated by '\t' location '/log/clean'"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "alter table $HIVE_TB drop partition (date='$yesterday')"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "alter table $HIVE_TB add partition (date='$yesterday')"
echo "[**add a partition $yesterday to $HIVE_DB.$HIVE_TB**]"
########################################
#  analysis the date using hive        #
########################################
##PV
echo "-------------------------pv start---------------------------------------"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "CREATE TABLE if not exists pv_tb(pv string) row format delimited fields terminated by '\t'"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "insert overwrite table pv_tb SELECT COUNT(1) FROM $HIVE_TB WHERE date='$yesterday'"
echo "-------------------------pv finished------------------------------------"
##register
echo "-------------------------rg start---------------------------------------"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "create table if not exists register_tb(rg string) row format delimited fields terminated by '\t'"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "insert overwrite table register_tb select count(1) from $HIVE_TB where date='$yesterday' and instr(url,'member.php?mod=register')>0"
echo "-------------------------rg finished------------------------------------"
##ip
echo "-------------------------ip start---------------------------------------"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "CREATE TABLE if not exists ip_tb(ip string) row format delimited fields terminated by '\t'"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "insert overwrite table ip_tb select count(distinct ip) from $HIVE_TB where date='$yesterday'"
echo "-------------------------ip finished------------------------------------"
##jump
echo "-------------------------jp start---------------------------------------"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "CREATE TABLE if not exists jump_tb(jp string) row format delimited fields terminated by '\t'"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "insert overwrite table jump_tb select count(1) from (select count(ip) ip_single from $HIVE_TB where date='$yesterday' group by ip having ip_single=1) jump"
echo "-------------------------jp finished------------------------------------"
##result
echo "**************************create the result table************************"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "create table if not exists result(day string,pv string,register string ,ip string ,jump string) row format delimited fields terminated by '\t'"
$HIVE_HOME/bin/hive --database $HIVE_DB -e "insert overwrite table result select '$yesterday',a.pv,b.rg,c.ip,d.jp from pv_tb a join register_tb b on 1=1 join ip_tb c on 1=1 join jump_tb d on 1=1"
##export to mysql
$SQOOP_HOME/bin/sqoop --options-file /opt/datas/logs/sqoop.file
















































1 0