Sqoop mysql与hdfs(hive)间传输数据

@羲凡——只为了更好的活着

Sqoop mysql与hdfs(hive)间传输数据

前期准备
a.将hive-site.xml 拷贝到 ${SQOOP_HOME}/conf 目录下
b.将 ${HIVE_HOME}/lib 所有jar包拷贝到 ${SQOOP_HOME}/lib下,否则报错连不上hive

0.参数说明
--connect #关系型数据库连接
--username #关系型数据库连接用户名
--password #关系型数据库连接密码
--table #关系型数据库的表
--split-by #如果-m的数量不为1,则一定要加上该参数且最好是数值类型,否则会报错
--direct #快速模式,使用mysql自带的mysqldump导出数据
--delete-target-dir #如果hdfs的目录已经存在则先删除
--target-dir #导入到hdfs时的目标目录
--export-dir #从hdfs导出时的源目录
--fields-terminated-by #导入到hdfs时的hdfs文件分隔符
--input-fields-terminated-by #从hdfs导出时的hdfs文件分隔符
--hive-overwrite #覆盖之前的数据
--hive-drop-import-delims #导入hive中的数据某列中如果有换行符或回车键可以删除
--hive-database #hive的数据库
--hive-table #hive的表
-m #指定map数量,也是生成的文件数
1.mysql数据导入到hdfs
#!/bin/bash
/usr/local/package/sqoop-1.4.7.bin/bin/sqoop import \
--connect "jdbc:mysql://deptest23:3306/test_data?useUnicode=true&characterEncoding=utf8" \
--username root \
--password password \
--table mysql_stu_info \
--split-by age \
--direct \
--delete-target-dir \
--target-dir /testdata/tmp/mysql2hdfs \
--fields-terminated-by '^A' \
-m 3
2.mysql数据导入到hive
#!/bin/bash
/usr/local/package/sqoop-1.4.7.bin/bin/sqoop import \
--connect "jdbc:mysql://deptest23:3306/test_data?useUnicode=true&characterEncoding=utf8" \
--username root \
--password password \
--table mysql_stu_info \
--split-by age \
--delete-target-dir \
--hive-delims-replacement , \
--hive-import \
--hive-overwrite \
--hive-database test_data \
--hive-table hive_stu_info \
--fields-terminated-by '^A' \
-m 3
3.hdfs(hive)数据导出到mysql
#!/bin/bash
/usr/local/package/sqoop-1.4.7.bin/bin/sqoop export \
--connect "jdbc:mysql://deptest23:3306/test_data?useUnicode=true&characterEncoding=utf8" \
--username root \
--password password \
--table mysql_stu_info \
--update-key name \
--update-mode allowinsert \
--export-dir /hive/warehouse/test_data.db/hive_stu_info/* \
--input-fields-terminated-by '^A' \
-m 3
4.注意事项

a.mysql数据导入到hivedelete-target-dir 参数是不会删除hive中已经有的数据
b.mysql数据导入到hive 中 如果mysql的某一列中有换行符或者回车键,必须加上 hive-drop-import-delims 参数
c.hdfs(hive)数据导出到mysql 时,如果mysql中有主键,一定要指定 update-key(后面跟需要更新的字段) 和 update-mode(必须为 allowinsert,默认为 updateonly) 。
d.hdfs数据导出到mysqlhive数据导出到mysql操作是一样的,只需要在 export-dir 写入对应的hdfs路径即可
e.在mysql向hive中导入数据的时候如果指定的map的数量不为1,则一定要加上–split-by参数且最好是数值类型

====================================================================

@羲凡——只为了更好的活着

若对博客中有任何问题,欢迎留言交流

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值