1:先將mysql一張表的數據用sqoop導入到hdfs中mysql
準備一張表 sql
需求 將 bbs_product 表中的前100條數據導 導出來 只要id brand_id和 name 這3個字段 數據庫
數據存在 hdfs 目錄 /user/xuyou/sqoop/imp_bbs_product_sannpy_ 下apache
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 app
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自帶的導出功能oop
第一次執行所須要的時間 post
第二次執行所須要的時間 (加了direct屬性)spa
執行成功3d
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 數據