用sqoop將mysql的資料匯入到hive表中
阿新 • • 發佈:2019-01-08
用sqoop將mysql的資料匯入到hive表中
1:先將mysql一張表的資料用sqoop匯入到hdfs中
準備一張表
需求 將 bbs_product 表中的前100條資料導 匯出來 只要id brand_id和 name 這3個欄位
資料存在 hdfs 目錄 /user/xuyou/sqoop/imp_bbs_product_sannpy_ 下
bin/sqoop import \ --connect jdbc:mysql://172.16.71.27:3306/babasport \ --username root \ --password root \ --query 'select id, brand_id,name from bbs_product where $CONDITIONS LIMIT 100' \ --target-dir /user/xuyou/sqoop/imp_bbs_product_sannpy_ \ --delete-target-dir \ --num-mappers 1 \ --compress \ --compression-codec org.apache.hadoop.io.compress.SnappyCodec \ --fields-terminated-by '\t'
ps: 如果匯出的資料庫是mysql 則可以新增一個 屬性 --direct
1 bin/sqoop import \ 2 --connect jdbc:mysql://172.16.71.27:3306/babasport \ 3 --username root \ 4 --password root \ 5 --query 'select id, brand_id,name from bbs_product where $CONDITIONS LIMIT 100' \ 6 --target-dir /user/xuyou/sqoop/imp_bbs_product_sannpy_ \ 7 --delete-target-dir \ 8 --num-mappers 1 \ 9 --compress \ 10 --compression-codec org.apache.hadoop.io.compress.SnappyCodec \ 11 --direct \ 12 --fields-terminated-by '\t'
加了 direct 屬性在匯出mysql資料庫表中的資料會快一點 執行的是mysq自帶的匯出功能
第一次執行所需要的時間
第二次執行所需要的時間 (加了direct屬性)
執行成功
2:啟動hive 在hive中建立一張表
1 drop table if exists default.hive_bbs_product_snappy ; 2 create table default.hive_bbs_product_snappy( 3 id int, 4 brand_id int, 5 name string 6 ) 7 ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;
3:將hdfs中的資料匯入到hive中
1 load data inpath '/user/xuyou/sqoop/imp_bbs_product_sannpy_' into table default.hive_bbs_product_snappy ;
4:查詢 hive_bbs_product_snappy 表
1 select * from hive_bbs_product_snappy;
此時hdfs 中原資料沒有了
然後進入hive的hdfs儲存位置發現
注意 :sqoop 提供了 直接將mysql資料 匯入 hive的 功能 底層 步驟就是以上步驟
建立一個檔案 touch test.sql 編輯檔案 vi test.sql
1 use default; 2 drop table if exists default.hive_bbs_product_snappy ; 3 create table default.hive_bbs_product_snappy( 4 id int, 5 brand_id int, 6 name string 7 ) 8 ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;
在 啟動hive的時候 執行 sql指令碼
bin/hive -f /opt/cdh-5.3.6/sqoop-1.4.5-cdh5.3.6/test.sql
執行sqoop直接匯入hive的功能
1 bin/sqoop import \ 2 --connect jdbc:mysql://172.16.71.27:3306/babasport \ 3 --username root \ 4 --password root \ 5 --table bbs_product \ 6 --fields-terminated-by '\t' \ 7 --delete-target-dir \ 8 --num-mappers 1 \ 9 --hive-import \ 10 --hive-database default \ 11 --hive-table hive_bbs_product_snappy
看日誌輸出可以看出 在執行map任務之後 又執行了load data
查詢 hive 資料
分類: 大資料