Fork me on GitHub

sqoop之旅6-数据导出

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

  • 导出的基本语法
1
2
$ sqoop export (generic-args) (export-args)
$ sqoop-export (generic-args) (export-args)
  • 主要参数有

Mf6G7D.png

  • 主要的控制参数

Mf6Dnf.png

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

  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参数,则在进行操作的时候会更新现有的数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

  • 全量导出
1
2
3
4
5
6
$ sqoop export
--connect jdbc:mysql://ubuntu:3306/sqooptest \
--username root \
--password 123456 \
--table bigdata \
--export-dir /usr/root/bigdata2 # 从bigdata导出bigdata2中
  • 增量导出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ 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 # 更新和追加

本文标题:sqoop之旅6-数据导出

发布时间:2019年11月20日 - 19:11

原始链接:http://www.renpeter.cn/2019/11/20/sqoop%E4%B9%8B%E6%97%856-%E6%95%B0%E6%8D%AE%E5%AF%BC%E5%87%BA.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea