Hive创建分区表

如下
1 在hive上创建一个外部表,四个字段,ip地址,项目名称,ctime 创建时间, content 类型为struct.struct可以理解为hive的自定义类型,格式为 struct<key:type,key:type,key,type>. 本例中content类型为struct.其中又嵌套了一个struct类型的字段properties
2PARTITIONED BY(logday string) 指定分区字段,这个名称logday可以自定义.此时表中并没有分区.因为还没有导入数据.
3LOCATION '/sources/news/'; 指定表原始文件的位置

CREATE EXTERNAL TABLE if not exists news1 (
    ip string,
    project string,
    ctime string,
    content struct<distinct_id:string,event:string,properties:struct<model:string,network_type:string,is_charging:string,app_version:string,element_name:string,element_page:string,carrier:string,os:string,imei:string,battery_level:string,screen_width:string,screen_height:string,device_id:string,client_time:string,ip:string,manufacturer:string>>
)
PARTITIONED BY(logday string)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
LOCATION '/sources/news/';

查看表结构

hive (ods_db_news)> desc news1;
OK
ip                      string                  from deserializer
project                 string                  from deserializer
ctime                   string                  from deserializer
content                 struct<distinct_id:string,event:string,properties:struct<model:string,network_type:string,is_charging:string,app_version:string,element_name:string,element_page:string,carrier:string,os:string,imei:string,battery_level:string,screen_width:string,screen_height:string,device_id:string,client_time:string,ip:string,manufacturer:string>>      from deserializer
logday                  string

表创建完成,需要向表中导入数据,由于是分区表,不能用load into方法.

hive (ods_db_news)> show partitions news1;
OK
hive (ods_db_news)> alter table ods_db_news.news1 drop if exists PARTITION(logday='20201103');
OK
hive (ods_db_news)> alter table ods_db_news.news1 add partition (logday='20201103') location 'hdfs://mypc01:8020/sources/news/20201103/';
OK
hive (ods_db_news)> show partitions news1;
OK
logday=20201103
hive (ods_db_news)> select * from news1 limit 5;
OK
xx.1x6.208.130  news    1604402197754   {"distinct_id":"4291","event":"AppPageView","properties":{"model":"iPad Air2","network_type":"","is_charging":"","app_version":"1.2","element_name":"","element_page":"我的","carrier":"中国电信","os":"Windows","imei":"673172260816","battery_level":"7","screen_width":"1024","screen_height":"320","device_id":"TAIJIXXXXA46DE0A18EB8","client_time":"2020-11-03 19:16:17","ip":"36.63.118.197","manufacturer":"Apple"}}      20201103

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值