sqoop使用

本文介绍了如何使用Sqoop将MySQL的数据同步到Hive,并实现增量导入。首先创建了MySQL和Hive的表结构,然后通过 Sqoop 导入原始数据。当尝试增量导入时遇到问题,但通过指定正确的参数实现了直接写入HDFS的增量数据导入。此外,还展示了从Hive导出数据到MySQL的示例,注意在导出时不能使用--direct选项,因为会导致权限错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.增量添加数据测试:

创建mysql表
CREATE TABLE sqoop_test (
id int(11) DEFAULT NULL,
name varchar(255) DEFAULT NULL,
age int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

创建hive表(表结构与mysql一致)
hive> create external table sqoop_test (id int,name string,age int)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ‘,’
STORED AS TEXTFILE
location ‘/user/hive/external/sqoop_test’;

添加mysql数据
insert into sqoop_test values(1,‘test1’,20),(2,‘test2’,25);

先导入mysql中的原始数据
sqoop import --connect jdbc:mysql://emr-header-1:3306/sqooptest --username root --password EMRroot1234 --table sqoop_test --hive-import --hive-overwrite --hive-table sqoop_test --fields-terminated-by ‘,’ -m 1

导入成功,查看hive表中的数据
hive> select * from sqoop_test;

在mysql中添加几条增量数据
insert into sqoop_test values(1,‘test3’,26),(2,‘test4’,28);
一开始使用一下增量方式:(不成功)
sqoop import --connect jdbc:mysql://emr-header-1:3306/sqooptest --username root --password EMRroot1234 --table sqoop_test --hive-import --hive-table sqoop_test --check-column id --incremental append --last-value 3 -m 1

hive查询增量的数据未null
在这里插入图片描述
正确写法,直接写入到hdfs:
sqoop import --connect jdbc:mysql://emr-header-1:3306/test --username root --password EMRroot1234 --table sqoop_test --target-dir ‘/user/hive/external/sqoop_test’ --incremental append --check-column id --last-value 3 -m 1

2、hive3与mysql8之间的数据同步
import例子:
sqoop import --connect jdbc:mysql://rm-uf6rvcc7y5x7jhzf190130.mysql.rds.aliyuncs.com:3306/lsr --username linsirong --password VWfv8FRm! --table linsirong --columns “id,name” --num-mappers 1 --hive-import --fields-terminated-by “\t” --hive-overwrite --hive-table testdb.linsirong
export例子:
sqoop export --connect jdbc:mysql://rm-uf6rvcc7y5x7jhzf190130.mysql.rds.aliyuncs.com:3306/lsr --username xxx --password xxx --table linsirong --num-mappers 1 --export-dir /user/hive/warehouse/testdb.db/linsirong --fields-terminated-by ‘\t’ --columns id,name

注意:export不能使用–direct,否则会报错
2021-04-26 14:12:36,312 INFO [Thread-15] org.apache.sqoop.mapreduce.MySQLExportMapper: mysqlimport: Error: 1227 Access denied;you need (at least one of) the SUPER, SYSTEM_VARIABLES_ADMIN or SESSION_VARIABLES_ADMIN privilege(s) for this operation
原因是:
https://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html#_mysql_direct_connector
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值