当前位置:网站首页>Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
2022-07-26 09:40:00 【シ風】
1.环境说明
还是之前的环境:
# 不必要信息不再贴出
# JDK
[[email protected] ~]# java -version
java version "1.8.0_251"
# MySQL
[[email protected] ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.28
# Hadoop
[[email protected] ~]# hadoop version
Hadoop 3.1.3
# Hive
[[email protected] ~]# hive --version
Hive 3.1.2
# Sqoop
[[email protected] ~]# sqoop version
Sqoop 1.4.7
2.import-all-tables
Sqoop1支持通过 import-all-tables 命令进行全库导出到 HDFS/Hive,但需要注意有以下两个限制:
- 所有表必须有主键;或者使用 --autoreset-to-one-mapper ,代表只启动一个 map task,即不并行执行。【若仍希望并行执行,则可以使用 --split-by 指明拆分数据的参考列】 ;
- 你不能使用非默认的分割列,也不能通过 WHERE 子句添加任何限制。官方原文
You must not intend to use non-default splitting column, nor impose any conditionsvia a WHERE clause.
导入的默认路径是:/user/用户名/若干数据表名/数据文件和_SUCCESS,如果想要指定目录需要使用指定的参数【具体参数可以使用 sqoop help import-all-tables 查看】
【实例 1️⃣ 】全库导入到 HDFS:
# 注意:指定导入目录使用的是 --warehouse-dir 不是 --target-dir 参数
sqoop import-all-tables \
--connect jdbc:mysql://tcloud:3306/mysql \
--username root \
--password [email protected]\
--warehouse-dir /sqoop_all \
--fields-terminated-by '\t' \
--bindir /usr/local/sqoop/lib/ \
-m 1
# 所有表都有主键时 可以设置-m 的参数大于1 否则只能为 1
【实例 2️⃣ 】全库导出到 Hive:
# 注意:指定导入的数据库使用 --hive-database
sqoop import-all-tables \
-Dorg.apache.sqoop.splitter.allow_text_splitter=true \
--connect jdbc:mysql://tcloud:3306/mysql \
--username root \
--password [email protected]\
--hive-database sqoop_test \
--hive-import \
--hive-overwrite \
--bindir /usr/local/sqoop/lib/ \
-m 1
3.query
Sqoop1 支持使用 query 参数定义查询 SQL实现对数据的过滤,从而可以导入任何想要的结果集。实例:
sqoop import \
--connect jdbc:mysql://tcloud:3306/mysql \
--username root \
--password [email protected]\
--query 'select * from help_keyword where $CONDITIONS and help_keyword_id < 50' \
--delete-target-dir \
--target-dir /sqoop_hive \
--hive-database sqoop_test \ # 指定导入目标数据库 不指定则默认使用Hive中的default库
--hive-table filter_help_keyword \ # 指定导入目标表
--split-by help_keyword_id \ # 指定用于 split 的列
--hive-import \ # 导入到 Hive
--hive-overwrite \
--bindir /usr/local/sqoop/lib/ \
-m 1
在使用 query 进行数据过滤时,需要注意以下三点:
- 必须用 --hive-table 指明目标表;
- 如果并行度 -m 不为 1 或者没有指定 --autoreset-to-one-mapper ,则需要用 --split-by 指明参考列;
- SQL 的 where 字句必须包含 $CONDITIONS ,这是固定写法,作用是动态替换。
4.字段类型支持
Sqoop1默认支持数据库的大多数字段类型,但是某些特殊类型是不支持的。遇到不支持的类型,程序会抛出异常 Hive does not support the SQL type for column xxx 异常,此时可以通过下面两个参数进行强制类型转换:
- –map-column-java :重写 SQL 到 Java 类型的映射;
- –map-column-hive :重写 Hive 到 Java 类型的映射。
示例如下,将原先 id 字段强制转为 String 类型, value 字段强制转为 Integer 类型:
$ sqoop import ... --map-column-java id=String,value=Integer
边栏推荐
- The provincial government held a teleconference on safety precautions against high temperature weather across the province
- Source code analysis of object wait notify notifyAll
- 音视频知识
- antd TreeSelect获取父节点的值
- 登录模块用例编写
- 新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐
- Great reward for interview questions
- C managed and unmanaged
- Gauss elimination solves the inverse of matrix (Gauss)
- 【信息系统项目管理师】初见高项系列精华汇总
猜你喜欢

Gauss elimination solves the inverse of matrix (Gauss)

挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破

asp.net 使用redis缓存

Jmeter配置元件之CSV数据文件设置

【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)

苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
![[MySQL] understand the important architecture of MySQL (I)](/img/89/5fb595b0112fac987626857b76f9a4.png)
[MySQL] understand the important architecture of MySQL (I)

The problem of accessing certsrv after configuring ADCs

matlab simulink实现模糊pid对中央空调时延温度控制系统控制

Fiddler抓包工具之移动端抓包
随机推荐
(2) Hand eye calibration of face scanner and manipulator (eye out of hand: nine point calibration)
POJ 1012 Joseph
EOJ 2020 1月月赛 E数的变换
Calling DLL to start thread
Node 内存溢出及V8垃圾回收机制
面试突击68:为什么 TCP 需要 3 次握手?
dll中的全局变量
Node memory overflow and V8 garbage collection mechanism
网站设计需要的基本知识
Redis sentinel mode setup under Windows
Server and client dual authentication (2)
QT handy notes (III) use qtcharts to draw a line chart in VS
uni-app学习总结
mfc随手笔记
Customize permission validation in blazor
E. Two Small Strings
正则表达式
2020-12-29
matlab simulink实现模糊pid对中央空调时延温度控制系统控制
The provincial government held a teleconference on safety precautions against high temperature weather across the province