Sqoop 命令详解

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 test2 --export-dir /test


案例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


 

猜你喜欢

转载自www.cnblogs.com/Transkai/p/10556255.html
今日推荐