Sqoop学习笔记

  1. 什么是Sqoop?

Apache Sqoop是在Hadoop生态体系和RDBMS体系之间传送数据的一种工具。

Sqoop的工作机制是将导入导出命令转换为mapreduce名利。

2.sqoop安装

修改sqoop-env.xml:

修改一下三个位置 :
export HADOOP_COMMON_HOME= /export/servers/hadoop-2.7.5 
export HADOOP_MAPRED_HOME= /export/servers/hadoop-2.7.5
export HIVE_HOME= /export/servers/hive

3.全量导入

  • mysql导入hdfs
bin/sqoop import 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root 
--password hadoop 
--delete-target-dir 
--target-dir /sqoopresult 
--table emp --m 1

属性说明: 
import : 执行导入
--connect :   连接数据库地址
--username : 用户名
--password : 密码
--delete-target-dir :  如果--target-dir指定目录存在, 则先进行删除
--target-dir : 指定导出路径(hdfs)
--table  :  指定要导出的数据库的表名
--m   :  指定要执行几个map, 如果指定为 多个, 需要配合 --split-by 使用
--split-by :  用于指定根据那个字段进行划分
--fields-terminated-by : 指定导出后字段与字段之间的分隔号, 默认为 逗号
  •  mysql导入表结构到hive
bin/sqoop create-hive-table 
--connect jdbc:mysql://hadoop01:3306/userdb 
--table emp_add 
--username root 
--password 123456 
--hive-table hadoop32.emp_add_sp
属性说明 :
create-hive-table : 指定操作 : 创建hive的表
--hive-table :  导入到hive的那个表中(注意: 建议 库名.表名, 否则会将表放置到默认的数据库中)
需要先存在此数据库
  • mysql导入数据到hive
bin/sqoop import 
--connect jdbc:mysql://hadoop01:3306/sqoopdb 
--username root 
--password hadoop 
--table emp_add 
--hive-table test.emp_add_sp 
--hive-import 
--m 1
--hive-import : 标识为是hive的导入
  • 同时导入表结构和数据
bin/sqoop import 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root 
--password 123456 
--table emp_conn 
--hive-import 
--m 1 
--hive-database hadoop32;


注意:这时候不需要指定hive里面的表名,这和mysql中的表名一致
  • where过滤导入
bin/sqoop import 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root 
--password 123456 
--where "city ='sec-bad'" 
--target-dir /wherequery 
--table emp_add 
--m 1
  • 子查询导入
bin/sqoop import 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root 
--password 123456 
--target-dir /wherequery12 
--query 'select id,name,deg from emp WHERE id>1203 and $CONDITIONS' 
--split-by id 
--fields-terminated-by '	' 
--m 2

注意:
query条件必须有where条件
不用加--table参数因为在查询语句中已经指定了
where后面必须跟着¥Conditions
sql语句必须是单引号
  

 4.sqoop增量导入

  • append模式
bin/sqoop import 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root  --password 123456 
--table emp --m 1 
--target-dir /appendresult 
--incremental append 
--check-column id 
--last-value  1205


--incremental : 按照那种方式进行增量,  可选择为 append  和 lastmodified
--check-column : 根据那个列名来检测数据是否更新
--last-value : 上一次的值
  • lastmodified模式
bin/sqoop import 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root 
--password 123456 
--table customertest 
--target-dir /lastmodifiedresult 
--check-column last_mod 
--incremental lastmodified 
--last-value "2019-09-25 15:45:13" 
--m 1 
--append

这是因为采用lastmodified模式去处理增量时,会将大于等于last-value值的数据当做增量插入。但是不做合并
  •  modified合并
bin/sqoop import 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root 
--password 123456 
--table customertest 
--target-dir /lastmodifiedresult 
--check-column last_mod 
--incremental lastmodified 
--last-value "2019-09-25 15:45:48" 
--m 1 
--merge-key id

由于merge-key模式是进行了一次完整的mapreduce操作,
因此最终我们在lastmodifiedresult文件夹下可以看到生成的为part-r-00000这样的文件,会发现id=1的name已经得到修改,同时新增了id=6的数据

 5 .sqoop导出

  • 默认导出
bin/sqoop export 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root 
--password 123456 
--table employee 
--export-dir /emp/emp_data

--input-fields-terminated-by '	'  
	指定文件中的分隔符
--columns 
	选择列并控制它们的排序。当导出数据文件和目标表字段列顺序完全一致的时候可以不写。否则以逗号为间隔选择和排列各个列。没有被包含在–columns后面列名或字段要么具备默认值,要么就允许插入空值。否则数据库会拒绝接受sqoop导出的数据,导致Sqoop作业失败
--export-dir 
	导出目录,在执行导出的时候,必须指定这个参数,同时需要具备--table或--call参数两者之一,--table是指的导出数据库当中对应的表,
--call
	是指的某个存储过程。
--input-null-string --input-null-non-string
	如果没有指定第一个参数,对于字符串类型的列来说,“NULL”这个字符串就回被翻译成空值,如果没有使用第二个参数,无论是“NULL”字符串还是说空字符串也好,对于非字符串类型的字段来说,这两个类型的空串都会被翻译成空值。比如:
--input-null-string "\N" --input-null-non-string "\N" 
  • 更新导出(uploadonly)
bin/sqoop export 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root --password 123456 
--table updateonly 
--export-dir /updateonly_2 
--update-key id 
--update-mode updateonly

最终只更新了数据
  •  更新导出(allowinsert)
bin/sqoop export 
--connect jdbc:mysql://hadoop01:3306/userdb 
--username root --password hadoop 
--table allowinsert 
--export-dir /allowinsert_2/ 
--update-key id 
--update-mode allowinsert

数据进行更新操作的同时也进行了新增的操作


原文地址:https://www.cnblogs.com/qidi/p/11584812.html