前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sqoop之旅6-数据导出

sqoop之旅6-数据导出

作者头像
皮大大
发布2021-03-02 16:06:32
4560
发布2021-03-02 16:06:32
举报

sqoop-export

Purpose

The export tool exports a set of files from HDFS back to an RDBMS. The target table must already exist in the database. The input files are read and parsed into a set of records according to the user-specified delimiters.

  • 目的:将数据从HDFS导出到RDBMS中
  • 导出的目标表table必须是已经存在的
Syntax
  • 导出的基本语法
代码语言:javascript
复制
$ sqoop export (generic-args) (export-args)
$ sqoop-export (generic-args) (export-args)
  • 主要参数有

  • 主要的控制参数

对上面的表格的几个重要参数解释:

  1. —columns:没有包含在其后面的字段类型,要么具有默认参数,要么允许插入空值

By default, all columns within a table are selected for export. You can select a subset of columns and control their ordering by using the --columns argument. Note that columns that are not included in the --columns parameter need to have either defined default value or allow NULL values.

  1. —export-dir:导出目录,必须指定;参数必须配合—table或者—call

The --export-dir argument and one of --table or --call are required.

  1. —input-null-string—input-null-string

The --input-null-string and --input-null-non-string arguments are optional. If --input-null-string is not specified, then the string “null” will be interpreted as null for string-type columns. (1) If --input-null-non-string is not specified, then both the string “null” and the empty string will be interpreted as null for non-string columns. (2)

  • 两个参数是可选的
  • 如果参数(1)未被指定,则NULL被翻译成空值
  • 如果参数(2)未被指定,则无论是NULL值还是空字符串都被翻译成空值
Inserts and Updates

By default, sqoop-export appends new rows to a table; each input record is transformed into an INSERT statement that adds a row to the target database table. If you specify the --update-key argument, Sqoop will instead modify an existing dataset in the database. Each input record is treated as an UPDATE statement that modifies an existing row.

  • 默认情况下,sqoop-export是将新的一行数据追加到表的末尾
  • 上面的操作相当于是执行了一条SQL的insert语句
  • 指定了—update-key参数,则在进行操作的时候会更新现有的数据
代码语言:javascript
复制
CREATE TABLE foo(
    id INT NOT NULL PRIMARY KEY,
    msg VARCHAR(32),
    bar INT);

# HDFS中的数据表现为
0,this is a test,42
1,some more data,100
...

# SQL语句表现形式
UPDATE foo SET msg='this is a test', bar=42 WHERE id=0;
UPDATE foo SET msg='some more data', bar=100 WHERE id=1;
...

两个更新的模式updatemod

  • updateonly:默认模式,更新已经存在的记录,不插入新数据
  • allowinsert:允许插入新值,相当于是append
update-key

根据update-key中指定的字段是否为主键

  1. 不是主键:
    1. updateonly:仅仅是更新update
    2. allowinsert:相当于是append,会有数据的冗余
  2. 是主键:
    1. updateonly:仅仅是更新update
    2. allowinsert:相当于是insert+append
demo
  • 全量导出
代码语言:javascript
复制
$ sqoop export
--connect jdbc:mysql://ubuntu:3306/sqooptest \
--username root \
--password 123456 \
--table bigdata \
--export-dir /usr/root/bigdata2    # 从bigdata导出bigdata2中
  • 增量导出
代码语言:javascript
复制
$ sqoop export
--connect jdbc:mysql://ubuntu:3306/sqooptest \
--username root \
--password 123456 \
--table bigdata \
--export-dir /usr/root/bigdata2  \  # 从bigdata导出bigdata2中
--update-key class_id  \   # 没有主键
--update-mode updateonly   # 只更新update
--update-mode allowinsert  # 相当于是append,会有数据冗余

$ sqoop export
--connect jdbc:mysql://ubuntu:3306/sqooptest \
--username root \
--password 123456 \
--table bigdata \
--export-dir /usr/root/bigdata2  \  # 从bigdata导出bigdata2中
--update-key class_id  \   # 有主键
--update-mode updateonly   # 只更新update
--update-mode allowinsert   # 更新和追加
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • sqoop-export
    • Purpose
      • Syntax
        • Inserts and Updates
          • update-key
            • demo
            相关产品与服务
            数据库
            云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档