1、數據庫的操作
create database hive_db //創建數據庫hive_db
create table hive_db.test(字段內容及其格式省略) //在數據庫hive_db中創建test表
create database student_db location '/user/hive/student.db' //創建數據庫student_db,但是在hdfs中顯示student.db,在hive控制端中顯示studentdb(在有location的情況下)
create database if not exists hive_db
show databases like 'hive*' //結果為hive_db
drop database hive_db //這種方式只能刪除空數據庫
drop database studentdb casecade //強制刪除非空數據庫
describe database hive_db //顯示數據庫的信息
create database teacherdb comment "數據庫teacherdb的備注"
2、對表的操作
create table if not exists hive_db.t1(字段) //在數據庫hive_db中創建表t1
show tables in hive_db like "t*" //在數據庫hive_db中尋找以t開頭的表。
create table student1 as select * from stu; //復制表及其數據
describe extended records; //查看表信息
describe formatted records //查看表詳細信息
2.1、內部表與外部表的相互轉換:
alter table student set tblproperties("EXTERNAL"="TRUE"); //內部表轉換為外部表
alter table student set tblproperties("EXTERNAL"="FALSE"); //外部表轉換為內部表
2.2、分區表(分區在hdfs上其實是目錄,分區名不是表結構中的字段名而是在創建表和分區時另外加的):
create table stu_partition(id int,name string)
partitioned by (month string)
row format delimited fields terminated by '\t';
此表名為stu_partition按照月份來分區。
上傳數據到分區表:
load data local inpath '/home/hdc/Document/student1.txt' into table stu_partition partition(month="201906");
分區表查找:
select * from stu_partition; //查找分區表中的所有記錄;
select * from stu_partition where month="201906" //查找分區表中分區名201906中的所有記錄
查看分區:
show partitions stu_partition;
增加分區:
alter table stu_partition add partition (month="201908");
alter table stu_partition add partition (month="201909") partition (month="201910");
刪除分區:
alter table stu_partition drop partition(month="201908");
alter table stu_partition drop partition(month="201909"),partition (month="201910");
ps:二級分區指的是2個分區字段,按照字段的順序來設置分區順序,例如:partition(month="201909",day="01")就是一個二級分區,其目錄結構是day文件夾是month文件夾的子文件夾。
利用Hadoop和hive命令創建分區的區別:
其實Hadoop命令創建分區就是在數據倉庫中的表下創建一個文件夾,若將數據導入Hadoop命令創建的分區,再利用hive的select語句查詢,將查詢不到結果。這是因為Hadoop命令創建的分區在hive中沒有關於此分區的元數據信息。
而利用hive命令創建的分區不僅會在hdfs上的hive數據倉庫中創建相應的文件夾,而且還將此文件夾在hdfs上的信息(元數據)存儲在hive中的matestore數據庫中。
解決方法:
(1)msck repair table stu_partition;
(2)alter table stu_partition add partition(month="201911");
//此方法為分區表在hdfs上創建文件夾和在hive中創建此文件夾的元數據,之前因為利用Hadoop命令手動創建了文件夾故現在只需創建元數據。
(3)正常上傳數據即load data local inpath '/home/hdc/Document/student1.txt' into table stu_partition partition(month="201911");
2.3、分桶表
分區表是針對數據的存儲路徑,分桶表針對的是數據文件。其中分區字段是表外字段,而分桶字段是表內字段。
create table stu_bucket(
id int,
name string
)clustered by (id) into 4 buckets
row format delimited fields terminated by '\t';
上傳數據到分桶表只能通過insert方法如下例所示:
insert into table stu_bucket
select *from stu_temp;
利用分桶表對數據進行抽樣查詢(桶數為z):
select * from stu_bucket tablesample(bucket x out of y on id)
注意:x<=y,z%y==0 || y%z==0
抽樣數n=z/y
從第x桶開始抽取n桶,第一個抽取的是第x桶,第二個桶是x+y
注意:數據塊抽樣,按照數據塊的百分比抽樣,若表的數據大小小於普通的塊大小,那么將會返回所有行。
(1)分桶的概念:對於每一個表(table)或者分區, Hive可以進一步組織成桶,也就是說桶是更為細粒度的數據范圍划分。Hive也是針對某一列進行桶的組織。Hive采用對列值哈希,然后除以桶的個數求余的方式決定該條記錄存放在哪個桶當中。
(2)把表或分區組織成桶的好處:
a、獲得更高的查詢處理效率。桶為表加上了額外的結構,Hive 在處理有些查詢時能利用這個結構。具體而言,連接兩個在(包含連接列的)相同列上划分了桶的表,可以使用 Map 端連接 (Map-side join)高效的實現。比如JOIN操作。對於JOIN操作兩個表有一個相同的列,如果對這兩個表都進行了桶操作。那么將保存相同列值的桶進行JOIN操作就可以,可以大大較少JOIN的數據量。
b、使取樣(sampling)更高效。在處理大規模數據集時,在開發和修改查詢的階段,如果能在數據集的一小部分數據上試運行查詢,會帶來很多方便。
按我的理解,所謂Hive中的分桶,實際就是指的MapReduce中的分區。根據Reduce的數量,分成不同個數的文件。
3、對表的操作
刪除表:
drop table if exists stu_partition;
修改表:
表重命名:alter table stu_partition rename to student_partition;
修改表中列信息:alter table student_partition change columns id student_id int;
增加列:alter table student_partition add columns(
ClassId int commet "備注信息",
ClassName string comment "備注信息"
);
刪除或者替換列:alter table student_partition replace columns(
id string commet "備注信息",
name string commet "備注信息"
);//此種替換是指將所用列全部刪除再來新建以上兩列。、
PS:alter語句改變的是表的元數據信息而不是真正的數據。