sqoop overview

1/列出mysql数据库中的所有数据库sqoop list-databases -connect jdbc:mysql://localhost:3306/ -username root -password 123456报错:host is not allowed to connect mysql / access denied for ‘root’@‘hadoop1’一个是查看服务器防火墙  然后查看 show grants  是否允许远程连接  grant all privileges on *.* to 'root'@'%' identified by 'password' grant option; flush privileges;注意格式 list-databases  是一个命令option 没有空格  可以在connect参数中指出具体的dbname2/连接mysql并  列出test数据库中的表sqoop list-tables -connect jdbc:mysql://localhost:3306/test -username root -password 1234563/将关系型数据的表结构 复制到hive中,只是复制表结构  内容不复制  -hive-table hiveTableName 默认指定default数据库的表名 可采用如下方式指定具体的数据库内的数据表  hiveDbName.hiveTableName  如果目标表已经存在了,那么创建任务会失败sqoop create-hive-table -connect jdbc:mysql://localhost:3306/test -table student -username root -password 123456 -hive-table studentinhive4/从关系数据库导入文件到hive中 -m,--num-mappersUse n map tasks to import in parallel 导入数据时并行开启n Map任务 sqoop import -connect jdbc:mysql://localhost:3306/test -username root -password 123456 -table student -hive-import -hive-table studentinhive -m 2报错:error tool.importtool error during import no primary key could be found for table s_test .please speify one with --split-by or perform a sequential import with '-m 1'sqoop import -connect jdbc:mysql://localhost:3306/zxtest -username root -password 123456 -table sqoop_test -hive-import -hive-table s_test --split-by id -m 2报错:error tool.importtool encounted ioexception running import job java.io.exception failed to run job application rejected by queue placement policysqoop import -Dmapred.job.queue.name=develop -connect jdbc:mysql://localhost:3306/test -username root -password 123456 -table sqoop_test -hive-import -hive-table s_test --split-by id -m 2假设id为1-6-6-7-8由于执行导入时指定两个map任务 以id进行分区的,因此在存储时也是以part-m-00000 和  part-m-00001形式, 对应的id为1-4 / 5-9当再次执行导入任务时,设定m为4,默认以追加形式写入数据。会以part-m-00000/part-m-00001/part-m-00002/part-m-00003 对应的id分区为 1-2 3-4 5-6-6 7-8 (默认根据hash进行分区 所以第三个分区有三个记录)但是由于当前数据库对应数据表的存储位置下已经存在了part-m-00000和part-m-00001,则新追加导入的以part-m-00000_copy_1 和 part-m-00001_copy_1存在。因此在hive操作时,如果是select 操作,会首先读取part-m-00000 然后是 part-m-00000_copy_1,然后 part-m-00001,然后 part-m-00001_copy_1 然后 part-m-00002,part-m-00003。数据记录为1-4 1-2 5-8 3-4 5-6-6 7-8  hive没有指定字段分隔符和行分隔符时 默认的字段分隔符为ascii码的控制符\001,建表的时候用 --fields-terminated-by '\001'    --lines-terminated-by “\n”5/将hive中的表数据导入到mysql 中,在进行导入前 mysql中的表hive_test 必须提前创建好sqoop export -Dmapred.job.queue.name=develop -connect jdbc:mysql://localhost:3306/test -username root -table studentfromhive --fields-terminated-by '\001' --lines-terminated-by “\n” -export-dir /user/hive/warehouse/test_shi.db/studentinhive/studentinhive;会读取studentinhive目录下的多个文件 以指定分隔符形式读取字段解析记录,导入mysql数据库中。如果因为某些原因导致中断,任务失败,可能会造成部分数据导入成功。如果要求,保证导入数据的事务性,可以通过配置(类似与分布暂存,然后一次性写入) --staging-tableThe table in which data will be staged before being inserted into the destination table.--clear-staging-table Indicates that any data present in the staging table can be deleted. 由于sqoop是通过map完成数据的导入,各个map过程是独立的,没有事物的概念,可能会有部分map数据导入失败的情况。为了解决这一问题,sqoop中有一个折中的办法,即是指定中间 staging表,成功后再由中间表导入到结果表。这一功能是通过 --staging-table指定,同时staging表结构也是需要提前创建出来的:sqoop export --connect jdbc:mysql://192.168.81.176/sqoop --username root -password passwd --table sds --export-dir /user/guojian/sds --staging-table sds_tmp需要说明的是,在使用 --direct, --update-key或者--call存储过程的选项时,staging中间表是不可用的。如果指定的--fields-terminated-by '\001'(^A )    --lines-terminated-by “\n” 不正确或者没有指定的话,在yarn(每一个sqoop 导入数据程序配置了队列 会以一个jar(导出或者导入的表名)包形式使用mapreduce计算引擎运行在该队列上)的日志里可以看到failed task , cannot export data please check failed map task logs.caused by java.lang.numberformatexception for input string '****'所以在往hive中导入数据时一定要显示定义使用字段分隔符和行分隔符号6/从数据库导出表的数据到HDFS上的文件sqoop import -Dmapred.job.queue.name=develop -connect jdbc:mysql://localhost:3306/test -username=root -table student --fields-terminated-by '\t' --lines-terminated-by “\n” --split-by id -m 2 -target-dir /user/test/studentfile  备注:studentfile 必须是在hdfs文件系统上不存在的文件夹。由于指定 -m 2 因此,会在studentfile 目录下生成三个文件,一个是存储元数据信息,另外两个分别是part-m-00000/part-m-00001通过hdfs dfs -cat ..../studentfile/part-m-00000 浏览对应的数据库记录。7/数据库增量导入表数据到hdfs中sqoop import -Dmapred.job.queue.name=develop -connect jdbc:mysql://localhost:3306/test -username=root -table student --fields-terminated-by '\t' --lines-terminated-by “\n” --split-by id -m 2 -target-dir /user/test/studentfile -check-column id -incremental append -last-value 8hdfs文件里多个两个part-m-00002/part-m-00003While the Hadoop generic arguments must precede any import arguments, you can type the import arguments in any order with respect to one another. arguments are grouped into collections organized by function. Some collections are present in several tools (for example, the "common" arguments). hadoop的类参数需要在任何其他的import参数之前指定,余下所有参数的顺序可以随意。因为解析命令时,调用函数对参数以分组形式写入集合开始后续操作的。其类参数大概有-conf -D -fs -jt -files -libjars -archivessqoop读取数据库是一行一行的读取记录,导入hdfs。导入进程是并发执行的,输出会有多个文件。可能是序列化文件 文本文件(分隔符)sqoop option-file$ sqoop import -Dmapred.job.queue.name=develop -connect jdbc:mysql://localhost:3306/test -username=root -table student --fields-terminated-by '\t' --lines-terminated-by “\n” --split-by id -m 2 -target-dir /user/test/studentfile$ sqoop --options-file /users/homer/work/import.txt## Options file for Sqoop import## Specifies the tool being invokedimport# Hadoop parameter and value-Dmapred.job.queue.name=develop# Connect parameter and value--connectjdbc:mysql://localhost:3306/test# Username parameter and value--usernameroot# --fields-terminated-by\t# --lines-terminated-by\n# --split-byid# -m2# --target-dir/user/test/studentfile## Remaining options should be specified in the command line.#注意事项:1/ option在option file里参数和值需要换行 且 每一类都需要换行 且每一个参数和值后不能有空格等多余字符  如果是 --split-by id 会报 -- error parsing arguments for import  unrecognized argument 2/ --fields-terminated-by ‘\t’ 以及 --lines-terminated-by '\n'  不仅需要换行 还需要将单引号去除 否则 会报 -- error parsing arguments for import  unrecognized argument 3/ 可以在option-file 外指定相关配置$ sqoop import --connect jdbc:mysql://database.example.com/employees \    --username venkatesh --password-file ${user.home}/.password使用密码文件读取相关密码 需要确保密码文件中没有多余字符  可以采取 echo -n “yoursecret” > password.filesqoop默认支持多种数据库,包含mysql,以jdbc:mysql:// 开头的连接字符串会被sqoop自动处理,如果是其他类型的数据库,则需要指定驱动和保证sqoop的lib库中包含对应的jdbc jar包。--null-stringThe string to be written for a null value for string columns--null-non-stringThe string to be written for a null value for non-string columns

