我会不间断的更新,维护,希望可以对正在找大数据工作的朋友们有所帮助.
第十一章目录
第十一章 Sqoop
sqoop本质是一款使用MR进行数据迁移的工具。
目前业界普遍用1.4.6版本,该版本与CDH集成。
hive对外的一个统一存储格式的接口,使用hcatalog对接到hive的数据不需要担心数据在hive中的存储格式和存储位置。sqoop在1.4.4版本后集成了HCatalog
11.1 sqoop导入数据到hive
11.1.1 sqoop+hcatalog
#使用建表的方式增量导入hive orc;
#如果已经使用hcatalog,则hive-import系列的指定全都不能用,否则报错;
和--hcatalog-storage-stanza,存储格式和压缩格式会自动对齐hive表的配置
sqoop import
--connect jdbc:mysql://127.0.0.1:3306/test
--username your_user_name --password 123456
--table table_name --driver com.mysql.jdbc.Driver
#用sql语句只支持简单查询,使用子查询或连接查询可能查出错误结果;如果指定-split by,必须使用$CONDITIONS关键字,双引号的查询语句还要加
--query "SELECT a.*, b.* FROM a JOIN b on (a.id == b.id) WHERE $CONDITIONS"
#hcatalog-storage-stanza 建表时追加存储格式到建表语句中,tblproperties修改表的属性,这里设置orc的压缩格式为gzip
#如果表已经存在,必须去掉create-hcatalog-table 和hcatalog-storage-stanza
--create-hcatalog-table
--hcatalog-storage-stanza 'stored as orc tblproperties ("orc.compress"="gzip")'
--hcatalog-database test
--hcatalog-table table_name
#keys和values必须同时存在,相当于静态分区;
--hcatalog-partition-keys month,day
--hcatalog-partition-values 12,09
#以自增长主键作增量导入
--incremental append
--check-column order_id
--last-value 5201314
#以时间字符串为条件增量导入
--incremental lastmodified
--check-column time
--last-value “2014-11-09 21:00:00”
#指定mysql数据为空值时用什么符号存储
--null-string 'N'
--null-not-string 'N'
#设置无视字符串中的分割符(hcatalog默认开启)
--hive-drop-import-delims
#-m不为1时,需要指定分片字段进行并行导入,尽量指定int型
--split-by key
-m 3
11.1.2 sqoop->hdfs->hive
不使用hcatalog和hive,导入orc表的方法(老业务可能使用)
1.先导入数据到hdfs
sqoop job --create bap_us_order -- import
--connect jdbc:mysql://master:3306/qfbap_ods?dontTrackOpenResources=true&defaultFetchSize=1000&useCursorFetch=true
--driver com.mysql.jdbc.Driver
--username root
--password-file hdfs:///password.file
--table us_order
--target-dir /qfbap/ods_tmp/ods_us_order/
--fields-terminated-by '001'
--check-column order_id
--incremental append
--last-value 0
2.load数据到临时表中
3.将临时表数据查出来插入到orc表中
insert into table ods_table partition(dt) select * from table
11.2 sqoop导出数据到mysql
sqoop export
--hcatalog-database temp
--hcatalog-table js_pianyuan_orc
--hcatalog-partition-keys curdate
--hcatalog-partition-values 20180122
--connect jdbc:mysql://ip:3306/test
--username username
--password passwd --m 10
--table js_pianyuan
#把''N视作空值
--input-null-string 'N'
--input-null-non-string 'N'
#以更新的方式导出数据(key不存在时允许新增)
--update-key id
--update-mode allowinsert
#导出时默认使用4个并行任务
#-m 1
#批量导出
--batch
#导出作业遵循最少一次语义,因此可能有数据重复,这里可以设置开启暂存临时表,临时表可清理。如果是更新作业就没必要用这个。
#--staging-table
#--clear-staging-table
更多sqoop相关:http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html#_sqoop_hcatalog_integration