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)
Hive 外部表 分区表的更多相关文章
- HIVE外部表 分区表
HIVE外部表 分区表 外部表 创建hive表,经过检查发现TBLS表中,hive表的类型为MANAGED_TABLE. 在真实开发中,很可能在hdfs中已经有了数据,希望通过hi ...
- Hive 外部表的练习(多表关联查询,以及分组,子查询)
Hive 外部表的练习 hive创建库和表操作 hive加载数据,4种发放 1.数据放在Linux创建表结构 进行加载 2.先定义表结构.将一个查询select语句结果插入到表中 3.数据先放在hdf ...
- hive外部表删除遇到的一个坑
hive外部表删除遇到的一个坑 操作步骤 创建某个表(create external table xxx location xxx) 插入数据(insert xxx select xxx from x ...
- hive外部表的建立与数据匹配
1.建立hive的外部表匹配hdfs上的数据 出现如下报错: hive (solar)> ; OK Failed with exception java.io.IOException:java. ...
- Hive 外部表新增字段或者修改字段类型等不生效
标题比较笼统,实际情况是: 对于Hive 的分区外部表的已有分区,在对表新增或者修改字段后,相关分区不生效. 原因是:表元数据虽然修改成功,但是分区也会对应列的元数据,这个地方不会随表的元数据修改而修 ...
- hive 外部表和内部表的区别和相互转换
Hive内部表和外部表区别 1.创建内部表时,内部表的数据文件是保存在指定的路径的:如若创建外部表,则只记录数据所在的路径,不会对数据位置做改变. 2.删除表的时候,内部表元数据和数据会跟着一起删除. ...
- hive -- 外部表、内部表、临时表
1.外部表 关键字:EXTERNAL 外部表创建时需要指定LOCATION 删除外部表时,数据不被删除 CREATE EXTERNAL TABLE page_view(viewTime INT, us ...
- hive外部表
创建外部表.数据从HDFS获取 只是建立了链接,hdfs中的数据丢失,表中数据也丢失;hdfs数据增加,表中数据也增加 上传文件 创建外部表 删除文件 执行查询语句,发现少了
- hive学习笔记之三:内部表和外部表
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
随机推荐
- NFS基础优化
一.环境 环境接上篇 https://www.cnblogs.com/suffergtf/p/9486250.html 二.参数详解 1.nfsserver端配置参数详解 [root@nfsserve ...
- 2018年,最经典的26个JavaScript面试题和答案!
根据 Stack Overflow 的 2018 年度调查,JavaScript 连续六年成为最常用的编程语言.所以我们必须面对这样的现实,JavaScript 已经成为全栈开发技能的基石,在全栈开发 ...
- CSS3-::selection
“::selection”只能设置两个属性,一个就是background,另一个就是color属性,设置其他属性是没有任何效果的. ::selection使用语法: /*Webkit,Opera9.5 ...
- LeetCode(109) Convert Sorted List to Binary Search Tree
题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height b ...
- shell脚本举例
1.有时在写一些以循环方式运行的监控脚本,设置时间间隔是必不可少的,下面是一个Shell进度条的脚本演示在脚本中生成延时. #!/bin/bash b='' for ((i=0;$i<=100; ...
- docker+Battery Historian 环境搭建(电量分析)
docker 安装(windows) 1. 下载 https://docs.docker.com/docker-for-windows/install/ 和 安装和添加环境变量(...) 2. 安 ...
- 用btrace定位问题
btrace 截取方法出入参 用btrace的手法,网上有很多.简单记录下我自己的使用经历. btrace脚本: import static com.sun.btrace.BTraceUtils.ex ...
- iOS WKWebView
Webkit 是 iOS 8.0 后提供的新的框架,组件WKWebView比较UIWebView 速度更快.占用内存更少了,可支持性更多 WKWebView可通过KVO监听属性 title.estim ...
- BZOJ 2242 [SDOI2011]计算器 ——EXGCD/快速幂/BSGS
三合一的题目. exgcd不解释,快速幂不解释. BSGS采用了一种不用写EXGCD的方法,写起来感觉好了很多. 比较坑,没给BSGS的样例(LAJI) #include <map> #i ...
- SPOJ CIRU The area of the union of circles ——Simpson积分
[题目分析] 圆的面积并. 直接Simpson积分,(但是有计算几何的解法,留着flag). simpson积分,如果圆出现了不连续的情况,是很容易出事情的.(脑补一下) 但是没有什么办法,本来就是一 ...