Sqoop 学习笔记

Sqoop 学习笔记

1. 简介

Sqoop是一种用于在Hadoop和结构化数据存储(如关系数据库)之间高效传输批量数据的工具。

2. 安装

  1. 解压sqoop安装包
  2. 在conf中,复制sqoop-env-template.sh并重命名为sqoop-env.sh。配置HADOOP_COMMON_HOME, HADOOP_MAPRED_HOME, HBASE_HOME, HIVE_HOME,其中Hadoop是必须配置的,其余的有需要时可以再配。
  3. 将MySQL的JDBC包复制到lib目录下
  4. 测试是否安装成功
bin/sqoop list-databases \
--connect jdbc:mysql://localhost:3306 \
--username root \
--password root

3. 数据导入(import)

3.1 指令参数解析

bin/sqoop import <参数>

  • Common arguments:
    • --connect <jdbc-uri>: 指定JDBC连接URI
    • --connection-manager <class-name>: 自定连接manager
    • --connection-param-file <properties-file>: 数据库连接参数配置文件
    • --driver <class-name>: 驱动名
    • --hadoop-home <hdir>: Hadoop安装目录
    • --hadoop-mapred-home <dir>: MR安装目录
    • --password <password>: 数据库密码
    • --username <username>: 数据库用户名
    • --verbose: 打印更多的信息
  • Import control arguments:
    • --append: 追加模式,不删除原数据
    • --columns <col,col,col...>: 导入数据库的列名
    • --delete-target-dir: 删除指定的导入存放目录
    • --direct: Use direct import fast path
    • --direct-split-size <n>: 在direct模式下,每n个字节分为一个切片
    • -e,--query <statement>: 导入SQL语句的查询结果
    • --fetch-size <n>: 设置查询行数
    • -m,--num-mappers <n>: 设置mapper任务数量
    • --mapreduce-job-name <name>: 设置MR任务名称
    • --table <table-name>: 指定表名
    • --target-dir <dir>: 指定导入HDFS路径
    • --warehouse-dir <dir>: HDFS parent for table destination
    • --where <where clause>: where设置查询条件
  • Incremental import arguments:
    • --check-column <column>: 根据某列设置值进行增量导入
    • --incremental <import-type>: 设置增量导入模式 ‘append’ 或 ‘lastmodified’
    • --last-value <value>: check-column 从哪个值之后开始导入
  • Output line formatting arguments:
    • --enclosed-by <char>: Sets a required field enclosing character
    • --escaped-by <char>: Sets the escape character
    • --fields-terminated-by <char>: 字段分隔符
    • --lines-terminated-by <char>: 行分隔符
    • --mysql-delimiters: Uses MySQL’s default delimiter set: fields: , lines: \n escaped-by: \ optionally-enclosed-by: ‘
    • --optionally-enclosed-by <char>: Sets a field enclosing character
  • Input parsing arguments:
    • --input-enclosed-by <char>: Sets a required field encloser
    • --input-escaped-by <char>: Sets the input escape character
    • --input-fields-terminated-by <char>: Sets the input field separator
    • --input-lines-terminated-by <char>: Sets the input end-of-line char
    • --input-optionally-enclosed-by <char>: Sets a field enclosing character
  • Hive arguments:
    • --create-hive-table: 创建hive表
    • --hive-database <database-name>: 指定导入的hive数据库
    • --hive-home <dir>: 指定Hive的安装目录
    • --hive-import: 向Hive中导入数据
    • --hive-overwrite: 覆写Hive表中的内容
    • --hive-partition-key <partition-key>: 指定分区Key
    • --hive-partition-value <partition-value>: 指定分区value
    • --hive-table <table-name>: 指定导入的Hive表
  • HBase arguments:
    • --column-family <family>: Sets the target column family for the import
    • --hbase-bulkload: Enables HBase bulk loading
    • --hbase-create-table: If specified, create missing HBase tables
    • --hbase-row-key <col>: Specifies which input column to use as the row key
    • --hbase-table <table>: Import to in HBase

3.2 从MySQL导入至HDFS

  • 普通导入
###########################################
## 表结构: to_hdfs(id int, name varchar(20))
###########################################
bin/sqoop import \
--connect jdbc:mysql://hh1:3306/play \
--username hive \
--password 123456 \
--table to_hdfs
  • 指定导入路径
