在sqoop中,「匯入」概念指:從非大資料集群(rdbms)向大資料集群(hdfs,hive,hbase)中傳輸資料,叫做:匯入,即使用import關鍵字。
1) 確定mysql服務開啟正常
2) 在mysql中新建一張表並插入一些資料
$ mysql -uroot -p000000
mysql> create database company;
mysql> create table company.staff(id int(4) primary key not null auto_increment, name varchar(255), *** varchar(255));
mysql> insert into company.staff(name, ***) values('thomas', 'male');
mysql> insert into company.staff(name, ***) values('catalina', 'female');
3) 匯入資料
(1)全部匯入
$ bin/sqoop import \
--connect jdbc:mysql://hadoop104:3306/company \
--username root \
--password 000000 \
--table staff \
--target-dir /user/company \
--delete-target-dir \
--fields-terminated-by "\t"
(2)查詢匯入
$ bin/sqoop import \
--connect jdbc:mysql://hadoop104:3306/company \
--username root \
--password 000000 \
--target-dir /user/company \
--delete-target-dir \
--fields-terminated-by "\t" \
--query 'select name,*** from staff where id <=3 and $conditions;'
(3)匯入指定列
$ bin/sqoop import \
--connect jdbc:mysql://hadoop104:3306/company \
--username root \
--password 000000 \
--target-dir /user/company \
--delete-target-dir \
--fields-terminated-by "\t" \
--columns id,*** \
--table staff
columns
中如果涉及到多列,用逗號分隔,分隔時不要新增空格
(4)使用sqoop關鍵字篩選查詢匯入資料
$ bin/sqoop import \
--connect jdbc:mysql://hadoop104:3306/company \
--username root \
--password 000000 \
--target-dir /user/company \
--delete-target-dir \
--fields-terminated-by "\t" \
--table staff \
--where "id=2"
$ bin/sqoop import \
--connect jdbc:mysql://hadoop104:3306/company \
--username root \
--password 000000 \
--table staff \
--hive-import \
--fields-terminated-by "\t" \
--hive-overwrite \
--hive-table staff_hive
在sqoop中,「匯出」概念指:從大資料集群(hdfs,hive,hbase)向非大資料集群(rdbms)中傳輸資料,叫做:匯出,即使用export關鍵字。
$ bin/sqoop export \
--connect jdbc:mysql://hadoop104:3306/company \
--username root \
--password 000000 \
--export-dir /user/hive/warehouse/staff_hive \
--table staff \
--input-fields-terminated-by "\t"
思考:資料是覆蓋還是追加
使用opt格式的檔案打包sqoop命令,然後執行
1) 建立乙個.opt檔案
$ mkdir opt
$ touch opt/job_hdfs2rdbms.opt
2) 編寫sqoop指令碼
$ vi opt/job_hdfs2rdbms.opt
export
--connect
jdbc:mysql://hadoop104:3306/company
--username
root
--password
--table
staff
--export-dir
/user/hive/warehouse/staff_hive
--input-fields-terminated-by
"\t"
3) 執行該指令碼
$ bin/sqoop --options-file opt/job_hdfs2rdbms.opt
Sqoop語句簡單案例應用
sqoop 中文手冊 1 列出mysql資料庫中的所有資料庫 bin sqoop list databases connect jdbc mysql vampire04 3306 username root password 123456 2 mysql hdfs 連線mysql 資料匯出到 hdf...
Sqoop (二)Sqoop 的簡單使用案例
二 匯出資料 三 指令碼打包 在sqoop中,匯入 概念指 從非大資料集群 rdbms 向大資料集群 hdfs,hive,hbase 中傳輸資料,叫做 匯入,即使用import關鍵字。確定mysql服務開啟正常 在mysql中新建一張表並插入一些資料 mysql uroot p000000 mysq...
Sqoop 的簡單使用案例
1 匯入資料 在 sqoop 中,匯入 概念指 從非大資料集群 rdbms 向大資料集群 hdfs,hive,hbase 中傳輸資料,叫做 匯入,即使用 import 關鍵字。1 rdbms 到 hdfs 1 確定 mysql 服務開啟正常 2 在 mysql 中新建一張表並插入一些資料 mysql...