当前位置:网站首页>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
边栏推荐
- RMQ学习笔记
- The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
- R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)
- Logical architecture of MySQL
- 在同一conda环境下先装Pytroch后装TensorFlow
- JS table auto cycle scrolling, mouse move in pause
- SSG框架Gatsby访问数据库,并显示到页面上
- copyTo
- Force deduction brush questions, sum of three numbers
- 解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.
猜你喜欢
uni-app学习总结
[MySQL] understand the important architecture of MySQL (I)
【荧光字效果】
2022年中科磐云——服务器内部信息获取 解析flag
The diagram of user login verification process is well written!
Search module use case writing
A new paradigm of distributed deep learning programming: Global tensor
Qt随手笔记(三)在vs中使用QtCharts画折线图
Fuzzy PID control of motor speed
服务器、客户端双认证(2)
随机推荐
PHP一次请求生命周期
“互联网+”时代的现代医学
EOJ 2020 January race E-number transformation
Learning notes: what are the common array APIs that change the original array or do not change the original array?
Interview shock 68: why does TCP need three handshakes?
The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
copyTo
QT随手笔记(六)——更新界面、截图、文件对话框
OFDM Lecture 16 - OFDM
IIS website configuration
[MySQL] understand the important architecture of MySQL (I)
解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
Fuzzy PID control of motor speed
asp. Net using redis cache
OpenCV 表格识别之表格提取(二)
nodejs服务后台执行(forever)
附加到进程之后,断点显示“当前不会命中断点 还没有为该文档加载任何符号”
开发转测试:从0开始的6年自动化之路...
Neural network and deep learning-6-support vector machine 1-pytorch