The --null-string and --null-non-string arguments are optional.\ If not specified, then the string "null" will be used.

$ sqoop import -Dmapred.job.queue.name=develop -connect jdbc:mysql://localhost:3306/test -username=root -table student --columns “id,name” --where "id>6" --fields-terminated-by '\t' --lines-terminated-by “\n” --split-by id -m 2 -target-dir /user/test/studentfileSelect

注意事项:指定列时 不能重复指定

$ sqoop import -Dmapred.job.queue.name=develop -connect jdbc:mysql://localhost:3306/test -username=root --query ’select * from student where id > 7 and $CONDITIONS‘ --fields-terminated-by '\t' --lines-terminated-by “\n” --split-by id -m 2 -target-dir /user/test/studentfileQuery

error: tool.importtool:encountered ioexecption running import job java.io.exception query [select * from student where id > 7] must contains '$CONDITIONS' in where clause

注意事项:where条件中必须要有 $CONDITIONS

如果判断条件中包含的是双引号,则查询语句需要更改 使用 \$CONDITIONS 如下:

$ sqoop import -Dmapred.job.queue.name=develop -connect jdbc:mysql://localhost:3306/test -username=root --query "select * from student where name = 'shidaiwu' and \$CONDITIONS" --fields-terminated-by '\t' --lines-terminated-by “\n” --split-by id -m 2 -target-dir /user/test/studentfileQuery

