时间: 2020-09-03 00:08:26 人气: 2276 评论: 0
导语:本文为系列文章《基于OGG和Sqoop的TBDS接入方案系列》的第二篇,其余两篇文章的传送门如下:
第一篇:《基于OGG和Sqoop的TBDS接入方案系列-总体方案》
第三篇:《基于OGG和Sqoop的TBDS接入方案系列-数据合并方案》
本文主要讨论了如何利用Sqoop将Oracle源表接入到腾讯大数据套件TBDS中。
Oracle里存储的结构化数据导出到Hadoop体系做离线计算是一种常见数据处置手段,此案例介绍了一个利用Sqoop将数据从Oracle离线导入到腾讯大数据套件TBDS中Hadoop和Hive组件的场景。
离线数据的导入,主要包含全量和增量数据的导入。
本文中示例的环境配置如下表所示:
角色 | 数据存储服务及版本 | IP |
|---|---|---|
源服务器 | Oracle Release 11.2.0.1 | 10.0.0.85 |
Sqoop服务器 | Sqoop 1.4.5.2.2.0.0-2041 | 10.0.0.90 |
目标服务器 | TBDS 4.0.3.1 | 10.0.0.90 |
Sqoop可以非常便捷地以RPM包的形式直接集成到TBDS集群环境中,安装包可以从以下链接下载到:
链接:https://share.weiyun.com/5zgpbZi 密码:danme3
将上述安装包上传到TBDS集群的Portal节点上,例如目录/tmp下。
Sqoop本质上是一个客户端性质的工具,在集群中任意节点安装Sqoop均可。为方便起见,使用以下命令安装Sqoop到Portal节点上
rpm -ivh /tmp/sqoop_2_2_0_0_2041-1.4.5.2.2.0.0-2041.el6.noarch.rpm
把/etc/sqoop/conf/下的sqoop-env-templete.sh复制一份,命名为sqoop-env.sh,修改Hadoop、MR、Hive、HBase、ZooKeeper配置路径如下(export字样部分):
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # included in all the hadoop scripts with source command # should not be executable directly # also should not be passed any arguments, since we need original $* # Set Hadoop-specific environment variables here. #Set path to where bin/hadoop is available export HADOOP_COMMON_HOME=/usr/hdp/2.2.0.0-2041/hadoop #Set path to where hadoop-*-core.jar is available export HADOOP_MAPRED_HOME=/usr/hdp/2.2.0.0-2041/hadoop #set the path to where bin/hbase is available export HBASE_HOME=/usr/hdp/2.2.0.0-2041/hbase #Set the path to where bin/hive is available export HIVE_HOME=/usr/hdp/2.2.0.0-2041/hive #Set the path for where zookeper config dir is export ZOOCFGDIR=/usr/hdp/2.2.0.0-2041/zookeeper/conf
验证Sqoop是否安装成功:
sqoop version
Oracle源数据库所在节点IP为10.0.0.85,检查Oracle基本信息
su - oracle; sqlplus sys/sysoracle++@orcl as sysdba select name from v$database; show parameter db_name; select * from v$version;
到Oracle官方网站,下载对应版本的JDBC驱动
https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html
本例中使用的Oracle的版本为11.2.0.1.0,选择下载对应版本为11.2.0.4的JDBC驱动,
将OGG环境部署及数据导入示例-old文件上传到Sqoop节点,并移动到目录/usr/hdp/2.2.0.0-2041/sqoop/lib/下
为测试方便,在Oracle源端创建用户aron,赋予DBA权限,并通过aron用户登陆Oracle,创建一张测试表aron_test_tab
su - oracle; sqlplus sys/sysoracle++@orcl as sysdba create user aron identified by abcd1234; grant dba to aron; quit sqlplus #以Aron登陆 drop table aron_test_tab; create table aron_test_tab (id int, name varchar(20)); insert into aron_test_tab values (1, 'Aron'); insert into aron_test_tab values (2, 'Ben'); insert into aron_test_tab values (3, 'Cindy'); commit; select * from aron_test_tab;
若TBDS开启了认证,需要在导入数据之前,在Sqoop节点上先导出身份认证相关的环境变量,
export hadoop_security_authentication_tbds_secureid=<secureId> export hadoop_security_authentication_tbds_securekey=<secureKey> export hadoop_security_authentication_tbds_username=<userName>
其中<secureId>、<secureKey>、<userName>分别为使用Sqoop工具的用户的密钥ID、密钥值和用户名,例如hdfs用户,其对应的密钥ID和密钥值可以通过以该用户登录Portal页面 -> 点击右上方用户名 -> 个人中心 -> 密钥管理中找到,
在Sqoop节点,执行sqoop import命令导入表ARON_TEST_TAB的全量数据
sqoop import --connect jdbc:oracle:thin:@10.0.0.85:1521:ORCL --username aron --password abcd1234 --table ARON_TEST_TAB --target-dir /tmp/Aron -m 1
重要参数说明:
参数 | 说明 |
|---|---|
--query | SQL查询语句 |
--target-dir | HDFS目标目录(确保目录不存在,否则会报错,因为Sqoop在导入数据至HDFS时会自己在HDFS上创建目录) |
--null-string | string类型空值的替换符(Hive中Null用\n表示) |
--null-non-string | 非string类型空值的替换符 |
--split-by | 数据切片字段(int类型,m>1时必须指定) |
-m | Mapper任务数,默认为4 |
更多的参数,请参考Sqoop官方文档:
http://sqoop.apache.org/docs/1.4.5/SqoopUserGuide.html#_literal_sqoop_import_literal
若任务成功运行,其结束后可看到以下信息:
查看HDFS对应目录和文件内容
hdfs dfs -ls /tmp
可见,3条数据已成功导入到HDFS中,其中:
常见的错误如下,
1) 源表缺少主键
如果源表没有主键,需要指定-m 1参数 ERROR tool.ImportTool: Error during import: No primary key could be found for table ARON_TEST_TAB. Please specify one with --split-by or perform a sequential import with '-m 1'.
2) TBDS认证不通过
如果认证不通过,是因为没有export相应的环境变量
Caused by: java.io.IOException: failure to login
Caused by: javax.security.auth.login.LoginException: Can't find user name for tbds auths
增量导入只要是由以下三个参数控制:
用来指定一些列,这些列在增量导入时用来检查这些数据是否作为增量数据进行导入,和关系型数据库中的自增字段及时间戳类似. 注意:这些被指定的列的类型不能使任意字符类型,如char、varchar等类型都是不可以的,同时--check-column可以去指定多个列
用来指定增量导入的模式,两种模式分别为Append和Lastmodified
指定上一次导入中检查列指定字段最大值
该方式比较适用于源表中有一个自增型的主键列,如ID
在Oracle源表新增5条记录
在Sqoop节点,执行命令,导入新增的5条记录,因为此处id可以看做是自增的,从id > 3开始(即id=4一直到id=8的记录)
sqoop import --connect jdbc:oracle:thin:@10.0.0.85:1521:ORCL --username aron --password abcd1234 --table ARON_TEST_TAB --target-dir /tmp/Aron -m 1 --check-column id --incremental append --last-value 3
可见,增量部分的数据已成功导入到HDFS中:
该方式适用于源表中有一个时间/日期/日期+时间类型的字段,记录当前条目插入到表中的时间。
在Oracle新建另一个源表:
sqlplus #以Aron登陆 drop table aron_test_tab_2; create table aron_test_tab_2 (id int, name varchar(20), last_mod timestamp DEFAULT systimestamp); insert into aron_test_tab_2 (id, name) values (1, 'Alibaba'); insert into aron_test_tab_2 (id, name) values (2, 'Baidu'); insert into aron_test_tab_2 (id, name) values (3, 'Tencent'); insert into aron_test_tab_2 (id, name) values (4, 'SAP'); insert into aron_test_tab_2 (id, name) values (5, 'Oracle'); commit; select * from aron_test_tab_2;
这5条记录大概插入时间为2018/08/24 16:00:00左右。
先用全量的方式导入到HDFS,记录存在文件/tmp/Aron2/part-m-00000中:
sqoop import --connect jdbc:oracle:thin:@10.0.0.85:1521:ORCL --username aron --password abcd1234 --table ARON_TEST_TAB_2 --target-dir /tmp/Aron2 -m 1
大概16:10,再新增5条记录:
sqlplus #以Aron登陆 insert into aron_test_tab_2 (id, name) values (6, 'Microsoft'); insert into aron_test_tab_2 (id, name) values (7, 'Google'); insert into aron_test_tab_2 (id, name) values (8, 'Amazon'); insert into aron_test_tab_2 (id, name) values (9, 'Facebook'); insert into aron_test_tab_2 (id, name) values (10, 'Twitter'); commit; select * from aron_test_tab_2;
在Sqoop节点,执行命令,导入新增的5条记录,因为新增的记录的last_mod列是大于2018/8/24 16:10:00,所以设定基准时间点为2018/8/24 16:10:00,执行以下命令导入增量数据:
注意:列的名字要大写,即不能写成last_mod,而是LAST_MOD。
sqoop import --connect jdbc:oracle:thin:@10.0.0.85:1521:ORCL --username aron --password abcd1234 --table ARON_TEST_TAB_2 --target-dir /tmp/Aron2 -m 1 --check-column LAST_MOD --incremental lastmodified --last-value "2018-08-24 16:10:00" --append
任务执行成功,查看hdfs文件,可见新记录存在另外的文件/tmp/Aron2/part-m-00001中。
先对源表做已有记录的更新,以及新条目的插入:
sqlplus #以Aron登陆 update aron_test_tab_2 set name = 'Taobao' where id = 1; update aron_test_tab_2 set name = 'AWS' where id = 8 技术沙龙 教程文章 热点综合