sqoop,将oracle数据传递到hive,可以直接将表数据覆盖传递,也可以按select传递,这里我用的是select查询的
脚本如下:
1、创建分区表,按c1分区
CREATE TABLE `xx.cc`( `c1` string) PARTITIONED BY(`c2` string);
2、通过sqoop将数据导入临时表中
sqoop import -D oraoop.jdbc.url.verbatim=true \ --hive-import --hive-overwrite \ --connect jdbc:oracle:thin:@ip:port:实例名 --username xx_name --password xx_password \ --query "select c1,c2 from table_name where c1=1 and \$CONDITIONS" \--hive-database hive_database_name --hive-table cc_temp \ --target-dir /user/hive/warehouse/xx.db/cc_temp_target \ --delete-target-dir --num-mappers 1 --fetch-size 5000 --hive-drop-import-delims --null-string '\\N' --null-non-string '\\N'
sqoop说明:
- query里面,必须有where条件,同时必须添加 \$CONDITIONS, \$CONDITIONS在执行时会自动替换位 (1= 0),但是不影响查询
- query表如果不存在,会自动创建表,字段顺序与select的字段相同;如果存在,会根据字段名一一对应
- 非常要注意的,临时表对应的分区字段,必须放再最后面,其他字段顺序是否要求相同,没有尝试(不理解他的原理)
- target-dir 对应的目录,需要当前用户有操作权限,同时,target-dir对应的目录,不可存在,或者对应即将生成的表文件;原因如下
- target-dir是sqoop将oracle导入数据生成的临时文件(是文件,不是目录),如果已存在,则会报错
- sqoop将oracle导入到hive后,会将target-dir删除,如果target-dir对应的是即将生成的表文件,则数据会删除
3、将临时表,导入分区表
set hive.exec.dynamic.partition.mode=nonstrict
insert overwrite table xx.cc partition(c1) select * from xx.cc_temp;
4、出现得问题
使用过程中,出现错误:Cannot insert into target table because column number/types are different,后台异常提示为:node tried to create too many dynamic partitions.The maximum number of dynamic partitions is... 这是因为默认动态分区只能为100个,所以异常了。可以在执行插入数据到分区时,添加参数设置: set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=nonstrict; set hive.exec.max.dynamic.partitions.pernode=10000; set hive.exec.max.dynamic.partitions=10000; set hive.exec.max.created.files=10000; 或者不使用动态分区,改为新增数据,新增时按指定数据分区