hive中外部表、内部表、分区表、分桶表

外部表

创建数据库
create database myhive;
选择数据库
use myhive;
创建外部表 ( external)
create external table techer (t_id string,t_name string) row format delimited fields terminated by ‘\t’;
加载数据 ( /export/servers/hivedatas/techer .csv 数据在虚拟机上地址)
load data local inpath ‘/export/servers/hivedatas/techer .csv’ into table techer ;
在hdfs查看表中的数据 /user/hive/warehouse/myhive.db/techer 数据在hdfs上的地址
hadoop fs -ls /user/hive/warehouse/myhive.db/techer
在hive中查询
select * from techer
删除数据表techer
drop table techer;
再次查看
hadoop fs -ls /user/hive/warehouse/myhive.db/techer(数据依然存在)

内部表

创建数据库 create database myhive; 选择数据库 use myhive; 创建内部表

create table student(t_id string,t_name string) row format delimited fields terminated by ‘\t’;
加载数据 ( /export/servers/hivedatas/student .csv 数据在虚拟机上地址)
load data local inpath ‘/export/servers/hivedatas/student .csv’ into table student;
在hdfs查看表中的数据 ( /user/hive/warehouse/myhive.db/student 数据在hdfs上的地址)
hadoop fs -ls /user/hive/warehouse/myhive.db/student
在hive中查询
select * from student
删除数据表techer
drop table student;
再次查看
hadoop fs -ls /user/hive/warehouse/myhive.db/student(数据不存在)

分区表

企业常见的分区规则:按天进行分区(一天一个分区)
创建数据库
create database myhive;
选择数据库
use myhive;
创建分区表的语句 ( partitioned by (分区名 分区类型)
create table score(s_id string,c_id string,s_score int) partitioned by (month string) row format delimited
fieldsterminated by ‘\t’;
create table score2 (s_id string,c_id string,s_score int) partitioned by (year string,month string,day string) row formatdelimited fields terminated by ‘\t’;
数据加载
load data local inpath ‘/opt/hive/score.csv’ into table score partition (month=‘201806’);
load data local inpath ‘/opt/hive/score.csv’ into table score2 partition(year=‘2018’,month=‘06’,day=‘02’);
特别强调:分区字段绝对不能出现在数据表以有的字段中。
作用:
将数据按区域划分开,查询时不用扫描无关的数据,加快查询速度。

分桶表

是在已有的表结构之上新添加了特殊的结构
开启hive的桶表功能
set hive.enforce.bucketing=true;
设置桶(reduce)的个数
set mapreduce.job.reduces=3;
创建数据库
create database myhive;
选择数据库
use myhive;
建分桶表 (clustered by(c_id))
create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format
delimited fields terminated by ‘\t’;
创建基本表
create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by
‘\t’;
基本表添加数据
load data local inpath ‘/export/servers/hivedatas/course.csv’ into table course_common;
在基本表中查询数据插入到分桶表
insert overwrite table course select * from course_common cluster by(c_id);
确认分桶内的数据
[root@node01 hive]# hadoop fs -cat /user/hive/warehouse/course/000000_0 03 英语 03
[root@node01hive]# hadoop fs -cat /user/hive/warehouse/course/000001_0 01 语文 02
[root@node01 hive]# hadoop fs -cat /user/hive/warehouse/course/000002_0 02 数学 01
特别强调:
分桶字段必须是表中的字段。
分桶逻辑:
对分桶字段求哈希值,用哈希值与分桶的数量取余,余几,这个数据就放在那个桶内。
分桶的作用和好处
1、对于join的需求,能够起到优化加速的作用。(前提是,join字段设置为分桶字段)
2、用于数据取样(获取/提取数据样本)
将数据编号作为分桶字段。这样每个桶内各种“级别”的数据都有。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值