安装好sqoop后,我们来尝试实战下sqoop的些命令吧
注意:在执行导入导出相关命令之前,将mysql,oracle对应的Java数据库驱动包放入到 $Sqoop/lib目录下面.
sqoop主要包含以下功能
1 impor ImportTool 从关系型数据库中导入数据(来自表或者查询语句)到HDFS中
2 export ExportTool 将HDFS中的数据导入到关系型数据库中
3 codegen CodeGenTool 获取数据库中某张表数据生成Java并打成jar包
4 create-hive-table CreateHiveTableTool 创建Hive表
5 eval EvalSqlTool 查看SQL执行结果
6 import-all-tables ImportAllTablesTool 导入某个数据库下所有表到HDFS中
7 job JobTool 任务工具,常常用于增量导入
8 list-databases ListDatabasesTool 列出所有数据库名
9 list-tables ListTablesTool 列出某个数据库下所有表
10 merge MergeTool 合并工具体要求
11 metastore MetastoreTool
12 help HelpTool 查看帮助
13 version VersionTool 查看版本
先来两个实例:
1:将mysql中testdb库中的account表导入到hdfs /sqoop/testdb目录下面
sqoop import --connect jdbc:mysql://192.168.233.130/testdb --username hive --password hive --table account --target-dir /sqoop/testdb
2: 用SQL语句导入到hdfs,where 条件后要拼上$CONDITIONS 从日志看是1=0 ,
sqoop import --connect jdbc:mysql://192.168.233.130/testdb --username hive --password hive --query 'select * from account where uid<100 and $CONDITIONS' -m 1 --target-dir /sqoop/testdb_query
输出日志:
[root@Master ~]# sqoop import --connect jdbc:mysql://192.168.233.130/testdb --username hive --password hive --query 'select uid,username from account where uid<100 and $CONDITIONS' -m 1 --target-dir /sqoop/testdb_query
Warning: /usr/lib/hcatalog does not exist! HCatalog jobs will fail.
Please set $HCAT_HOME to the root of your HCatalog installation.
13/12/05 17:06:36 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. Consider using -P instead.
13/12/05 17:06:36 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
13/12/05 17:06:36 INFO tool.CodeGenTool: Beginning code generation
13/12/05 17:06:37 INFO manager.SqlManager: Executing SQL statement: select uid,username from account where uid<100 and (1 = 0)
13/12/05 17:06:37 INFO manager.SqlManager: Executing SQL statement: select uid,username from account where uid<100 and (1 = 0)
13/12/05 17:06:37 INFO manager.SqlManager: Executing SQL statement: select uid,username from account where uid<100 and (1 = 0)
13/12/05 17:06:37 INFO orm.CompilationManager: HADOOP_MAPRED_HOME is /usr/local/hadoop-1.2.1
Note: /tmp/sqoop-root/compile/4e56ebb65ffd4dbf7fd744c1c1446d89/QueryResult.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
13/12/05 17:06:38 INFO orm.CompilationManager: Writing jar file: /tmp/sqoop-root/compile/4e56ebb65ffd4dbf7fd744c1c1446d89/QueryResult.jar
13/12/05 17:06:38 INFO mapreduce.ImportJobBase: Beginning query import.
13/12/05 17:06:39 INFO mapred.JobClient: Running job: job_201312051330_0007
13/12/05 17:06:40 INFO mapred.JobClient: map 0% reduce 0%
13/12/05 17:06:47 INFO mapred.JobClient: map 100% reduce 0%
13/12/05 17:06:48 INFO mapred.JobClient: Job complete: job_201312051330_0007
13/12/05 17:06:48 INFO mapred.JobClient: Counters: 18
13/12/05 17:06:48 INFO mapred.JobClient: Job Counters
13/12/05 17:06:48 INFO mapred.JobClient: SLOTS_MILLIS_MAPS=5787
13/12/05 17:06:48 INFO mapred.JobClient: Total time spent by all reduces waiting after reserving slots (ms)=0
13/12/05 17:06:48 INFO mapred.JobClient: Total time spent by all maps waiting after reserving slots (ms)=0
13/12/05 17:06:48 INFO mapred.JobClient: Launched map tasks=1
13/12/05 17:06:48 INFO mapred.JobClient: SLOTS_MILLIS_REDUCES=0
13/12/05 17:06:48 INFO mapred.JobClient: File Output Format Counters
13/12/05 17:06:48 INFO mapred.JobClient: Bytes Written=77
13/12/05 17:06:48 INFO mapred.JobClient: FileSystemCounters
13/12/05 17:06:48 INFO mapred.JobClient: HDFS_BYTES_READ=87
13/12/05 17:06:48 INFO mapred.JobClient: FILE_BYTES_WRITTEN=68048
13/12/05 17:06:48 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=77
13/12/05 17:06:48 INFO mapred.JobClient: File Input Format Counters
13/12/05 17:06:48 INFO mapred.JobClient: Bytes Read=0
13/12/05 17:06:48 INFO mapred.JobClient: Map-Reduce Framework
13/12/05 17:06:48 INFO mapred.JobClient: Map input records=4
13/12/05 17:06:48 INFO mapred.JobClient: Physical memory (bytes) snapshot=44548096
13/12/05 17:06:48 INFO mapred.JobClient: Spilled Records=0
13/12/05 17:06:48 INFO mapred.JobClient: CPU time spent (ms)=540
13/12/05 17:06:48 INFO mapred.JobClient: Total committed heap usage (bytes)=15990784
13/12/05 17:06:48 INFO mapred.JobClient: Virtual memory (bytes) snapshot=379265024
13/12/05 17:06:48 INFO mapred.JobClient: Map output records=4
13/12/05 17:06:48 INFO mapred.JobClient: SPLIT_RAW_BYTES=87
13/12/05 17:06:48 INFO mapreduce.ImportJobBase: Transferred 77 bytes in 10.2691 seconds (7.4982 bytes/sec)
13/12/05 17:06:48 INFO mapreduce.ImportJobBase: Retrieved 4 records.
import还是很简单的,只要知道import参数的意义即可.
import命令总结
import命令参数:
1 --connect 连接关系型数据库的URL jdbc:mysql://localhost/testdb
2 --connection-manager 连接管理类,一般不用
3 --driver 连接驱动
4 --hadoop-home hadoop目录 /usr/local/hadoop-1.2.1
5 --help 查看帮助信息
6 --password ** :连接关系型数据库的密码
7 --username username :链接关系型数据库的用户名
8 --verbose :查看更多的信息,其实是将日志级别调低 该参数后面不接值
9 -- query SQL : 按sql语句导入如 : --query 'select uid,username from account where uid<100 and $CONDITIONS'
10 --m n : 分几个线程导入 如: --m 8
11 --split-by : 如果是分多个线程进行,那这个就会自动按总数据分面多个线程进行,如
默认是主键,假设有2000行数据,它会执行那个SELECT * FROM sometable WHERE id >= lo AND id < hi,
with (lo, hi) 会分为2次导入(0,1000),(1000,2000)
12. --direct 是为了利用某些数据库本身提供的快速导入导出数据的工具,比如mysql的mysqldump
性能比jdbc更好,但是不知大对象的列,使用的时候,那些快速导入的工具的客户端必须的shell脚本的目录下
13 --target-dir 目录 :至指定目录
14.转换为对象 --map-column-java 转换为java数据类型
--map-column-hive 转转为hive数据类型
15.追加导入
--check-column (col) Specifies the column to be examined when determining which rows to import.
--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.
增加导入支持两种模式append和lastmodified,用--incremental来指定
例: $ sqoop import --connect jdbc:mysql://192.168.233.130/test --table account
--where "uid < 100000" --target-dir /sqoop/testdata --append
16.在导入大对象,比如BLOB和CLOB列时需要特殊处理,小于16MB的大对象可以和别的数据一起存储,超过这个值就存储在_lobs的子目录当中
它们采用的是为大对象做过优化的存储格式,最大能存储2^63字节的数据,我们可以用--inline-lob-limit参数来指定每个lob文件最大的限制是多少
如果设置为0,则大对象使用外部存储
17.分隔符、转移字符
下面的这句话
Some string, with a comma.
Another "string with quotes"
使用这句命令导入$ sqoop import --fields-terminated-by , --escaped-by \\ --enclosed-by '\"' ...
会有下面这个结果
"Some string, with a comma.","1","2","3"...
"Another \"string with quotes\"","4","5","6"...
使用这句命令导入$ sqoop import --optionally-enclosed-by '\"' (the rest as above)...
"Some string, with a comma.",1,2,3...
"Another \"string with quotes\"",4,5,6...
$ sqoop import --connect jdbc:mysql://192.168.233.130/testdb --table EMPLOYEES \
--fields-terminated-by '\t' --lines-terminated-by '\n' \
--optionally-enclosed-by '\"'
18.hive导入参数
--hive-home
--hive-import 插入数据到hive当中,使用hive的默认分隔符
--hive-overwrite 重写插入
--create-hive-table 建表,如果表已经存在,该操作会报错!
--hive-table 设置到hive当中的表名
--hive-drop-import-delims 导入到hive时删除 \n, \r, and \01
--hive-delims-replacement 导入到hive时用自定义的字符替换掉 \n, \r, and \01
--hive-partition-key hive分区的key
--hive-partition-value hive分区的值
--map-column-hive 类型匹配,sql类型对应到hive类型
19.hive空值处理
sqoop会自动把NULL转换为null处理,但是hive中默认是把\N来表示null,因为预先处理不会生效的
我们需要使用 --null-string 和 --null-non-string来处理空值 把\N转为\\N
sqoop import ... --null-string '\\N' --null-non-string '\\N'
20.导入数据到hbase
导入的时候加上--hbase-table,它就会把内容导入到hbase当中,默认是用主键作为split列
也可以用--hbase-row-key来指定,列族用--column-family来指定,它不支持--direct。
如果不想手动建表或者列族,就用--hbase-create-table参数
例如:
sqoop import --connect jdbc:mysql://mysqlserver_IP/databaseName --table
datatable --hbase-create-table --hbase-table hbase_tablename --columnfamily col_fam_name --hbase-row-key key_col_name
其中 ,databaseName 和datatable 是mysql的数据库和表名,hbase_tablename是
要导成hbase的表名,key_col_name可以指定datatable中哪一列作 为hbase新表的
rowkey,col_fam_name是除rowkey之外的所有列的列族名
21.代码生成参数,没看懂
--bindir
--class-name Sets the generated class name. This overrides --package-name. When combined with --jar-file, sets the input class.
--jar-file Disable code generation; use specified jar
--outdir
--package-name Put auto-generated classes in this package
--map-column-java Override default mapping from SQL type to Java type for configured columns.
22.通过配置文件conf/sqoop-site.xml来配置常用参数
property.name
property.value
如果不在这里面配置的话,就需要像这样写命令
sqoop import -D property.name=property.value ...
23.两个特别的参数
sqoop.bigdecimal.format.string 大decimal是否保存为string,如果保存为string就是 0.0000007,否则则为1E7
sqoop.hbase.add.row.key 是否把作为rowkey的列也加到行数据当中,默认是false的
24: --columns "employee_id,first_name,last_name,job_title" 指定列
25:使用sequencefile作为存储方式
$ sqoop import --connect jdbc:mysql://192.168.233.130/testdb --table EMPLOYEES \
--class-name com.foocorp.Employee --as-sequencefile
25.导入所有的表sqoop-import-all-tables
每个表都要有主键,不能使用where条件过滤
sqoop import-all-tables --connect jdbc:mysql://192.168.233.130/testdb