bin/sqoop import \
--connect jdbc:mysql://hh1:3306/play \
--username hive \
--password 123456 \
--table to_hdfs \
--target-dir /sqoop/test1
  • 指定Mapper任务数和字段分隔符
bin/sqoop import \
-connect jdbc:mysql://hh1:3306/play \
-username hive \
-password 123456 \
--direct \
--table to_hdfs \
--delete-target-dir \
--target-dir /sqoop/test1 \
--num-mappers 1 \
--fields-terminated-by '\t'
  • 数据增量导入
bin/sqoop import \
-connect jdbc:mysql://hh1:3306/play \
-username hive \
-password 123456 \
--direct \
--table to_hdfs \
--target-dir /sqoop/test1 \
--num-mappers 1 \
--fields-terminated-by '\t' \
--check-column id \
--incremental append \
--last-value 7

3.3 从MySQL导入至Hive

bin/sqoop import \
-connect jdbc:mysql://hh1:3306/play \
-username hive \
-password 123456 \
--table to_hdfs \
--fields-terminated-by '\t' \
--lines-terminated-by '\n' \
--hive-import \
--hive-database sqoop \
--create-hive-table \
--hive-table test1 \
--num-mappers 1

4. 数据导出(export)

4.1 指令参数解析

bin/sqoop export <参数>

  • Common arguments: 同import参数
  • Export control arguments:
    • --batch: 表示以批处理模式执行的底层语句
    • --columns <col,col,col...>: 导出的数据库字段
    • --direct: Use direct export fast path
    • --export-dir <dir>: 导出到指定的HDFS目录
    • -m,--num-mappers <n>: 设置mapper任务数量
    • --mapreduce-job-name <name>: 设置MR任务名称
    • --table <table-name>: 导出的表名称
    • --update-key <key>: Update records by specified key column
    • --update-mode <mode>: Specifies how updates are performed when new rows are found with non-matching keys in database
    • --staging-table <table-name>: Intermediate staging table
    • --clear-staging-table: Indicates that any data in staging table can be deleted
  • Input parsing arguments:
    • --input-enclosed-by <char>: Sets a required field encloser
    • --input-escaped-by <char>: Sets the input escape character
    • --input-fields-terminated-by <char>: Sets the input field separator
    • --input-lines-terminated-by <char>: Sets the input end-of-line char
    • --input-optionally-enclosed-by <char>: Sets a field enclosing character
  • Output line formatting arguments:
    • --enclosed-by <char>: Sets a required field enclosing character
    • --escaped-by <char>: Sets the escape character
    • --fields-terminated-by <char>: Sets the field separator character
    • --lines-terminated-by <char>: Sets the end-of-line character
    • --mysql-delimiters: Uses MySQL’s default delimiter set: fields: , lines: \n escaped-by: \ optionally-enclosed-by: ‘
    • --optionally-enclosed-by <char>: Sets a field enclosing character

4.2 从HDFS导出至MySQL

  • 全表数据导入
###########################################
## 表结构: to_mysql(id int, name varchar(20))
###########################################
bin/sqoop export \
-connect jdbc:mysql://hh1:3306/play \
-username hive \
-password 123456 \
--table to_inc \
--columns id,name \
--export-dir /sqoop/test1 \
--fields-terminated-by '\t' \
--lines-terminated-by '\n' \
--num-mappers 1
  • 指定列导入
###################################################################
## 表结构: to_inc(no int auto_increatment, id int, name varchar(20))
###################################################################
bin/sqoop export \
-connect jdbc:mysql://hh1:3306/play \
-username hive \
-password 123456 \
--table to_inc \
--columns id,name \
--export-dir /sqoop/test1 \
--fields-terminated-by '\t' \
--lines-terminated-by '\n' \
--num-mappers 1

5. Job

5.1 创建Job

  • 指令:bin/sqoop job --create <job-id>
  • 示例:创建一个数据增量导入job
bin/sqoop job \
--create import_data1 \
-- \
import \
--onnect jdbc:mysql://hh1:3306/play \
--username hive \
--password 123456 \
--direct \
--table to_hdfs \
--target-dir /sqoop/test1 \
--num-mappers 1 \
--fields-terminated-by '\t' \
--check-column id \
--incremental append \
--last-value 7

5.2 查看所有Job

bin/sqoop job --list

5.3 查看一个Job详情

bin/sqoop job --show <job-id>

5.4 执行Job

bin/sqoop job --exec <job-id>

5.5 删除Job

bin/sqoop job --delete <job-id>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值