一.啟動
1.啟動hadoop
2.直接在命令行下輸入hive就可以換啟動hive
二.創建
1.創建普通表(注意各種分隔符的寫法)
create table employees( name string, salary float, subordiantes array<string>, deductions map<string,float>, address struct<street:string,city:string,state:string,zip:int>) row format delimited fields terminated by ' ' collection items terminated by ',' map keys terminated by ':' lines terminated by '\n';
2.創建分區表(注意partitioned by (country string,state string)的位置)
create table employees_partition( name string, salary float, subordiantes array<string>, deductions map<string,float>, address struct<street:string,city:string,state:string,zip:int>) partitioned by (country string,state string) row format delimited fields terminated by ' ' collection items terminated by ',' map keys terminated by ':' lines terminated by '\n';
3.向hive中加載數據,指定了不存在的分區就相當於創建分區
load data local inpath '[本地文件路徑]'
into table employees_partition partition (country='China',state='Liaoning');
三.分區表
1向分區表中添加分區
alter table employees_partition add [if not exists] partition(country='usa',state='ca');
2刪除分區
alter table employees_partition drop [if exists] partition(country='usa',state='ca');
四.查看信息
1查看一個分區表的分區
show partitions employees_partition;
2查看分區表的詳細信息
describe formatted employees_partition;
五.重命名表
alter table employees_partition rename to employees_par;
六.修改列
1增加列
alter table employees_partition add columns(app_name string,session_id int);
2刪除列(其實是重新指定列,如果沒有某列,那么就刪除了)
alter table employees_partition replace columns(name string,salary float,subordinates array<string>,deductions map<string,float>,address struct<street:string,city:string,state:string,zip:int>);
七.桶表
1創建桶表
create table employees_bucket(id int,name string,age int) clustered by(id) into 3 buckets row format delimited fields terminated by ',';
2向桶表中插入數據(不能從文件中直接load)
insert into employees_bucket select * from employees_temp;
八.裝載數據
1從文件中裝載數據
load data local inpath '[本地文件路徑]' [overwrite]
into table employees_temp [partition (country='china',state='ca')];
2通過查詢語句裝載數據
insert into employees_bucket [partition (country='us',state='ca')]
select * from employees_temp;
九.查詢語句
1查詢集合中的元素
查詢數組中的元素:
select name,subordinates[0] from employees_partition;
查詢map中的元素:
select name,deductions["a"] from employees_partition;
查詢struct中的元素:
select name,address.city from employees_partition;
2在查詢中摻入計算
select upper(name),salary,deductions["a"],round(salary*(1-deductions["a"])) from employees_partition;
3聚合查詢(每個查詢都是針對某一列進行聚合查詢)
select count(*),avg(salary) from employees_partition;
4在分區表的某個分區內查詢
select * from [table] where [分區字段名] = 'xxx'
十.視圖
1創建視圖(使用視圖限制查詢表的一部分)(創建過后是可以在show tables中查到shorter_view的)
create view shorter_view as
> select * from employees_temp > where id > 2;
2創建視圖(將表中的map等集合元素單獨列成一個表)
create view shorter_view2(street,city,state) as
> select deductions["a"],deductions["b"],deductions["c"]
> from employees_partition;
十一.其他
1列出查詢計划
explain select sum(age) from employees_temp;
2顯示所有函數
show functions
3 顯示函數幫助
describe function split;