利用Sqoop从HDFS导出数据到DB

本文介绍了如何利用Sqoop将HDFS中的数据导出到关系型数据库管理系统(RDBMS),包括命令语法、参数详解以及如何支持插入、更新和更新或插入操作。详细阐述了导出过程中的关键参数配置,如JDBC连接、表映射、数据分隔符等,以确保高效、准确的数据迁移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求
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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值