Sqoop命令詳解
1、import命令
案例1:將mysql表test中的數據導入hive的hivetest表,hive的hivetest表不存在。
sqoop import --connect jdbc:mysql://hadoop-001:3306/test --username root --password 123456 --table my_user --hive-table hivetest --hive-import -m 1
案例2:在案例1的基礎上,分別進行overwrite(覆蓋)導入和into(直接加入)導入。
into: 命令同案例1
overwrite:
sqoop import --connect jdbc:mysql://hadoop-001:3306/test --username root --password 123456 --table test --hive-table hivetest --hive-import -m 1 --hive-overwrite
案例3:在案例2的基礎上,通過增加mysql的test表數據,增量導入到hive表中。
sqoop import --connect jdbc:mysql://hadoop-001:3306/test --username root --password 123456 --table test --where "id>9" --hive-table hivetest --hive-import -m 1
或者
sqoop import --connect jdbc:mysql://hadoop-001:3306/test --username root --password 123456 --table test --query "select id,name from test where id>9" --hive-table hivetest --hive-import -m 1
案例4:將test表中的數據導出到使用','分割字段的hive表(hivetest2)中。
創建表: create table hivetest2(id int,name string) row format delimited fields terminated by ',';
sqoop:
sqoop import --connect jdbc:mysql://hadoop-001:3306/test --username root --password 123456 --table test --hive-table hivetest2 --hive-import -m 1 --fields-terminated-by ","
案例5:將PARTITIONS表的數據導入到hdfs中。
sqoop import --connect jdbc:mysql://hadoop-001:3306/metastore --username root --password 123456 --table PARTITIONS --target-dir /wordcount -m 1

可以看到hdfs有生成文件

cat一下

案例6:在案例5的基礎上,增量導入數據到hdfs中。
sqoop import --connect jdbc:mysql://hadoop-001:3306/test --username root --password 123456 --table test --target-dir /test -m 1 --check-column id --incremental append --last-value 11
2、export命令
案例1:將hdfs上的文件導出到關系型數據庫test2表中。
./sqoop export --connect jdbc:mysql://hadoop-001:3306/test --username root --password 123456 --table tb_dept --export-dir /test/dept.txt

案例2:將hive表(hivetest)數據導出到關系型數據庫test2表中(使用insertOrUpdate方法導入)。
hivetest表只留id為1,2,3,4,5的數據,其他數據刪除。
hivetest表分隔方式是'\u0001',但是export命令默認使用','分隔數據
sqoop export --connect jdbc:mysql://hadoop-001:3306/test --username root --password hive --table test2 --export-dir /hive/hivetest --input-fields-terminated-by "\\01" --update-mode
allowinsert --update-key id
3.其他命令
list-databases命令
sqoop list-databases --connect jdbc:mysql://hadoop-001:3306 --username root --password 123456
list-tables
sqoop list-tables --connect jdbc:mysql://hadoop-001:3306/default --username root --password 123456
create-hive-table命令
sqoop create-hive-table --connect jdbc:mysql://hadoop-001:3306/test --username root --password 123456--table test --hive-table hivetest
help命令
1. sqoop help
2. sqoop help list-tables

