需求
目标
语法
hadoop分析完的结果数据最终要呈现给用户,其中一些结果可能需要导入DB。
这里大部分是insert操作,但是有些时候可能还需要update。最好是如果存在就update,不存在就insert。
目标
将一系列HDFS中的文件导出到RDBMS。这里要求RDBMS的表必须提前创建。
文件中的数据通过指定的分割符被解析为表中的记录。
支持insert、update和insertOrUpdate
语法
* 命令
sqoop-export或sqoop export
* 通用参数
--connect <jdbc-uri> 指定JDBC链接串
--connection-manager <class-name> 指定使用的connection manager class
--driver <class-name> 手动指定用户的JDBC driver class
-P 从console读入连接DB的password
--password <password> 设置连接DB的password
--username <username> 设置连接DB的username
--connection-param-file <filename> 可选的连接DB的properties文件
--verbose 运行时输出更多信息
--hadoop-home <dir> 覆盖$HADOOP_HOME
--help输出用法介绍
* 导出参数
--direct Use direct export fast path
--export-dir <dir> 为了export的HDFS源路径
-m,--num-mappers <n> 使用n个map tasks去并行的执行export,默认为4
--table <table-name> 导入的Table
--update-key <col-name> update时使用的条件列,多个列时用逗号分隔
-update-mode <mode> update时,发现新记录的处理方式,默认updateonly,还可以设置allowinsert
--input-null-string <null-string> The string to be interpreted as null for string columns
--input-null-non-string <null-str> The string to be interpreted as null for non-string columns
--staging-table <staging-table-name> The 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.
--batch Use batch mode for underlying statement execution.
* Input解析参数
--input-enclosed-by <char> Sets a required field encloser
--input-escaped-by <char> Sets the input escape character
--input-fields-terminated-by <char> Sets the input field separator
--input-lines-terminated-by <char> Sets the input end-of-line character
--input-optionally-enclosed-by <char> Sets a field enclosing character
* 输出行格式参数
--enclosed-by <char> Sets a required field enclosing character
--escaped-by <char> Sets the escape character
--fields-terminated-by <char> Sets the field separator character
--lines-terminated-by <char> Sets the end-of-line character
--mysql-delimiters Uses MySQL’s default delimiter set: fields: , lines: \n escaped-by: \ optionally-enclosed-by: '
--optionally-enclosed-by <char> Sets a field enclosing character
* 代码生成参数
--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.
最佳示例
sqoop export --connect jdbc:oracle:thin:@xxx:1521:xxx --username xxx --password xxx\
--export-dir /user/hadoop/stat/*\
-m 1 \
--table NS_SLLOG_CLIENT_SUM_OS \
--update-key ACTIVITYTIME,CLIENT_VER,OS \
--update-mode allowinsert \
--input-fields-terminated-by '\t' \
--outdir jobs \
--package-name com.xxxx.sqoop