sqoop数据导入导出工具

Sqoop:
    关系型数据库(mysql/oracle)  <======>  hadoop(hdfs/hive/hbase)


    关系型数据库向hadoop组件进行数据导入导出的工具

    底层使用MR技术,适用于大数据迁移

    将sqoop命令编译成Mr作业,提交给hadoop

    mysql ===> hadoop    导入
    hadoop ==> mysql    导出


    sqoop2不适用于生产部署


sqoop1.4.7==================================
    准备:启动hadoop(hdfs && yarn)和mysql
        
    1、解压
        tar -xzvf
    2、符号链接
        ln -s
    3、环境变量
        sudo nano /etc/profile
    4、环境变量生效
        source /etc/profile
    5、sqoop version

    6、将mysql驱动放置在sqoop的lib目录/soft/sqoop/lib
        mysql 5.5 ==> mysql-connector-java-5.1.17
        mysql 5.6 ==> mysql-connector-java-5.1.41


    7、在mysql中准备两张表,orders/customers

orders                                  customers
+-----+---------+-------+------+    +-----+----------+------+     
| oid | orderno | price | cid  |    | id  | name     | age  | 
+-----+---------+-------+------+    +-----+----------+------+ 
|   1 | no001   |  12.3 |    7 |    |   1 | jerry    | NULL | 
|   2 | no002   |  18.8 |    4 |    |   2 | tomas    | NULL | 
|   3 | no003   |    20 |    3 |    |   3 | tomason  | NULL | 
|   4 | no004   |    50 |    7 |    |   4 | tomson   | NULL | 
|   5 | no005   |  23.1 |    2 |    |   5 | tomasLee | NULL | 
|   6 | no006   |    39 |    3 |    |   6 | tom6     | NULL | 
|   7 | no007   |     5 |    2 |    |   7 | tom7     | NULL | 
|   8 | no008   |     6 |    1 |    |   8 | tom8     | NULL | 
|   9 | no009   |     6 |   11 |    |  10 | jerry    | NULL | 
|  11 | no010   |   999 |    8 |    |  11 | jerry011 |    0 | 
|  12 | no001   |   100 |    8 |    | 111 | 222      | NULL | 
|  13 | no011   |    11 |   11 |    | 123 | 44444    | NULL | 
+-----+---------+-------+------+    +-----+----------+------+ 

    8、修改配置文件
        1)将/soft/sqoop/conf/sqoop-env-template.sh
            将其重命名为sqoop-env.sh

        2)修改sqoop-env.sh,添加如下配置
            #Set path to where bin/hadoop is available
            export HADOOP_COMMON_HOME=/soft/hadoop

            #Set path to where hadoop-*-core.jar is available
            export HADOOP_MAPRED_HOME=/soft/hadoop

            #set the path to where bin/hbase is available
            export HBASE_HOME=/soft/hbase

            #Set the path to where bin/hive is available
            export HIVE_HOME=/soft/hive

            #Set the path for where zookeper config dir is
            export ZOOCFGDIR=/soft/zk/conf
    

    
注意事项:
    sqoop可以连接windows的mysql =====> URL地址需要写为VMnet8的网卡地址eg:192.168.xx.1
                       关闭windows防火墙,以免3306端口被屏蔽

         可以连接centos的mysql  =====> URL地址写为s101地址
                            防火墙已关闭

    mysql 5.5 ==> mysql-connector-java-5.1.17
    mysql 5.6 ==> mysql-connector-java-5.1.41


    权限拒绝问题:
        grant all PRIVILEGES on *.* to root@'s201'  identified by 'root';

        解决:在mysql给主机授予权限
            grant all PRIVILEGES on *.* to root@''  identified by 'root';


sqoop命令:
================================
    sqoop    import
        export
        job                     
        list-databases       //list-databases
        list-tables      //list-tables

    命令查看帮助
        sqoop list-databases --help       //sqoop list-databases --help

常用sqoop使用方法:
    1、将sqoop命令做成脚本
    2、将sqoop命令做成job,以便下次使用


1、sqoop列出mysql数据库
    sqoop list-databases --connect jdbc:mysql://192.168.136.1:3306 --username root --password root

2、sqoop列出数据库中的表
    sqoop list-tables --connect jdbc:mysql://192.168.136.1:3306/day21 --username root --password root
        
3、将mysql数据导入到hdfs
    sqoop import --connect jdbc:mysql://192.168.136.1:3306/day21 --username root -P --table orders --target-dir /user/centos/orders -m 2
       
3.1、将mysql数据导入到hdfs-指定字段分隔符
    sqoop import --connect jdbc:mysql://192.168.136.1:3306/day21 --username root -P --table orders --target-dir /user/centos/orders -m 2 --fields-terminated-by '	' --delete-target-dir

    
3.2、指定对应列导入到hdfs
    sqoop import --connect jdbc:mysql://192.168.136.1:3306/day21 --username root -P --table orders --target-dir /user/centos/orders -m 2 --fields-terminated-by '	' --delete-target-dir --columns oid,orderno,price
    


注意:数据导入到hive,需要将hive-site.xml文件拷贝到sqoop/conf目录

4、将mysql数据导入到hive表
    1)创建hive表
    sqoop import --create-hive-table --connect jdbc:mysql://192.168.136.1:3306/day21 --username root -P --table orders --hive-table orders  -m 2 --fields-terminated-by '	' 

    2)导入数据


    sqoop import --hive-import --connect jdbc:mysql://192.168.136.1:3306/day21 --username root -P --table orders --hive-table orders  -m 2 --fields-terminated-by '	' --delete-target-dir
    
        
        

    导入时可以在当前目录查看生成的代码

    sqoop import --help
        --columns <col,col,col...>    //MySQL列指定

        --table <table-name>        //指定表名
                   
                   
        --compression-codec <codec>    //压缩编解码器
                       
                       
        --delete-target-dir        //删除目标目录,以免出现文件已存在异常

        -m                //指定mapper数量
                      
        --target-dir <dir>        //指定hdfs导入路径 

        -P                //隐式指定密码,需要重新输入

        --fields-terminated-by        //指定字段分隔符,默认","

         --create-hive-table        //创建hive表                 
                                          
         --external-table-dir <hdfs path> //外部表路径        
                              
         --hive-database <database-name>  //hive数据库         
                              
         --hive-delims-replacement <arg>  //行分隔符                  
                              
         --hive-drop-import-delims                
                              
                                                      
         --hive-import              //导入到hive表是指定的参数                   
                              
                              
                              
         --hive-overwrite                         
                              
         --hive-partition-key <partition-key>        //指定hive分区字段    
                              
         --hive-partition-value <partition-value>    //指定hive分区值
                              
         --hive-table <table-name>    //指定hive导入表              
                              
         --map-column-hive <arg>        //hive列映射
                                          
原文地址:https://www.cnblogs.com/zyde/p/8945924.html