Hive 外部表 分区表,hive外部表分区表


  之前主要研究oracle与mysql,觉得hive其实就是一种数据仓库的框架,也没有太多另类,所以主要精力都在研究hadoop,hbase,sqoop,mahout,最近稍微用心看了下hive,其实hive还是比我想象中好用的多,心里有点点暗爽,不论是与hadoop的衔接,还是在对外查询分析,定期hsql生成报表方面,都非常方便,可以不用mapreduce,直接用hive生成报表。真是方便。



 Hive 提供两者表的两种使用方式,一种是内部表(托管表),另外一种就是外部表。



对于两种表的使用场景,最主要的区别就是在于数据的存储位置,如果你没有只是使用hive来调用者部分数据,那索性就用内部表。如果还有其他任务共享这批数据的话,那就用外部表,目前我就是这么来做划分。但我更倾向于使用外部表,因为我更在意一种特性就是灵活的拓展性!外部表可以使用以后可能发生的数据使用上的变更。我一般都使用外部表方式。
外部表的分区:
以下就一个简单的案例来说一下外部表如果进行分区使用:
先上一下数据样本:
渠道 区服  角色名 唯一标识 等级  国家 vip等级 UID 注册时间 最后登录时间 剩余充值金币  剩余系统金币 充值金消耗 系统金消耗 粮食消耗 镔铁消耗 银币消耗 点券剩余数量  日期
178 XXX_260 凯旋 178_263_1809 33 3 0 34191904 2014-12-05 15:33:23 2014-12-05 17:18:58225 0 0 0 0 0 0 2015-06-07
178 XXX_262 凯旋 178_264_801 33 2 0 34191904 2014-12-06 12:13:03 2014-12-06 14:12:51237 0 0 0 0 0 0 2015-06-07
178 XXX_274 【痴货】 178_276_2131 11 1 0 34308396 2014-12-18 15:39:34 2014-12-18 21:28:38 0 100 0 0 0 0 0 0 2015-06-07
178 XXX_286 七雨1 178_288_859 23 2 0 34016769 2014-12-30 12:46:18 2014-12-30 19:27:21100 0 0 0 0 0 0 2015-06-07
178 XXX_323 丿无极灬南蕾 178_324_1780 29 2 0 34645311 2015-02-04 13:42:59 2015-02-04 20:18:12 0 120 0 0 0 0 0 0 2015-06-07



创建数据库:
create database gcld;
use gcld;





创建分区外部表(按照天进行分区):
create external table gcldlog(coopid string,zoneid string,rolename string,uniquecode string,level string,country string,viplevel string,uid  string,regdate string,lastlogintime string,remain_reg_coin  bigint,remain_sys_coin bigint,cost_reg_coin bigint,cost_sys_coin bigint,cost_food bigint,cost_iron bigint,cost_silvercoin bigint,remain_coupon  bigint,datetime string) partitioned by (dt string)
row format delimited 
   fields  terminated by '\t'
   collection items terminated by '\002'  map keys terminated by  '\003'
   lines terminated by '\n' stored as textfile
  location  '/source/gcld/';





给外部分区表增加分区:
alter table gcldlog add partition (dt='2015-06-07')  location '2015-06-07';
alter table gcldlog add partition (dt='2015-06-08')  location '2015-06-08';
alter table gcldlog add partition (dt='2015-06-09')  location '2015-06-09';


往HDFS上传数据,hdfs目录结构应该如下:
/source/gcld/2015-06-07/xxx.log
/source/gcld/2015-06-08/xxx.log
/source/gcld/2015-06-09/xxx.log





创建分区对应的hdfs目录:
hadoop fs -mkdir -p /source/gcld/2015-06-07
hadoop fs -mkdir -p /source/gcld/2015-06-08
hadoop fs -mkdir -p /source/gcld/2015-06-09
将文件上传至hdfs目录:
hadoop  fs -put      local_log_dir/2015-06-07.log /source/gcld/2015-06-07/
hadoop  fs -put      local_log_dir/2015-06-08.log /source/gcld/2015-06-08/
hadoop  fs -put      local_log_dir/2015-06-09.log /source/gcld/2015-06-09/





数据查询(使用分区方式查询):
Logging initialized using configuration in file:/etc/hive/conf.dist/hive-log4j.properties
hive> show databases;
OK
default
gcld
opensdkdb
tmp
Time taken: 1.754 seconds, Fetched: 4 row(s)
hive> use gcld;
OK
Time taken: 0.126 seconds
hive> show tables;
OK
gcldlog
Time taken: 0.114 seconds, Fetched: 1 row(s)
hive> select * from gcldlog t where t.dt='2015-06-08' limit 10;
OK
07073sy xxxx_220      叛逆者  07073sy_228_25641       37      3       0       140958  2015-04-30 09:08:54     2015-05-19 19:27:30590      0       0       0       0       0       0       2015-06-08      2015-06-08
07073sy xxxx_268      平台推广        07073sy_268_7017        18      1       0       13480   2014-12-27 16:38:51     2014-12-29 11:48:21 0       85      0       0       0       0       0       0       2015-06-08      2015-06-08</span>
07073sy xxxx_274      帝一    07073sy_276_147 6       2       0       6037    2014-12-18 10:59:35     2014-12-18 10:59:35     0  150      0       0       0       0       0       0       2015-06-08      2015-06-08</span>
07073sy xxxx_274      ?青春℡ 07073sy_276_7459        22      2       0       10297   2014-12-19 09:01:54     2014-12-19 11:44:40100      0       0       0       0       0       0       2015-06-08      2015-06-08
07073sy xxxx_274      将军    07073sy_276_8065        2       2       0       11287   2014-12-19 20:01:06     2014-12-19 20:01:06100      0       0       0       0       0       0       2015-06-08      2015-06-08
07073sy xxxx_275      苏小梵丶        07073sy_277_226 18      2       0       10808   2014-12-19 11:07:14     2014-12-19 11:07:14100      0       0       0       0       0       0       2015-06-08      2015-06-08
07073sy xxxx_275      宁小之  07073sy_277_571 6       3       0       11132   2014-12-19 11:48:46     2014-12-19 11:48:46     0  150      0       0       0       0       0       0       2015-06-08      2015-06-08
07073sy xxxx_278      阿杰    07073sy_278_7297        36      1       1       11885   2014-12-21 01:54:01     2014-12-21 11:57:222015-06-08       2015-06-08
07073sy xxxx_273      恩恩额  07073sy_279_2403        18      3       0       12094   2014-12-21 15:21:43     2014-12-22 17:50:17100      0       0       0       0       0       0       2015-06-08      2015-06-08
07073sy xxxx_273      诸葛,无敌      07073sy_279_3152        9       1       0       12137   2014-12-21 17:00:53     2014-12-21 19:09:52 0       150     0       0       0       0       0       0       2015-06-08      2015-06-08
Time taken: 2.428 seconds, Fetched: 10 row(s)














相关内容