Sqoop学习笔记

声明:允许转载,转载请注明链接,谢谢合作!
sqoop是Apache顶级项目,主要用来在Hadoop和关系数据库中传递数据。通过sqoop,我们可以方便的将数据从关系数据库导入到HDFS,或者将数据从HDFS导出到关系数据库。
一、
1.sqoop的作用在于连接关系型数据库与hdfs。其特色在于,可以使用通过起mr的形式从rdbms中取数。
Sqoop,类似于其他ETL工具,使用元数据模型来判断数据类型并在数据从数据源转移到Hadoop时确保类型安全的数据处理。Sqoop专为大数据批量传输设计,能够分割数据集并创建Hadoop任务来处理每个区块。

Sqoop支持增量更新,将新记录添加到最近一次的导出的数据源上,或者指定上次修改的时间戳。

2. sqoop ##sqoop命令
sqoop包含一系列的工具,运行sqoop help可以查看相关帮助,
$ ./sqoop help
usage: sqoop COMMAND [ARGS]
Available commands:
   codegen             Generate code to interact with database records
  create-hive-table  Import a table definition into Hive
  eval               Evaluate a SQL statement and display the results
   export              Export an HDFS directory to a database table
  help               List available commands
   import              Import a table from a database to HDFS
  import-all-tables  Import tables from a database to HDFS
  job                Work with saved jobs
  list-databases     List available databases on a server
  list-tables         List available tables in a database
  merge              Merge results of incremental imports
  metastore          Run a standalone Sqoop metastore
  version            Display version information
二、

import ##表示导入

--connect jdbc:mysql://ip:3306/sqoop ##告诉jdbc,连接mysql的url

--username root ##连接mysql的用户名

--password admin ##连接mysql的密码

--table aa ##从mysql导出的表名称

--fields-terminated-by '\t' ##指定输出文件中的行的字段分隔符

-m 1 ##复制过程使用1个map作业

三、
1)列出mysql数据库中的所有数据库 由数据库连接获取数据库信息
sqoop list-databases –connect jdbc:mysql://localhost:3306/ –username root –password 123456

2)连接mysql并列出test数据库中的表
sqoop list-tables –connect jdbc:mysql://localhost:3306/test –username root –password 123456
test为数据库的名字

3)将关系型数据的表结构复制到hive中,只是复制表的结构,表中的内容没有复制过去。
sqoop create-hive-table –connect jdbc:mysql://localhost:3306/test
–table sqoop_test –username root –password 123456 –hive-table
test
其中 –table sqoop_test为mysql中的数据库test中的表 –hive-table
test 为hive中新建的表名称

4)从关系数据库导入文件到hive中
sqoop import –connect jdbc:mysql://localhost:3306/zxtest –username
root –password 123456 –table sqoop_test –hive-import –hive-table
s_test -m 1

5)将hive中的表数据导入到mysql中,在进行导入之前,mysql中的表
hive_test必须已经提起创建好了。
sqoop export –connect jdbc:mysql://localhost:3306/zxtest –username
root –password root –table hive_test –export-dir
/user/hive/warehouse/new_test_partition/dt=2012-03-05

6)从数据库导出表的数据到HDFS上文件
./sqoop import –connect
jdbc:mysql://10.28.168.109:3306/compression –username=hadoop
–password=123456 –table HADOOP_USER_INFO -m 1 –target-dir
/user/test

7)从数据库增量导入表数据到hdfs中
./sqoop import –connect jdbc:mysql://10.28.168.109:3306/compression
–username=hadoop –password=123456 –table HADOOP_USER_INFO -m 1
–target-dir /user/test    –check-column id –incremental append
–last-value 3

四、

Import

sqoop 数据导入具有以下特点:
1.支持文本文件(--as-textfile)、avro( --as-avrodatafile )、SequenceFiles(--as-sequencefile)。  RCFILE暂未支持 ,默认为文本
2.支持数据追加,通过--apend指定
3.支持table列选取(--column),支持数据选取(--where),和--table一起使用
4.支持数据选取,例如读入多表join后的数据'SELECT a.*, b.* FROM a JOIN b on (a.id == b.id) ‘,不可以和--table同时使用
5.支持map数定制(-m)
6.支持压缩(--compress)
7. 支持将关系数据库中的数据导入到Hive(--hive-import)、HBase(--hbase-table)
   数据导入Hive分三步:1)导入数据到HDFS  2)Hive建表  3)使用“LOAD DATA INPAHT”将数据LOAD到表中
   数据导入HBase分二部:1)导入数据到HDFS 2)调用HBase put操作逐行将数据写入表

Export

sqoop export 能将HDFS上的文件导出到关系数据库。其工作原理是根据用户指定的分隔符(字段分隔符:--fields-terminated-by)读入并解析数据,然后转换成insert/update语句导入数据到关系数据库。
其具有以下特点:
1. 支持将数据导出到表(--table)或者调用存储过程(--call)
2. 支持insert、update模式
3. 支持并发控制(-m)


参考原文:
1.http://www.aboutyun.com/thread-6242-1-1.html     Sqoop详细介绍包括:sqoop命令,原理,流程
2.http://blog.csdn.net/yfkiss/article/details/8700480Hadoop            数据传输工具sqoop
3.http://www.open-open.com/lib/view/open1401346410480.html   sqoop的安装与使用
posted @ 2022-07-27 21:10  feitiandamo  阅读(97)  评论(0)    收藏  举报