庫操作
創(chuàng)建庫
create databases if not exists 庫名;
刪除庫
drop databases 庫名 restrict; restrict 默認關鍵字(可不寫)只能刪除空庫,里邊沒有表的;
drop databases if exists 庫名
drop databases 庫名 cascade; cascade強制刪除
查看庫的列表信息
show databases;
使用庫
use databases;
查看正在使用的庫
select? current_databases();
查看庫信息
desc databases 庫名;
可使用模糊查詢 show databases 庫名 like 'my%'
修改庫
基本不用
表的操作
創(chuàng)建表示例(hive sql)
create [external] table [if not exists] table_name (clo_name type comment "字段描述") comment? '表描述'
[partitioned by?(col_name type [comment '描述'])]
[clustered by (col_name,col_name,...)]
[sorted by (col_name [asc|desc],...)into num_buckets buskets]
[row format row_format]
[stored as file_format]
[location hdfs_path]
關鍵字講解如下:
1)external 關鍵字:是否創(chuàng)建外部表侵佃,不加時是創(chuàng)建內部表钓账,加上external關鍵字創(chuàng)建的是外部表
2)partitioned by (col_name type [comment '描述']):指定分區(qū)字段?
分區(qū)表存的是不同的目錄
分區(qū)表在添加數(shù)據(jù)之前先添加分區(qū)
alter table table_name?add if not exists partition(col_name?='一個分區(qū)')
注意:分區(qū)字段一定不是建表字段中的字段允悦,是一個全新的字段
3)clustered by (col_name,col_name,...)指定分桶字段clustered by
sorted by (col_name [asc|desc],...)??into num_buckets buskets 指定排序字段
注意:分桶字段一定是建表語句中的某一個字段或多個字段
排序規(guī)則指定的是在同一個分桶內的排序規(guī)則
into num_buckets buskets;指定分桶個數(shù)num_buckets
判斷依據(jù):根據(jù)建表語句模板字段后邊是否跟type類型录择,因為一個字段在建表語句中不會建兩次
4)[row format row_format] 指定分隔符
delimited fields terminated by '' 指定列分隔符
lines terminated by '' 指定行分隔符
5)[stored as file_format] 指定最終表數(shù)據(jù)的存儲格式
textfile 文本格式 默認的格式
rcfile 行列結合的格式
parquet 壓縮格式
6)[location hdfs_path] 指定hive 上表的hdfs上的存儲路徑
不指定的話不从,存儲在配置的路徑下? ? ??
沒指定沒配置除师,默認在? /user /hive /warehouse
復制表匹舞,復制表結構不復制數(shù)據(jù)
?create table t1 like t2;
查看表
show tables;
show tables in 庫名;
show tables like 's%';
查看表信息
desc 表名:顯示表字段
desc extended 表名:顯示表的詳細信息(由左至右揩徊,連續(xù)在一起)
desc formatted 表名:格式化顯示表的詳細信息(規(guī)整的平夜,一行一行的)
刪除表
drop table if exists 表名;
清空表
truncate table 表名;清空表中的數(shù)據(jù)蝶棋,保留表結構
修改表 alter
修改表名稱
alter table 表名 rename to 新表名;
修改表字段信息
? ? 1)添加字段
? ? ? ? ? ? alter table 表名 add columns (col_name type,col_name type);
所有新的字段一定要指定類型
? ? 2)修改字段,修改字段類型
? ? ? ? ? ? alter table 表名 chage 原始字段 新字段 新字段類型;
注意:在進行修改表字段定義的時候一定注意表字段類型之間的匹配? ? 只能小類型轉大類型
????????????string 類型相當于數(shù)值類型屬于大類型
????????????hive1.2.2中沒有限制忽妒,各種類型之間都可以進行修改
? ? 3)替換列
? ? ? ? ? ? alter table table_name replacr columns (in int,name string);由原始表全部字段替換成兩個
修改表分區(qū)信息
? ? 1)添加分區(qū)
? ? ? ? alter table?table_name add if not exists partition(city='beijing')
????????添加分區(qū)的過程中玩裙,還可以指定分區(qū)的存儲路徑
? ? ? ? alter table?table_name?add if not exists partition(city='beijing') location '/user/beijing'
? ? 2)修改分區(qū) 修改分區(qū)的存儲路徑
? ? ? ? alter table?table_name? set partition(city='shenzhen')?location '/user/shenzhen'
? ? 3)刪除分區(qū)
? ??????alter table?table_name?drop if exists partition(city='beijing')
查看分區(qū)信息,針對分區(qū)表
show partitions table_name;
show partitions table_name partiton(city='beijing')
DDL和DML操作如下