水善利萬(wàn)物而不爭(zhēng),處眾人之所惡,故幾于道??
目錄
? 1. 使用Sqoop腳本將MySQL中的數(shù)據(jù)遷移到HDFS
? 2. 在Hive中建立與之對(duì)應(yīng)的表
? 3. 將HDFS中的數(shù)據(jù)load到 Hive 數(shù)倉(cāng)的ODS層的表中
1 . 使用Sqoop 將 MySQL中的數(shù)據(jù)導(dǎo)入到HDFS上
#! /bin/bash
sqoop=/opt/module/sqoop/bin/sqoop
#do_date默認(rèn)為前一天的時(shí)間
do_date=`date -d '-1 day' +%F`
#如果第二個(gè)參數(shù)沒(méi)有指定導(dǎo)入哪天的數(shù)據(jù),默認(rèn)為前一天的數(shù)據(jù)
if [[ -n "$2" ]]; then
do_date=$2
fi
import_data(){
$sqoop import \
--connect jdbc:mysql://hadoop101:3306/gmall \
--username root \
--password 000000 \
#指定導(dǎo)出數(shù)據(jù)的目錄路徑
--target-dir /origin_data/gmall/db/$1/$do_date \
#如果該目錄已經(jīng)存在,則刪除該目錄。設(shè)定此參數(shù)可以保證每次導(dǎo)入數(shù)據(jù)不會(huì)覆蓋之前的數(shù)據(jù)。
--delete-target-dir \
#使用 SQL 查詢(xún)語(yǔ)句導(dǎo)入數(shù)據(jù),有的sql會(huì)加一個(gè)where 1=1是為了滿(mǎn)足語(yǔ)法
--query "$2 and \$CONDITIONS" \
#Sqoop 并行的任務(wù)數(shù),默認(rèn)值為 4。因?yàn)樗讓舆\(yùn)行的實(shí)際上是MR中的Map,沒(méi)有Reduce,默認(rèn)是4個(gè)MapTask。數(shù)據(jù)導(dǎo)入時(shí),建議并行度設(shè)為1
--num-mappers 1 \
--fields-terminated-by '\t' \
#啟用壓縮
--compress \
#設(shè)置壓縮算法-lzop壓縮
--compression-codec lzop \
#Hive中的Null在底層是以\N來(lái)存儲(chǔ)的,而MySQL中的NULL就是NULL,為了導(dǎo)入數(shù)據(jù)的一致性
--null-string '\\N' \
--null-non-string '\\N'
#導(dǎo)入后立即建立lzo索引
hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/common/hadoop-lzo-0.4.20.jar com.hadoop.compression.lzo.DistributedLzoIndexer /origin_data/gmall/db/$1/$do_date
}
import_activity_order(){
import_data activity_order "select
id,
activity_id,
order_id,
create_time
from activity_order
where date_format(create_time,'%Y-%m-%d')='$do_date'"
}
import_base_region(){
import_data base_region "select
id,
region_name
from base_region
where 1=1"
}
case $1 in
"order_info")
import_order_info
;;
"base_category1")
import_base_category1
;;
# 導(dǎo)入指定的表,省略了,所有的表都應(yīng)該列出來(lái)
"first")
import_base_category1
import_base_category2
import_base_category3
import_order_info
#......
#所有的表,因?yàn)榈谝淮螌?dǎo)入為全量導(dǎo)入
;;
"all")
import_comment_info
import_coupon_use
#以后每次是增量導(dǎo)入,有些表就不用導(dǎo)入了
;;
esac
- 使用示例:
mysql_to_hdfs.sh all 2021-02-01
- 導(dǎo)出的數(shù)據(jù)用lzo壓縮,并且在導(dǎo)出每一張表后,都立即生成lzo索引文件,因?yàn)閘zo文件的切片依賴(lài)其索引文件,存放在指定的路徑下
2. 在Hive中建立與之對(duì)應(yīng)的表
常用的數(shù)據(jù)類(lèi)型有下面這幾個(gè):
?string - - - 字符型
?bigint - - - 數(shù)值類(lèi)型
?decimal(10,2) - - - 商品的金額
?decimal(16,2) - - - 支付、退款金額
??數(shù)倉(cāng)中一般創(chuàng)建的都是外部表,防止數(shù)據(jù)被誤刪(因?yàn)檫@個(gè)表的數(shù)據(jù)實(shí)際上是存儲(chǔ)在HDFS上,并不屬于Hive的數(shù)據(jù)集,所以當(dāng)我們刪除這個(gè)外部表的時(shí)候,只會(huì)刪除它在Hive元數(shù)據(jù)中的記錄,而不會(huì)刪除HDFS上的數(shù)據(jù)文件,因此比較安全)
drop table if exists You_HiveTable_Name;
CREATE EXTERNAL TABLE You_HiveTable_Name (
`field_name1` string,
`field_name2` bigint,
`field_name3` decimal(10,2)
)
PARTITIONED BY (`dt` string) --分區(qū)字段
row format delimited fields terminated by '\t' --指定列分割符
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION '/warehouse/gmall/ods/ods_start_log'; --外部表的存儲(chǔ)路徑,一建表這個(gè)路徑就會(huì)被創(chuàng)建
??以上SQL是創(chuàng)建一個(gè)外部表,支持lzo壓縮,也就是聲明這個(gè)表要讀取的是lzo文件,比如我進(jìn)行一個(gè)查詢(xún),(如果是MapReduce)實(shí)際上底層是通過(guò)MR去讀數(shù)據(jù),然后將結(jié)果輸出,MR讀數(shù)據(jù)會(huì)用到FileInputFormat,那么用LzoTextInputFormat就可以讀到數(shù)據(jù)了。
??Hive - Lzo壓縮的詳細(xì)介紹及配置 - Hive官網(wǎng)
3. 將HDFS中的數(shù)據(jù) load 到 Hive 數(shù)倉(cāng)的ODS層的表中
#!/bin/bash
APP=gmall
hive=/opt/module/hive/bin/hive
# 如果是輸入的日期按照取輸入日期;如果沒(méi)輸入日期取當(dāng)前時(shí)間的前一天
if [ -n "$2" ] ;then
do_date=$2
else
do_date=`date -d "-1 day" +%F`
fi
sql1="
load data inpath '/origin_data/$APP/db/order_info/$do_date' OVERWRITE into table ${APP}.ods_order_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/order_detail/$do_date' OVERWRITE into table ${APP}.ods_order_detail partition(dt='$do_date');
"
#每張表都要load,這里省略了
sql2="
load data inpath '/origin_data/$APP/db/base_province/$do_date' OVERWRITE into table ${APP}.ods_base_province;
load data inpath '/origin_data/$APP/db/base_region/$do_date' OVERWRITE into table ${APP}.ods_base_region;
"
case $1 in
"first"){
$hive -e "$sql1$sql2"
};;
"all"){
$hive -e "$sql1"
};;
esac
??這里的兩個(gè)sql字符串的意思是:有的表只需要在第一次導(dǎo)的時(shí)候?qū)耄瑢?dǎo)入后基本不改變,所以以后就不用導(dǎo)入,所以分開(kāi)了。
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-677577.html
到Hive中查看表數(shù)據(jù):文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-677577.html
至此,業(yè)務(wù)數(shù)據(jù)庫(kù)中的數(shù)據(jù)已經(jīng)從MySQL導(dǎo)入到了Hive中
到了這里,關(guān)于MySQL中的業(yè)務(wù)數(shù)據(jù)該如何正確導(dǎo)入到Hive中 - Sqoop的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!