sqoop 实操

教程 犀牛 ⋅ 于 2021-06-19 13:36:22 ⋅ 1196 阅读

sqoop常用参数

安全环境下操作需要做安全认证

  • 常用命令
命令名称 对应类 命令说明
import ImportTool 将关系型数据库数据导入到HDFS、HIVE、HBASE
export ExportTool 将HDFS上的数据导出到关系型数据库
codegen CodeGenTool 获取数据库中某张表数据生成Java并打成Jar包
create-hive-table CreateHiveTableTool 创建hive的表
eval EvalSqlTool 查看SQL的执行结果
list-databases ListDatabasesTool 列出所有数据库
list-tables ListTablesTool 列出某个数据库下的所有表
help HelpTool 打印sqoop帮助信息
version VersionTool 打印sqoop版本信息
  • 连接参数列表
Argument Description
--connect <jdbc-uri> Specify JDBC connect string 指定JDBC连接字符串
--connection-manager <class-name> Specify connection manager class to use 指定要使用的连接管理器类
--driver <class-name> Manually specify JDBC driver class to use 指定要使用的JDBC驱动类
--hadoop-mapred-home <dir> Override $HADOOP_MAPRED_HOME 指定$HADOOP_MAPRED_HOME路径
--help Print usage instructions 帮助信息
--password-file Set path for a file containing the authentication password 设置用于存放认证的密码信息文件的路径
-P Read password from console 从控制台读取输入的密码
--password <password> Set authentication password 设置认证密码
--username <username> Set authentication username 设置认证用户名
--verbose Print more information while working 打印运行信息
--connection-param-file <filename> Optional properties file that provides connection parameters 指定存储数据库连接参数的属性文件
  • 连接MySQL示例
# 查询数据库列表 对标show databases
sqoop list-databases --connect jdbc:mysql://worker-1:3306/ --username root --password Congf1daof4

file

# 查询指定库下面所有表 对标show tables in cm
sqoop list-tables --connect jdbc:mysql://worker-1:3306/cm --username root --password Congf1daof4

file

sqoop应用

  • eval查看sql查询结果
sqoop eval \
--connect jdbc:mysql://worker-1:3306/cm \
--username root \
--password Congf1daof4 \
--query "select * from HOSTS"

file

  • codegen指定表生成jar
sqoop codegen \
--connect jdbc:mysql://worker-1:3306/cm \
--username root \
--password Congf1daof4 \
--table HOSTS

file

file

  • create-hive-table创建hive表
sqoop create-hive-table \
--connect jdbc:mysql://worker-1:3306/cm \
--username root \
--password Congf1daof4 \
--table HOSTS \
--hive-table createbysqoop

file

  • import to hive
# 使用query参数或者-e参数时必须带where $CONDITIONS
# $CONDITIONS意义,为sqoop系统参数,任务执行时用于划分并行度使用,解析时会按照split-by字段进行分段划分,如下的host_id会被分为4份,而从什么位置开始划分则由系统参数$CONDITIONS决定的
# num-mappers意义,MapReduce执行时map并行度
sqoop import \
--connect jdbc:mysql://worker-1:3306/cm \
--username root \
--password Congf1daof4 \
--target-dir /user/hive/importbysqoop1 \
--delete-target-dir \
--fields-terminated-by "\t" \
--num-mappers 4 \
--split-by host_id \
--query 'select * from HOSTS where $CONDITIONS'

file

# 导入不同格式,支持格式as-avrodatafile、as-binaryfile、as-parquetfile、as-sequencefile、as-textfile(默认格式)
sqoop import \
--connect jdbc:mysql://worker-1:3306/cm \
--username root \
--password Congf1daof4 \
--target-dir /user/hive/importbysqoop1 \
--delete-target-dir \
--as-sequencefile \
--fields-terminated-by "\t" \
--num-mappers 4 \
--split-by host_id \
--query 'select * from HOSTS where $CONDITIONS'

file

# 导入hive表
sqoop import \
--connect jdbc:mysql://worker-1:3306/cm \
--username root \
--password Congf1daof4 \
--table HOSTS \
--delete-target-dir \
--hive-import \
--fields-terminated-by "\t" \
--hive-overwrite \
--hive-table HOSTS
# 导入hive表使用parquet格式
sqoop import \
--connect jdbc:mysql://worker-1:3306/cm \
--username root \
--password Congf1daof4 \
--table HOSTS \
--delete-target-dir \
--as-parquetfile \
--hive-import \
--fields-terminated-by "\t" \
--hive-overwrite \
--hive-table HOSTS

file

file

# hive增量导入
# check-column检查列,以哪一列作为增量检查列
# last-value 从哪里开始向后导入(不包含当前值)
# incremental 增量模式
sqoop import \
--connect jdbc:mysql://worker-1:3306/cm \
--username root \
--password Congf1daof4 \
--table HOSTS \
--as-parquetfile \
--target-dir /user/hive/warehouse/hosts \
--incremental append \
--check-column host_id \
--last-value 1

file

  • import to hbase
sqoop import \
--connect jdbc:mysql://worker-1:3306/cm \
--username root \
--password Congf1daof4 \
--table HOSTS \
--hbase-create-table \
--hbase-table xiniu:hosts \
--column-family cf1 \
--hbase-row-key HOST_ID

file

版权声明:原创作品,允许转载,转载时务必以超链接的形式表明出处和作者信息。否则将追究法律责任。来自海汼部落-犀牛,http://hainiubl.com/topics/75727
回复数量: 0
    暂无评论~~
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
    Ctrl+Enter