id="cproIframe_u1728839_3" width="120" height="250" src="http://pos.baidu.com/acom?adn=4&adp=1&at=0&aurl=&c01=1&cad=1&ccd=24&cec=GBK&cfv=13&ch=0&col=zh-CN&conBW=1&conOP=0&cpa=1&dai=3&dis=0&layout_filter=rank%2Cimage<r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3Dm81gT3lFlwU4l8AiZP4nJWgV5iQCJmC0muHE6OXk4pRTNIrBxnDPjGee5gDjkbwCM7gJ5yXFvqHBzCwMiTJMLK%26wd%3D%26eqid%3Dc6c0c7170001210300000003559a1915<u=http%3A%2F%2Fwww.aboutyun.com%2Fthread-9983-1-1.html&lunum=6&n=92051019_cpr&pat=6&pcs=1423x775&pih=0&pis=10000x10000&piw=0&ps=605x1170&psr=1440x900&pss=1423x942&ptbg=90&ptp=0&ptt=0&qn=637d2287a3f20718&rad=&rsi0=120&rsi1=250&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%23000000&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=tabcloud_skin_3&stid=5&td_id=1728839&titFF=%25E5%25BE%25AE%25E8%25BD%25AF%25E9%259B%2585%25E9%25BB%2591&titFS=14&titSU=0&tn=baiduCustNativeAD&tpr=1436162356857&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1728839&ti=sqoop%20%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E6%95%B4%E7%90%86-Sqoop-about%E4%BA%91%E5%BC%80%E5%8F%91&tt=1436162356808.3881.3920.3920" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="word-wrap: break-word;">
本帖最后由 pig2 于 2014-11-13 08:34 编辑 问题导读 1.sqoop是否支持复杂语句 2.sqoop如果存在就更新,不存在就插入,这是什么模式?  这些内容是从sqoop的官网整理出来的,是1.4.3版本的Document,如果有错误,希望大家指正。 1.使用sqoop导入数据
- sqoop import --connect jdbc:mysql://localhost/db --username foo --table TEST
复制代码
2.账号密码
- sqoop import --connect jdbc:mysql://database.example.com/employees \
- --username aaron --password 12345
复制代码
3.驱动
- sqoop import --driver com.microsoft.jdbc.sqlserver.SQLServerDriver \
- --connect string> ...
复制代码
4.写sql语句导入的方式
- sqoop import \
- --query 'SELECT a.*, b.* FROM a JOIN b on (a.id == b.id) WHERE $CONDITIONS' \
- --split-by a.id --target-dir /user/foo/joinresults
复制代码
如果是顺序导入的话,可以只开一个线程
- sqoop import \
- --query 'SELECT a.*, b.* FROM a JOIN b on (a.id == b.id) WHERE $CONDITIONS' \
- -m 1 --target-dir /user/foo/joinresults
复制代码
如果where语句中有要用单引号的,就像这样子写就可以啦"SELECT * FROM x WHERE a='foo' AND \$CONDITIONS" 5. 1.4.3版本的sqoop不支持复杂的sql语句,不支持or语句 6. --split-by 默认是主键,假设有100行数据,它会执行那个SELECT * FROM sometable WHERE id >= lo AND id 7. --direct 是为了利用某些数据库本身提供的快速导入导出数据的工具,比如mysql的mysqldump 性能比jdbc更好,但是不知大对象的列,使用的时候,那些快速导入的工具的客户端必须的shell脚本的目录下 8.导入数据到hdfs目录,这个命令会把数据写到/shared/foo/ 目录
- sqoop import --connnect --table foo --warehouse-dir /shared \
复制代码
或者
- sqoop import --connnect --table foo --target-dir /dest \
复制代码
9.传递参数给快速导入的工具,使用--开头,下面这句命令传递给mysql默认的字符集是latin1
- sqoop import --connect jdbc:mysql://server.foo.com/db --table bar \
- --direct -- --default-character-set=latin1
复制代码
10.转换为对象
- --map-column-java 转换为java数据类型
- --map-column-hive 转转为hive数据类型
复制代码
11.增加导入
- --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来指定 12.在导入大对象 比如BLOB和CLOB列时需要特殊处理,小于16MB的大对象可以和别的数据一起存储,超过这个值就存储在_lobs的子目录当中 它们采用的是为大对象做过优化的存储格式,最大能存储2^63字节的数据,我们可以用--inline-lob-limit参数来指定每个lob文件最大的限制是多少 如果设置为0,则大对象使用外部存储 13.分隔符、转移字符 下面的这句话 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... 14.hive导入参数 --hive-home 重写$HIVE_HOME --hive-import 插入数据到hive当中,使用hive的默认分隔符 --hive-overwrite 重写插入 --create-hive-table 建表,如果表已经存在,该操作会报错! --hive-table [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类型 15.hive空值处理 sqoop会自动把NULL转换为null处理,但是hive中默认是把\N来表示null,因为预先处理不会生效的 我们需要使用 --null-string 和 --null-non-string来处理空值 把\N转为\\N
- sqoop import ... --null-string '\\N' --null-non-string '\\N'
复制代码
16.导入数据到hbase 导入的时候加上--hbase-table,它就会把内容导入到hbase当中,默认是用主键作为split列 也可以用--hbase-row-key来指定,列族用--column-family来指定,它不支持--direct。 如果不想手动建表或者列族,就用--hbase-create-table参数 17.代码生成参数,没看懂 --bindir Output directory for compiled objects --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 Output directory for generated code --package-name Put auto-generated classes in this package --map-column-java Override default mapping from SQL type to Java type for configured columns. 18.通过配置文件conf/sqoop-site.xml来配置常用参数
- <property>
- <name>property.name</name>
- <value>property.value</value>
- </property>
复制代码
如果不在这里面配置的话,就需要像这样写命令
- sqoop import -D property.name=property.value ...
复制代码
19.两个特别的参数 sqoop.bigdecimal.format.string 大decimal是否保存为string,如果保存为string就是 0.0000007,否则则为1E7 sqoop.hbase.add.row.key 是否把作为rowkey的列也加到行数据当中,默认是false的 20.例子
- #指定列
- $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
- --columns "employee_id,first_name,last_name,job_title"
- #使用8个线程
- $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
- -m 8
- #快速模式
- $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
- --direct
- #使用sequencefile作为存储方式
- $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
- --class-name com.foocorp.Employee --as-sequencefile
- #分隔符
- $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
- --fields-terminated-by '\t' --lines-terminated-by '\n' \
- --optionally-enclosed-by '\"'
- #导入到hive
- $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
- --hive-import
- #条件过滤
- $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
- --where "start_date > '2010-01-01'"
- #用dept_id作为分个字段
- $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
- --split-by dept_id
- #追加导入
- $ sqoop import --connect jdbc:mysql://db.foo.com/somedb --table sometable \
- --where "id > 100000" --target-dir /incremental_dataset --append
复制代码
21.导入所有的表sqoop-import-all-tables 每个表都要有主键,不能使用where条件过滤
- sqoop import-all-tables --connect jdbc:mysql://db.foo.com/corp
-
复制代码
22.export 我们采用sqoop-export插入数据的时候,如果数据已经存在了,插入会失败 如果我们使用--update-key,它会认为每个数据都是更新,比如我们使用下面这条语句
- sqoop-export --table foo --update-key id --export-dir /path/to/data --connect …
- 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;
- ...
复制代码
这样即使找不到它也不会报错 23.如果存在就更新,不存在就插入 加上这个参数就可以啦--update-mode allowinsert 24.事务的处理 它会一次statement插入100条数据,然后每100个statement提交一次,所以一次就会提交10000条数据 25.例子
- $ sqoop export --connect jdbc:mysql://db.example.com/foo --table bar \
- --export-dir /results/bar_data
- $ sqoop export --connect jdbc:mysql://db.example.com/foo --table bar \
- --export-dir /results/bar_data --validate
- $ sqoop export --connect jdbc:mysql://db.example.com/foo --call barproc \
- --export-dir /results/bar_data
复制代码
|