9(14)7.3Sqoop导出脚本14

7.3Sqoop导出脚本
1)编写Sqoop导出脚本
在/home/atguigu/bin目录下创建脚本sqoop_export.sh
[atguigu@hadoop102 bin]$ vim sqoop_export.sh
在脚本中填写如下内容
#!/bin/bash

db_name=gmall

export_data() {

case 1 i n " a d s u v c o u n t " ) e x p o r t d a t a " a d s u v c o u n t " ; ; " a d s u s e r a c t i o n c o n v e r t d a y " ) e x p o r t d a t a " a d s u s e r a c t i o n c o n v e r t d a y " ; ; " a d s g m v s u m d a y " ) e x p o r t d a t a " a d s g m v s u m d a y " ; ; " a l l " ) e x p o r t d a t a " a d s u v c o u n t " e x p o r t d a t a " a d s u s e r a c t i o n c o n v e r t d a y " e x p o r t d a t a " a d s g m v s u m d a y " ; ; e s a c 关 于 导 出 u p d a t e 还 是 i n s e r t 的 问 题 Ø − − u p d a t e − m o d e : u p d a t e o n l y 只 更 新 , 无 法 插 入 新 数 据 a l l o w i n s e r t 允 许 新 增 Ø − − u p d a t e − k e y : 允 许 更 新 的 情 况 下 , 指 定 哪 些 字 段 匹 配 视 为 同 一 条 数 据 , 进 行 更 新 而 不 增 加 。 多 个 字 段 用 逗 号 分 隔 。 Ø − − i n p u t − n u l l − s t r i n g 和 − − i n p u t − n u l l − n o n − s t r i n g : 分 别 表 示 , 将 字 符 串 列 和 非 字 符 串 列 的 空 串 和 “ n u l l ” 转 换 成 ′ N ′ 。 官 网 地 址 : h t t p : / / s q o o p . a p a c h e . o r g / d o c s / 1.4.6 / S q o o p U s e r G u i d e . h t m l S q o o p w i l l b y d e f a u l t i m p o r t N U L L v a l u e s a s s t r i n g n u l l . H i v e i s h o w e v e r u s i n g s t r i n g N t o d e n o t e N U L L v a l u e s a n d t h e r e f o r e p r e d i c a t e s d e a l i n g w i t h N U L L ( l i k e I S N U L L ) w i l l n o t w o r k c o r r e c t l y . Y o u s h o u l d a p p e n d p a r a m e t e r s − − n u l l − s t r i n g a n d − − n u l l − n o n − s t r i n g i n c a s e o f i m p o r t j o b o r − − i n p u t − n u l l − s t r i n g a n d − − i n p u t − n u l l − n o n − s t r i n g i n c a s e o f a n e x p o r t j o b i f y o u w i s h t o p r o p e r l y p r e s e r v e N U L L v a l u e s . B e c a u s e s q o o p i s u s i n g t h o s e p a r a m e t e r s i n g e n e r a t e d c o d e , y o u n e e d t o p r o p e r l y e s c a p e v a l u e N t o N : H i v e 中 的 N u l l 在 底 层 是 以 “ N ” 来 存 储 , 而 M y S Q L 中 的 N u l l 在 底 层 就 是 N u l l , 为 了 保 证 数 据 两 端 的 一 致 性 。 在 导 出 数 据 时 采 用 − − i n p u t − n u l l − s t r i n g 和 − − i n p u t − n u l l − n o n − s t r i n g 两 个 参 数 。 导 入 数 据 时 采 用 − − n u l l − s t r i n g 和 − − n u l l − n o n − s t r i n g 。 3 ) 执 行 S q o o p 导 出 脚 本 [ a t g u i g u @ h a d o o p 102 b i n ] 1 in "ads_uv_count") export_data "ads_uv_count" ;; "ads_user_action_convert_day") export_data "ads_user_action_convert_day" ;; "ads_gmv_sum_day") export_data "ads_gmv_sum_day" ;; "all") export_data "ads_uv_count" export_data "ads_user_action_convert_day" export_data "ads_gmv_sum_day" ;; esac 关于导出update还是insert的问题 Ø --update-mode: updateonly 只更新,无法插入新数据 allowinsert 允许新增 Ø --update-key:允许更新的情况下,指定哪些字段匹配视为同一条数据,进行更新而不增加。多个字段用逗号分隔。 Ø --input-null-string和--input-null-non-string: 分别表示,将字符串列和非字符串列的空串和“null”转换成'\\N'。 官网地址:http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html Sqoop will by default import NULL values as string null. Hive is however using string \N to denote NULL values and therefore predicates dealing with NULL(like IS NULL) will not work correctly. You should append parameters --null-string and --null-non-string in case of import job or --input-null-string and --input-null-non-string in case of an export job if you wish to properly preserve NULL values. Because sqoop is using those parameters in generated code, you need to properly escape value \N to \\N: Hive中的Null在底层是以“\N”来存储,而MySQL中的Null在底层就是Null,为了保证数据两端的一致性。在导出数据时采用--input-null-string和--input-null-non-string两个参数。导入数据时采用--null-string和--null-non-string。 3)执行Sqoop导出脚本 [atguigu@hadoop102 bin] 1in"adsuvcount")exportdata"adsuvcount";;"adsuseractionconvertday")exportdata"adsuseractionconvertday";;"adsgmvsumday")exportdata"adsgmvsumday";;"all")exportdata"adsuvcount"exportdata"adsuseractionconvertday"exportdata"adsgmvsumday";;esacupdateinsertØupdatemodeupdateonlyallowinsertØupdatekeyØinputnullstringinputnullnonstringnullNhttp://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.htmlSqoopwillbydefaultimportNULLvaluesasstringnull.HiveishoweverusingstringNtodenoteNULLvaluesandthereforepredicatesdealingwithNULL(likeISNULL)willnotworkcorrectly.YoushouldappendparametersnullstringandnullnonstringincaseofimportjoborinputnullstringandinputnullnonstringincaseofanexportjobifyouwishtoproperlypreserveNULLvalues.Becausesqoopisusingthoseparametersingeneratedcode,youneedtoproperlyescapevalueNtoN:HiveNullNMySQLNullNullinputnullstringinputnullnonstringnullstringnullnonstring3Sqoop[atguigu@hadoop102bin] chmod 777 sqoop_export.sh
[atguigu@hadoop102 bin]$ sqoop_export.sh all
4)在MySQL中查看结果
SELECT * FROM ads_uv_count;
SELECT * FROMads_user_retention_day_rate;
SELECT * FROMads_user_action_convert_day;
SELECT * FROMads_gmv_sum_day;
SELECT * FROMads_gmv_sum_province;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值