where中不能有or

默认开启4个task

如果是并行查询导入  必须指定 -split-by  否则需要指定 -m 1

validate:验证源数据记录和目标数据记录数是否相同 -- 验证成功 在语句执行完毕会显示

$ sqoop import -Dmapred.job.queue.name=develop -connect jdbc:mysql://localhost:3306/test -username=root -table student --fields-terminated-by '\t' --lines-terminated-by “\n” --validate -m 2 -target-dir /user/test/studentfile

注意:validation is not supported for free from query but single table only 不支持 query 选项

导出数据时  未选择的列在数据库表中需要为可空类型 否则数据库将拒绝接受数据导入

可以增加map数,但是当数据库遇到性能瓶颈时,增加map反而会降低性能。

导出数据时,由于非空列 的null数据导出至表将会被打断任务

导出任务被分为多个部分 多事务 其中一部分错误将导致只有部分数据被导入至表  如果使用--staging-table 将数据保存至临时表内  最终作为单一事务将临时表数据导入至目标表

一次导出任务并行,每一个写操作都是一个独立的事务。注意事务缓存的增长不能超过警戒线,以及内存溢出的清空。因此,一次导出是一件原子事务。可能在数据尚未完全导入之前就可以在表中看见。

导出任务可能失败的原因:

失去连接

违反一致性约束插入

解析一个不完整的或残缺的记录

使用不正确的分割符解析记录

容量问题  内存不足或磁盘空间不足

每一个export map任务 都在独立的事务中进行作业,相互独立的提交他们各自的任务。如果任务失败,当前事务会回滚而先前已完成的事务将继续保留在数据库内,就会造成部分完成的导出任务。

插入操作  保证表已存在 违反一致性约束 将失败

那些未被选择导出数据的列  在数据库中要么具有默认值 要么允许null值 否则将会失败

