5、Sqoop安装配置

rmanager上操作

# tar -zxfsqoop-1.4.5.bin__hadoop-2.5.0.tar.gz -C /var/data/

# cd /var/data/; mvsqoop-1.4.5 sqoop  ##这里的sqoop是编译后的版本

# mvsqoop/conf/sqoop-site-template.xml sqoop/conf/sqoop-site.xml

# cp /opt/mysql-connector-java-5.1.18-bin.jarsqoop/lib/

# vim /etc/profile

#sqoop path

exportSQOOP_HOME=/var/data/sqoop

exportPATH=$PATH:$SQOOP_HOME/bin

# source /etc/profile

 

Sqoop 常用命令操作:

  1. 1.   列出mysql数据库中的所有数据库命令

# sqoop list-databases --connect jdbc:mysql://10.40.214.9:3306/--username hive --password hive

  1. 2.   连接mysql并列出数据库中的表命令

# sqoop list-tables--connect jdbc:mysql://10.40.214.9:3306/hive --username hive --password hive

  1. 3.   将关系型数据的表结构复制到hive

# sqoop create-hive-table --connectjdbc:mysql://10.40.214.9:3306/zhangdh --table tb1 --username hive --passwordhive --hive-table test

  1. 4.  从关系数据库导入文件到hive中

# sqoopimport --connect jdbc:mysql://10.40.214.9:3306/zhangdh --username hive--password hive --table tb1 --hive-import

  1. hive中的表数据导入到mysql

# sqoop export--connect jdbc:mysql://10.40.214.9:3306/emails --username hive --password hive--table izhenxin --export-dir/user/hive/warehouse/maillog.db/izhenxin_total/000000_0--input-fields-terminated-by '\t'

6. 把数据从mysql导入到hdfs(默认是/user/<username>)

 # sqoop import --connectjdbc:mysql://datanode5:3306/hive --username hive --password hive --table TBLS --fields-terminated-by'\t'  --null-string '**'  -m 1 --append --hive-import

 # sqoop import --connectjdbc:mysql://datanode5:3306/hive --username hive --password hive --table TBLS --fields-terminated-by'\t'  --null-string '**'  -m 1 --append --hive-import  --check-column'TBL_ID' --incremental append --last-value 6

 

7. 把数据从hdfs导出到mysql 

 # sqoop export --connectjdbc:mysql://datanode5:3306/hive  --usernamehive --password hive --table ids --fields-terminated-by '\t' --export-dir'/ids'

 

8. 设置为作业,运行作业

 # sqoop job --create myjob -- import --connectjdbc:mysql://datanode5:3306/hive --username hive --password hive --table TBLS --fields-terminated-by'\t'  --null-string '**'  -m 1 --append --hive-import