HIVE 数据模型,
体系结构: 元数据 /HQL的执行
安装: 嵌入 /远程 /本地
管理: CLI /web界面 /远程服务
数据类型: 基本 /复杂 /时间
数据模型: 数据存储 /内部表 /分区表 /外部表 /桶表 /视图
WEB管理工具:http://tdxy-bigdata-04:8889/notebook/editor?type=hive
集群状态管理:http://tdxy-bigdata-03:7180/cmf/home
基于HDFS没有专门的数据存储格式,默认使用制表符存储结构主要包括:数据库、文件、表、视图,可以直接加载文本文件创建表时,可以指定Hive数据的列分隔符和行分隔符。
HIVE的数据模型:
内部表:
- 与数据库中的Table概念相似,每一个Table在hive中都有一个相应的目录来存储数据,所有的数据都存储在这个目录下(外部表除外)
create table t1 (tid int, tname string, age int); create table t2 (tid int, tname string, age int) location '/mytable/hive/t2' create table t3 (tid int, tname string, age int) row format delimited fields terminated by ','; create table t4 as select * from t1;
分区表:
- partition对应于数据库中的Partition 列的密集索引
- 在Hive中,表中的一个Partition对应于表下的一个目录,所有的Partition的数据都存储在对应的目录中。
创建分区表: create table partition_table (sid int, sname string) partitioned by (gender string) row format delimited fields terminated by ','; 向分区表中插入数据: hive> insert into table partition_table partition(gender='M') select sid, sname from sampledata where gender='M'; hive> insert into table partition_table partition(gender='F') select sid, sname from sampledata where gender='F';
外部表:
- 指向已经在HDFS中存在的数据,可以创建Partition
- 它和内部表在元数据的组织上是相同的,而实际数据的存储则有较大的差异。
- 外部表侄有一个过程,加载数据和创建表同时完成,并不会移动到数据仓库目录中,只是与外部数据建立一个链接。当删除一个外部表时,仅删除该链接。
1、准备几张相同数据结构的数据txt文件,放在HDFS的/input 目录下。 2、在hive下创建一张有相同数据结构的外部表external_student,location设置为HDFS的/input 目录。则external_student会自动关连/input 下的文件。 3、查询外部表。 4、删除/input目录下的部分文件。 5、查询外部表。删除的那部分文件数据不存在。 6、将删除的文件放入/input目录。 7、查询外部表。放入的那部分文件数据重现。 (1)准备数据: student1.txt 1,Tom,M,60,80,96 2,Mary,F,11,22,33 student2.txt 3,Jerry,M,90,11,23 student3.txt 4,Rose,M,78,77,76 5,Mike,F,99,98,98 # hdfs dfs -ls / # hdfs dfs -mkdir /input 将文件放入HDFS文件系统 hdfs dfs -put localFileName hdfsFileDir # hdfs dfs -put student1.txt /input # hdfs dfs -put student2.txt /input # hdfs dfs -put student3.txt /input (2)创建外部表 create table external_student (sid int, sname string, gender string, language int, math int, english int) row format delimited fields terminated by ',' location '/input'; (3)查询外部表 select * from external_student; (4)删除HDFS上的student1.txt # hdfs dfs -rm /input/student1.txt (5)查询外部表 select * from external_student; (6)将student1.txt 重新放入HDFS input目录下 # hdfs dfs -put student1.txt /input (7)查询外部表 select * from external_student;
桶表:
- 对数据进行HASH运算,放在不同文件中,降低热块,提高查询速度
- 例如:根据sname进行hash运算存入5个桶中。
create table bucket_table (sid int, sname string, age int) clustered by (sname) into 5 buckets;
视图:
- 视图是一种虚表,是一个逻辑概念;可以跨越多张表
- 视图建立在已有表的基础上,视图赖以建立的这些表称为基表。
- 视图可以简化复杂的查询。
创建视图 create view viewName as select data from table where condition; 查看视图结构 desc viewName; 查询视图 select * from viewName;
数据准备:
sampledata.txt
1,Tom,M,60,80,96
2,Mary,F,11,22,33
3,Jerry,M,90,11,23
4,Rose,M,78,77,76
5,Mike,F,99,98,98
将文本数据插入到数据表:
hive> load data local inpath '/root/pl62716/hive/sampledata.txt' into table sampledata;
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。