导出数据时调用存储过程

Validation

比较源表和目标表的行数  验证操作效果

使用sqoop job 定义多次import export

job  重复执行  增量导入时 允许新增数据持续性加入当前文件

增量导入 --incremental append    --check-column id      --last-value 100

合并数据集时指定新旧数据集且指定目标目录

合并时保证一致性约束  保证主键的唯一性

自动生成java类文件

使用eval快速简单评估sql 查询    仅仅用于评估目的    验证数据库链接等目的  在生产工作流中并不支持

sqoop job --create

sqoop job --delete

sqoop job --exec

sqoop job --show

sqoop job --list

$ sqoop job -Dmapred.job.queue.name=develop --create importjob -- import -connect jdbc:mysql://localhost:3306/test -username=root -table student --fields-terminated-by '\t' --lines-terminated-by “\n” --validate -m 2 -target-dir /user/test/studentfile

注意事项:-Dmapred.job.queue.name=develop --create importjob -- import -connect hadoop类配置在最前,其次指定import时,需要注意留一个空格,否则会报错 无法识别的参数

Creating saved jobs is done with the --create action. This operation requires a -- followed by a tool name and its arguments. The tool and its arguments will form the basis of the saved job.

报警告: mapred.job.queue.name 过时了 使用 mapreduce.job.queuename=develop

sqoop job --delete importjob

sqoop job --exec importjob

sqoop job --show importjob

查看和执行 importjob时 需要指定Mysql的password

sqoop job --meta-connect jdbc:hsqldb:hsql://sqoopMetaserver.example.com:16000/sqoop

增量导入数据的:

sqoop job -Dmapred.job.queue.name=develop --create importjobIncrement -- import -connect jdbc:mysql://localhost:3306/test -username=root -table student --fields-terminated-by '\t' --lines-terminated-by “\n” -m 1 -target-dir /user/test/studentfileJobIncrement -incremental append --check-column id --last-value 0

此时通过show importjobIncrement 发现 last-value = 0

执行sqoop job --exec importjobIncrement 查看对应文件目录后发现数据开始导入

再次通过show importjobIncrement 发现 last-value = 10

insert 数据进入student 表中

执行sqoop job --exec importjobIncrement 查看对应文件目录后发现数据开始增加

再次通过show importjobIncrement 发现 last-value = 11

*********************************************************

Sqoop provides an incremental import mode which can be used to retrieve only rows newer than some previously-imported set of rows.

The following arguments control incremental imports:

Table 5. Incremental import arguments:

Argument Description

--check-column (col) Specifies the column to be examined when determining which rows to import. (the column should not be of type CHAR/NCHAR/VARCHAR/VARNCHAR/ LONGVARCHAR/LONGNVARCHAR)

--incremental (mode) Specifies how Sqoop determines which rows are new. Legal values for mode include append and lastmodified.

--last-value (value) Specifies the maximum value of the check column from the previous import.

Sqoop supports two types of incremental imports: append and lastmodified. You can use the --incremental argument to specify the type of incremental import to perform.

You should specify append mode when importing a table where new rows are continually being added with increasing row id values. You specify the column containing the row’s id with --check-column. Sqoop imports rows where the check column has a value greater than the one specified with --last-value.

An alternate table update strategy supported by Sqoop is called lastmodified mode. You should use this when rows of the source table may be updated, and each such update will set the value of a last-modified column to the current timestamp. Rows where the check column holds a timestamp more recent than the timestamp specified with --last-value are imported.

At the end of an incremental import, the value which should be specified as --last-value for a subsequent import is printed to the screen. When running a subsequent import, you should specify --last-value in this way to ensure you import only the new or updated data. This is handled automatically by creating an incremental import as a saved job, which is the preferred mechanism for performing a recurring incremental import. See the section on saved jobs later in this document for more information.

**************************************************************

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容