Hive导入/导出 : 创建分区表及分区表导入csv文本文件数据

1、csv导入分区表

基本思路:分别创建两个表,一张是分区表,另一张是非分区表,表结构相同;再通过insert语句将非分区表的数据插入到分区表1。

要注意是,分区表的插入分两种:静态插入和动态插入。在一般情况下,Hive不建议直接使用动态插入2,所以有个默认情况下是不允许使用动态分区插入:hive.exec.dynamic.partition=false;但在Hive 0.9.0及之后的版本,上述参数默认为true,虽然如此却有另一个参数约束着动态分区插入:hive.exec.dynamic.partition.mode=strict,动态分区插入的模式默认为严格模式,在严格模式下,插入操作需要指定一个特定的静态分区。所以如果在从非分区表select出来并insert到分区表中,就需要设置这两个参数。

# 创建分区表
create table table_partitioned (col1 string, col2 int) 
  partitioned by (month int) 
  row format delimited fields terminated by ',' lines terminated by '\n' 
  stored as textfile;
# 创建非分区表
create table table_unpartitioned (col1 string, col2 int, month int) 
  row format delimited fields terminated by ',' lines terminated by '\n' 
  stored as textfile;
# 导入csv文本文件
load data local inpath '/path/to/file' into table table_unpartitioned;
# 设置允许动态分区插入,并动态分区插入模式为非严格模式
SET hive.exec.dynamic.partition = true;
SET hive.exec.dynamic.partition.mode = nonstrict;
# 插入到分区表
insert into table table_partitioned partition(month) select * from table_unpartitioned;

2、hive导出csv

hive中在创建表时,一般会根据导入的数据格式来指定字段分隔符和列分隔符。一般导入的文本数据字段分隔符多为逗号分隔符或者制表符(但是实际开发中一般不用着这种容易在文本内容中出现的的符号作为分隔符),当然也有一些别的分隔符,也可以自定义分隔符。有时候也会使用hive默认的分隔符来存储数据。

hive (fdm_sor)> create table fdm_sor.mytest_tmp2(
              >  id int comment'编号',
              >  name string comment '名字'
              >  );
hive (fdm_sor)> show create table mytest_tmp2;
CREATE  TABLE `mytest_tmp2`(
  `id` int COMMENT '编号', 
  `name` string COMMENT '名字')
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' --hive默认的分割方式,即行为\n,列为^A
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat'    --hive默认的存储格式为textfile
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION                                    --内部表的默认的存储路径
  'hdfs://hadoop102:9000/user/hive/warehouse/fdm_sor.db/mytest_tmp2'
TBLPROPERTIES (
  'transient_lastDdlTime'='1526176805')
hive (fdm_sor)> create table  fdm_sor.mytest_tmp3(
              >   id int comment'编号',
              >  name string comment '名字'
              >  )
              >  row format delimited fields terminated by '\001'  --这里可以指定别的分隔符,如‘\t’,'$'等分隔符
              >  lines terminated by '\n'
              >  stored as textfile;
 
hive (fdm_sor)> show create table fdm_sor.mytest_tmp3;
OK
createtab_stmt
CREATE  TABLE `fdm_sor.mytest_tmp3`(
  `id` int COMMENT '编号', 
  `name` string COMMENT '编号')
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '\u0001' 
  LINES TERMINATED BY '\n' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://hadoop102:9000/user/hive/warehouse/fdm_sor.db/mytest_tmp3'
TBLPROPERTIES (
  'transient_lastDdlTime'='1526176859')

如上可以看出hive默认的列分割类型为org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe,而这其实就是A分隔符,hive中默认使用A(ctrl+A)作为列分割符,如果用户需要指定的话,等同于row format delimited fields terminated by ‘\001’,因为^A八进制编码体现为’\001’.所以如果使用默认的分隔符,可以什么都不加,也可以按照上面的指定加‘\001’为列分隔符,效果一样。

hive默认使用的行分隔符是’\n’分隔符 ,也可以加一句:LINES TERMINATED BY ‘\n’ ,加不加效果一样。但是区别是hive可以通过row format delimited fields terminated by '\t’这个语句来指定不同的分隔符,但是hive不能够通过LINES TERMINATED BY '$$'来指定行分隔符,目前为止,hive的默认行分隔符仅支持‘\n’字符。否则报错。

hive (fdm_sor)>  create table  fdm_sor.mytest_tm4(
              >   id int comment'编号',
              >  name string comment '名字'
              >  )
              >  lines terminated by '\t';
FAILED: ParseException line 5:1 missing EOF at 'lines' near ')'

一般来说hive的默认行分隔符都是换行符,如果非要自定义行分隔符的话,可以通过自定义Inputformat和outputformat类来指定特定行分隔符和列分隔符,一般公司实际开发中也都是这么干的,具体使用,见后面博客。
一般来说hive的默认行分隔符都是换行符,如果非要自定义行分隔符的话,可以通过自定义Inputformat和outputformat类来指定特定行分隔符和列分隔符,一般公司实际开发中也都是这么干的,具体使用,见后面博客。
当然如hive中集合数据类型struct ,map,array,也都有默认的字段分隔符,也都可以指定字段分隔符。hive中对于上述三个集合数据类型的默认字段分隔符是^B,八进制体现为‘\002’,用collection items terminated by '\002’语句来指定分隔符,对于map来说,还有键值之间的分割符,可以用map keys terminated by ‘\003’(^C)来指定分隔符。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值