Sqoop

官方参考文档:

http://sqoop.apache.org/docs/1.4.6/index.html

Sqoop介绍

Sqoop is a tool designed to transfer data between Hadoop and relational databases or mainframes. You can use Sqoop to import data from a relational database management system (RDBMS) such as MySQL or Oracle or a mainframe into the Hadoop Distributed File System (HDFS), transform the data in Hadoop MapReduce, and then export the data back into an RDBMS.

Sqoop 是数据转换的工具,在hadoop和关系型数据库之间进行传递:

import: 将关系型数据库的数据导入到hdfs中,可以为hdfs,hive,hbase;

export: 将hdfs(hdfs,hive)中的数据导入到关系型数据库当中;

帮助:

1 $sqoop help
2 $sqoop help import
3 $sqoop help export

import例子:

将数据从mysql中,导入到hdfs中,通过选项文件来执行操作;

1 sqoop --options-file options1.txt

options1.txt文件内容:

 1 #导入 从数据库到hdfs
 2 import
 3 
 4 #数据库连接
 5 --connect
 6 jdbc:mysql://node1:3306/test
 7 
 8 #数据库用户名密码
 9 --username
10 root
11 --password
12 root
13 
14 #字段
15 --columns
16 id,name,age
17 --where
18 1=1
19 
20 #表名
21 --table
22 t_persion
23 
24 #num of map task, 只执行一次,且导入按顺序,需要设置值为1
25 --num-mappers
26 1
27 
28 #hdfs中如果存在,则删除再导入
29 --delete-target-dir
30 
31 #目标目录
32 --target-dir
33 /sqoop/test1
34 
35 #sting类型字段,值为null, 则修改为
36 --null-string
37 ''
38 
39 #非string类型字段,值为null,则修改为
40 --null-non-string
41 ''

export例子:

从hive中导出数据到mysql中;

  1. 首先mysql中的表结构必须存在;
  2. 默认的hdfs中文件的字段分隔符是”,”;

常用的选项说明:

--update-key    更新的条件

--update-mode  updateonly(默认)  allowinsert

--input-fields-terminated-by "\01"  hive表存储的文件,默认的字段分隔符;

通过选项文件来执行:

 1 sqoop --options-file options4.txt --input-fields-terminated-by "\01" 

options4.txt的文件内容:

 1 export
 2 #连接数据库
 3 --connect 
 4 jdbc:mysql://node1:3306/test 
 5 --username 
 6 root 
 7 --password 
 8 root 
 9 
10 #数据库表
11 --table 
12 t_persion 
13 
14 #hive文件位置
15 --export-dir 
16 /user/hive/warehouse/t_persion
17 
18 #允许插入
19 --update-mode 
20 allowinsert 
21 
22 #更新字段的条件
23 --update-key 
24 id,name
原文地址:https://www.cnblogs.com/one--way/p/5640589.html