利用sqoop完成数据导入导出

一.安装SQOOP后可使用如下命令列出mysql数据库中的所有数据库,与检验是否安装成功。
     #  sqoop list-databases --connect jdbc:mysql://localhost:3306/ --username root --password 123456

二. hive与mysql数据互导

    一. 使用命令范例:

     sqoop import --connect jdbc:mysql://192.168.137.1:3306/test
     --username root --password 123456 --table mytabs --fields-terminated-by
     '\t' -m 1 [--hive-import] --append --check-column 'id'  --incremental
     append --last-value 5 --where 'id>2 and id<5'

        参数详解:

       1.--fields-terminated-by '\t',表示将数据导入到hadoop中列记录之间的间隔符,默认符号为英文逗号。这里通常使用制表符\t来间隔数据,避免数据再次从HDFS到入到关系数据库时引起分割混乱
        2.-m 1,是--num-mappers的缩写,表示指定MapReduce的个数为1个(默认会自动开启多个),sqoop转化的MR程 序不包含reduce
        3.--append,表示数据导入到hadoop的方式为追加,否则不允许重复导入
        4.--check-column '主键列名' --incremental append --last-value 5,表示数据为增量导入,根据--last-value的值来判断,有大于这个值的记录则执行导入,否则不执行导入操作
        5.表示数据为增量导入,根据--last-value的值来判断, 有大于这个值的记录则执行导入,否则不执行导入操作
        6.--hive-import,表示将数据导入到Hive中;
        7.--where '',数据筛选条件
        8.-e 或--query 'select * from table where id>5 and $CONDITIONS',自定义导入数据的sql语句。使用自定义sql语句 需要注意:
           ① 使用了自定义sql就不能指定--table;
           ② 自定义sql语句的where条件中必须包含字符串"$CONDITIONS",$CONDITIONS是一个变量,用于给多个map任务划分任务范 围;
           ③ 使用自定义sql时,如果通过参数-m指定多个map任务,由于自定义sql中可能存在多表查询,因此必须使用参数“--split-by 表名.字段名”指定多个map任务分割数据的根据字段,如--split-by users.id;
        9. --target-dir,显示指定数据导入到HDFS中的位置,默认保存路径为:/user/{当前用户}/{表名}/表数据文件,
           如果导入时需要将已存在的HDFS文件删除,可使用--delete-target-dir

    二. hive数据导入导出数据到mysql

     sqoop export -connect jdbc:mysql://localhost:3306/sqoop
     -username root -password hadoop -table hive_student -export-dir
     /hive/student/student --input-fields-terminated-by '\t'
    三.mysql数据库数据导入hive中
     sqoop import --connect jdbc:mysql://localhost:3306/sqoop
     -username root -password hadoop -table test -hive-import -m 1
三.hbase与关系型数据库数据互导
    从Mysql导入到Hbase中
    参数说明:
    test 为mysql中要传入到hbase表中的表名。
    mysql_sqoop_test 传入hbase中的表名
     --column-family hbase表中的列族
    --hbase-row-key 在hbase中那一列作为rowkey
    使用范例:
   sqoop import --connect jdbc:mysql://10.120.10.11:3306/sqoop
   --username sqoop --password sqoop --table test --hbase-create-table
   --hbase-table mysql_sqoop_test --column-family info --hbase-row-key id -m 1


你可能感兴趣的:(SQOOP)