sqoop哪个命令可以列出mysql_Hive记录-Sqoop常用命令

Sqoop 是一个用于在 Hadoop 和传统 RDBMS 之间迁移数据的开源工具。它依赖 MapReduce 实现并行导入。本文详细介绍了 Sqoop 常用命令,包括列出数据库、表,创建 Hive 表,导入导出数据等操作,涵盖了从 MySQL 导入数据到 Hive,以及从 Hive 导出数据到 MySQL 的各种场景,对于数据迁移工作具有实践指导意义。

1.sqoop是什么

Sqoop是一款开源的数据迁移工具,主要用于Hadoop(Hive)与传统的关系型数据库(mysql...)相互之间的数据迁移。

2.sqoop的特点

sqoop的底层实现是mapreduce,所以sqoop依赖于hadoop,数据是并行导入的。

3.sqoop常用命令

sqoop help

1)列出mysql数据库中的所有数据库中的test数据库

sqoop list-databases --connect jdbc:mysql://localhost:3306/test -usernametest -password

test

2)连接mysql并列出数据库中的表

sqoop list-tables --connect jdbc:mysql://localhost:3306/test --username test --password test

3)将关系型数据的表结构复制到hive中

sqoop create-hive-table --connect jdbc:mysql://localhost:3306/test --table users --username test

--password test --hive-table users  --fields-terminated-by "\0001"  --lines-terminated-by "\n";

参数说明:

--fields-terminated-by "\0001"  是设置每列之间的分隔符,"\0001"是ASCII码中的1,它也是hive的默认行内分隔符, 而sqoop的默认行内分隔符为"," --lines-terminated-by "\n"  设置的是每行之间的分隔符,此处为换行符,也是默认的分隔符;

注意:只是复制表的结构,表中的内容没有复制

4)将数据从关系数据库导入文件到hive表中

sqoop import --connect jdbc:mysql://localhost:3306/test --username test --password test

--table users --hive-import --hive-table users -m 2 --fields-terminated-by "\0001";参数说明:

-m 2 表示由两个map作业执行;

--fields-terminated-by "\0001"  需同创建hive表时保持一致;

5)将hive中的表数据导入到mysql数据库表中

sqoop export --connect jdbc:mysql://192.168.66.66:3306/test --username test --password test

--table users --export-dir /user/hive/warehouse/users/part-m-00000 --input-fields-terminated-by '\0001'

注意:

a.在进行导入之前,mysql中的表users必须已经提起创建好了。

b.jdbc:mysql://192.168.66.66:3306/test中的IP地址改成localhost会报异常

6)将数据从关系数据库导入文件到hive表中,--query 语句使用

sqoop import --append --connect jdbc:mysql://192.168.66.66/test --username test --password test --query "select id,age,name from userinfo where \$CONDITIONS"  -m 1  --target-dir /user/hive/warehouse/userinfos2 --fields-terminated-by ",";

7)将数据从关系数据库导入文件到hive表中,--columns  --where 语句使用

sqoop import --append --connect jdbc:mysql://192.168.66.66:3306/test --username test --password test --table userinfo --columns "id,age,name"  --where "id > 3 and (age = 88 or age = 80)"  -m 1  --target-dir /user/hive/warehouse/userinfos2 --fields-terminated-by

",";

注意:--target-dir /user/hive/warehouse/userinfos   可以用  --hive-import --hive-table userinfos进行替换

8)mysql导入数据到HDFS

sqoop import --connect jdbc:mysql://192.168.66.66:3306/test --username root --password 123456 --table test_user --target-dir  /usr/test -m 2 --fields-terminated-by "\t" --columns

"id,name"  --where 'id>2 and id<=6'

sqoop export --connect jdbc:mysql://192.168.66.66:3306/test --username root --password 123456 --export-dir '/usr/test' --table test -m 2

import的主要参数

--connect     jdbc连接地址

--connection-manager      连接管理者

--driver      驱动类

--hadoop-mapred-home

--help     help信息

-P     从命令行输入密码

--password      密码

--username      账号

--verbose    打印信息

--connection-param-file   可选参数

Argument     Description

--append     添加到hdfs中已经存在的dataset

--as-avrodatafile     导入数据作为avrodata

--as-sequencefile     导入数据位SequenceFiles

--as-textfile          默认导入数据为文本

--boundary-query      创建splits的边界

--columns

     选择列

--direct             使用直接导入快速路径

--direct-split-size      在快速模式下每n字节使用一个split

--fetch-size      一次读入的数量

--inline-lob-limit      最大数值 an inline LOB

-m,--num-mappers      通过实行多少个map,默认是4个,某些数据库8 or 16性能不错

-e,--query      通过查询语句导入

--split-by      创建split的列,默认是主键

--table      要导入的表名

--target-dir

--warehouse-dir

--where      where条件

-z,--compress     Enable compression

--compression-codec      压缩方式,默认是gzip

--null-string     字符列null值

--null-non-string      非字符列null值

export主要参数

--direct     快速导入

--export-dir

-m,--num-mappers      都少个map线程

--table      导出哪个表

--call      存储过程

--update-key      通过哪个字段来判断更新

--update-mode      插入模式,默认是只更新,可以设置为allowinsert.

--input-null-string      字符类型null处理

--input-null-non-string      非字符类型null处理

--staging-table      临时表

--clear-staging-table                     清空临时表

--batch                                     批量模式

转义字符相关参数。

Argument     Description

--enclosed-by      设置字段结束符号

--escaped-by      用哪个字符来转义

--fields-terminated-by      字段之间的分隔符

--lines-terminated-by      行分隔符

--mysql-delimiters             使用mysql的默认分隔符: , lines: \n escaped-by: \ optionally-enclosed-by: '

--optionally-enclosed-by      复制结束符

hive导入参数

--hive-home

--hive-import          插入数据到hive当中,使用hive的默认分隔符

--hive-overwrite  重写插入

--create-hive-table  建表,如果表已经存在,该操作会报错!

--hive-table   设置到hive当中的表名

--hive-drop-import-delims  导入到hive时删除 \n, \r, and \01

--hive-delims-replacement  导入到hive时用自定义的字符替换掉 \n, \r, and \01

--hive-partition-key          hive分区的key

--hive-partition-value   hive分区的值

--map-column-hive           类型匹配,sql类型对应到hive类型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值