1. 程式人生 > 其它 >export引數 sqoop_Adventure電商分析案例第四篇《sqoop初步學習》

export引數 sqoop_Adventure電商分析案例第四篇《sqoop初步學習》

技術標籤:export引數 sqoop

此係列文章是《資料蛙三個月就業班》adventure電商分析案例的總結,如要了解此專案,參考adventure電商分析專案總結(2020版本)整個案例包括linux、shell、python、hive、pycharm、git、pyechart、sqoop等的使用,為了讓就業班同學能夠更好的學習,所以對上面大家有疑問的內容進行了總結。本篇是Adventure電商分析案例第一篇-sqoop初步學習

閱讀目錄:

  • sqoop介紹
  • sqoop從mysql抽取資料到hive
  • sqoop從hive抽取資料到mysql

一:sqoop介紹

Sqoop是一個用來將Hadoop和關係型資料庫中的資料相互轉移的工具,可以將一個關係型資料庫(例如 : MySQL ,Oracle ,Postgres等)中的資料導進到Hadoop的HDFS中,也可以將HDFS的資料導進到關係型資料庫中。匯入圖:

ef0fa769b6ea958ef074cffb4d672e98.png

二:sqoop從mysql抽取資料到hive

1.把mysql資料庫,adventure_ods.dim_date_df時間維度表中資料抽取到hive資料庫ods庫中

3b95b2903500dd2b4cd388b7c8b34875.png

2.全量抽取dim_date_df表中資料

sqoop import 
--hive-import 
--connect "jdbc:mysql://106.13.128.83:3306/adventure_ods?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&dontTrackOpenResources=true&defaultFetchSize=50000&useCursorFetch=true" 
--driver com.mysql.jdbc.Driver 
--username root   #mysql 使用者名稱
--password ***  #mysql密碼
--query 
"select * from dim_date_df where "'$CONDITIONS'" "  # dim_date_df維度表資料
--fetch-size 50000  #每次批量抽取5000條
--hive-table ods.dim_date_df   #hive 中的表名,抽取時會自動建表
--hive-drop-import-delims 
--delete-target-dir   #刪除hive 路徑
--target-dir /user/hadoop/sqoop/dim_date_df  # 建立hive 路徑
-m 1

3.按照條件抽取dim_date_df表中資料

sqoop import 
--hive-import 
--connect "jdbc:mysql://106.13.128.83:3306/adventure_ods?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&dontTrackOpenResources=true&defaultFetchSize=50000&useCursorFetch=true" 
--driver com.mysql.jdbc.Driver 
--username root   #mysql 使用者名稱
--password *** #mysql密碼
--query 
"select * from dim_date_df where "'$CONDITIONS'" and is_21_day=1"  # 抽取dim_date_df最近21天維度表資料
--fetch-size 50000  #每次批量抽取5000條
--hive-table ods.dim_date_df   #hive 中的表名,抽取時會自動建表
--hive-drop-import-delims 
--delete-target-dir   #刪除hive 路徑
--target-dir /user/hadoop/sqoop/dim_date_df  # 建立hive 路徑
-m 1

這裡主要使用了query引數,並加上where判斷條件來進行的,其中對於"'$CONDITIONS'"這個引數大家不用深究,剛開始學習的時候滿足我們抽資料的需求就好。

三:sqoop從hive抽取資料到mysql

先看下我們上面抽取到hive 資料庫中的資料

02a16fc268515afc619a8c49e129561e.png

下面我們來看下hive 資料庫中的資料如何抽取到mysql資料

1.mysql資料庫,adventure_dw庫建表

CREATE TABLE adventure_dw.dim_date_df 
(`create_date` text COLLATE utf8_unicode_ci,
`year` bigint(20) DEFAULT NULL,
`month` bigint(20) DEFAULT NULL,
`day` bigint(20) DEFAULT NULL,
`quarter` bigint(20) DEFAULT NULL,
`day_name` text COLLATE utf8_unicode_ci,
`week_of_year` bigint(20) DEFAULT NULL,
`day_of_year` bigint(20) DEFAULT NULL,
`days_in_month` bigint(20) DEFAULT NULL,
`day_of_week` bigint(20) DEFAULT NULL,
`is_current_year` bigint(20) DEFAULT NULL,
`is_last_year` bigint(20) DEFAULT NULL,
`is_yesterday` bigint(20) DEFAULT NULL,
`is_today` bigint(20) DEFAULT NULL,
`is_21_day` bigint(20) DEFAULT NULL,
`is_current_month` bigint(20) DEFAULT NULL,
`is_current_quarter` bigint(20) DEFAULT NULL)

2.開始進行hive資料庫到mysql資料庫抽取

sqoop export 
--connect "jdbc:mysql://106.13.128.83:3306/adventure_dw?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&dontTrackOpenResources=true&defaultfetchSize=50000&useCursorfetch=true" 
--username root  # 資料庫賬號
--password ***  #資料庫密碼
--table dim_date_df  #mysql資料庫建好的表
--export-dir /user/hive/warehouse/ods.db/dim_date_df  #hive資料庫資料路徑,這個用show create table ods.dim_date_df 查hive表的路徑
--columns create_date,year,month,day,quarter,day_name,week_of_year,day_of_year,days_in_month,day_of_week,is_current_year,is_last_year,is_yesterday,is_today,is_21_day,is_current_month,is_current_quarter  # 抽取的列
--fields-terminated-by '001' 

一般是寫到shell指令碼執行,在linux 系統中執行即可3.最後我們來看下儲存到mysql中的資料

338c6832036303a7cfe3753ecb2be949.png

四:課後學習資料

1.sqoop參考教程
2.sqoop更新、增量、全量匯出
3.sqoop引數