sqoop 常用命令整理(一)

    xiaoxiao2026-04-18  1

    这些内容是从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 <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 <column-name> 默认是主键,假设有100行数据,它会执行那个SELECT * FROM sometable WHERE id >= lo AND id < hi, with (lo, hi)  会分为4次导入(0,250),(250,500),(500,750),(750,1001) 如果这个字段不能达到实际的划分区域的效果,可以用别的字段。如果没有索引列或者是组合主键的表,需要手动设置一个划分列。 7. --direct 是为了利用某些数据库本身提供的快速导入导出数据的工具,比如mysql的mysqldump性能比jdbc更好,但是不知大对象的列,使用的时候,那些快速导入的工具的客户端必须的shell脚本的目录下。 8.导入数据到hdfs目录,这个命令会把数据写到/shared/foo/ 目录。 sqoop import --connnect <connect-str> --table foo --warehouse-dir /shared \或者 sqoop import --connnect <connect-str> --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 <mapping>  转换为java数据类型   --map-column-hive <mapping>  转转为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 <dir>  重写$HIVE_HOME   --hive-import          插入数据到hive当中,使用hive的默认分隔符   --hive-overwrite  重写插入   --create-hive-table  建表,如果表已经存在,该操作会报错!   --hive-table <table-name>  设置到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 <v>  hive分区的值   --map-column-hive <map>          类型匹配,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 <dir>  Output directory for compiled objects   --class-name <name>  Sets the generated class name. This overrides --package-name. When combined with --jar-file, sets the input class.   --jar-file <file>  Disable code generation; use specified jar   --outdir <dir>  Output directory for generated code   --package-name <name>  Put auto-generated classes in this package   --map-column-java <m>  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
    最新回复(0)