前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hive中外部表、内部表、分区表、分桶表

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

作者头像
用户4870038
发布2021-02-05 11:25:05
6580
发布2021-02-05 11:25:05
举报
文章被收录于专栏:MyBatis入门案例-注解

文章目录

外部表

创建数据库 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、用于数据取样(获取/提取数据样本) 将数据编号作为分桶字段。这样每个桶内各种“级别”的数据都有。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/11/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 外部表
  • 内部表
  • 分区表
  • 分桶表
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档