sqoop导入数据到hive

1,984 阅读1分钟

正文:

1. 直接导入到hive,导入命令示例:

    sqoop import --connect jdbc:mysql://xx.xx.xx.xx/cc --username uuu --password xxxxxxxx --query 'select id,`name`,slogan,min_price,max_price from item where $CONDITIONS' --hive-table hhhttt --hive-import --hive-overwrite --hive-drop-import-delims --delete-target-dir --target-dir /hive/2019/10/17/ -m 1

   部分参数释义:

       指定临时目录

       --hive-overwrite 覆盖掉在hive表中已经存在的数据

       --hive-drop-import-delims 把导入数据中包含的hive默认的分隔符去掉

       --delete-target-dir --target-dir 指定临时目录并且在job完成后删除临时目录.sqoop导数据到hive会先将数据导入到HDFS上目录临时存放,然后再将数据load到hive中(此步骤会把临时目录的数据移动到hive的目录[默认/user/hive/warehouse]),最后把这个目录再删除掉.hive的目录结构如图:     


2. 如果某列在mysql中的值为null/"" 该如何处理:

    sqoop import --connect jdbc:mysql://xx.xxx.xxx.xx/xx --username xxx--password xxxxxxxxxxxxxxxxxxxxx --query 'select id,`name`,slogan,min_price,max_price from item where $CONDITIONS' --null-string 'emp' --null-non-string '-1' --target-dir /temp/hivetest/ --hive-table hivetest.item20191017 --hive-import --hive-overwrite --hive-drop-import-delims -m 1

   --null-string 含义是 string类型的字段,当Value是NULL,替换成指定的字符,该例子中为emp

   --null-non-string 含义是非string类型的字段,当Value是NULL,替换成指定字符,该例子中为-1

3.动态分区表使用:

    3.1 创建动态分区表

          drop table if exists hivetest.item_p; 

           create table hivetest.item_p( 

                     id int, 

                     name string, 

                     slogan string, 

                     min_price string, 

                     max_price string ) 

          partitioned by(modify_date string

          location 'hdfs://192.168.0.136:9000/user/hive/warehouse/hivetest/item_p';

    3.2 导入数据到动态分区表

          sqoop import --connect jdbc:mysql://xx.xxx.xxx.xx/xx --username xxx--password ccc  --query 'select id,`name`,slogan,min_price,max_price,date(modify_time) modify_date from item where $CONDITIONS' --null-string 'empStr' --null-non-string '-1' --delete-target-dir --target-dir /temp/hivetest/ --hive-table hivetest.item_p --hive-import --hive-overwrite --hive-drop-import-delims -m 1

         任务完成后,分区表文件目录如图:

    3.3 查询动态/静态分区表 

     select * from item_p where modify_date='2019-10-15';

   3.4 静态分区与动态分区

         3.4.1 静态分区使用

                  创建静态分区

                   sqoop导入时 指定参数:

                                         --hive-partition-key ods_date 

                                         --hive-partition-value 2019-10-24

         3.4.2 动态分区(遇到坑了)

                  开启hive动态分区

                                         hive-site.xml添加如下配置:

                                                <property>                                                       <name>hive.exec.dynamic.partition</name> 

<value>true</value> 

                                                </property> 

                                                <property> 

<name>hive.exec.dynamic.partition.mode</name> 

<value>nonstrict</value> 

                                                </property> 

                创建分区表  按列分区,注意分区列不能包含在表中  create(c1 int,c2 string) partitioned by (列名,类型);

                sqoop 导入(示例见 3),注意分区列为 --query 的sql中最后一列 ,sql中列的顺序与表一致

坑:

    作者在本地开环境的hive(3.1.2)  按照上面的动态分区使用步骤没有问题 

    生产环境使用按列动态分区则出现错误,生产环境(cdh6.3) 一直抛异常

           Need to specify partition columns because the destination table is partitioned

    在生产环境尝试开启/关闭动态分区 但异常信息都没有变化。。。

    有类似经历的大佬 还请不吝赐教。

   4.sqoop 增量导入 hive (hive 不支持 sqoop lastmodified模式 )

      注意: 慎用 --hive-overwrite 参数 =>清空表中已存在的所有数据

      示例1:  sqoop import --connect jdbc:mysql://xxx.ccc.vvv.bbb:3306/item --username bbb --password mmmmmm --table item --incremental append --check-column modify_time --last-value '2019-10-25 17:40:00' --null-string /n --null-non-string 0 --hive-import --hive-drop-import-delims --target-dir /hive/ods/item/ --hive-table ods.item -m 1 

      示例2:  sqoop import --connect jdbc:mysql://xxx.ccc.vvv.bbb:3306/item --username bbb --password mmmmmm --query 'select id,version,name,images,state,slogan,start_time,end_time,favorites,category_code,min_price,max_price,fk_merchant_id,solds,actual_solds,recommend,fk_goods_id,create_time,modify_time from item where $CONDITIONS' --incremental append --check-column modify_time --last-value '2019-10-25 17:40:00' --null-string /n --null-non-string 0 --hive-import --hive-drop-import-delims --target-dir /hive/ods/item/ --hive-table ods.item -m 1  

正文结束.

不当之处,还请批评指正.