Hive Shell 命令詳解

Hive服務介紹

Hive默認提供的cli(shell)服務,若是須要啓動其餘服務,那麼須要service參數來啓動其餘服務,好比thrift服務、metastore服務等。能夠經過命令hive --service help查看hive支持的命令。mysql

Hive Shell命令介紹
Hive的shell命令是經過${HIVE_HOME}/bin/hive文件進行控制的,經過該文件咱們能夠進行hive當前會話的環境管理、也進行進行hive的表管理等操做。hive命令須要使用';'進行結束標示。經過hive -H查看幫助信息:另外從hive0.11版本開始支持--database <databasename>.linux

 

Hive Shell經常使用基本命令
Hive的Shell基本經常使用命令主要包含退出客戶端、添加文件、修改/查看環境變量、執行linux命令、執行dfs命令等。命令包括:sql


quit、exit、set、add JAR[S] <filepath> <filepath>*、list JAR[S]、delete JAR[S] <filepath>*、! <linux-command>、dfs <dfs command>等。shell


除了Hive的基本命令外,其餘的命令主要是DDL和DML等操做數據表的命令。apache

 

HiveQL介紹
HiveQL簡稱HQL,是一種相似sql的查詢語言,絕大多數語法和sql相似。HQL支持基本類型和複雜類型兩大類數據類型。數組


基本類型包括TINYINT(1byte), SMALLINT(2byte), INT(4byte), BIGINT(8byte), FLOAT(4byte), DOUBLE(8byte), BOOLEAN(-), STRING(2G)。app


複雜類型包括ARRAY(一組有序數組,類型必須一致), MAP(無序鍵值對,鍵值內部字段類型必須相同,並且要求key的類型爲基本數據類型), STRUCT(一組字段,類型任意)。
show、describe、explain命令介紹oop



show命令的主要做用是查看database、table、function等組件的名稱信息,也就是經過show命令咱們能夠知道咱們的hive中有那些database;當前database中有那些table。等等。和mysql的show命令類型。
describe命令的主要做用是獲取database、table、partition的具體描述信息,包括存儲位置、字段類型等信息。
explain命令的主要做用是獲取hql語句的執行計劃,咱們能夠經過分析這些執行計劃來優化hql語句。優化

 


Database介紹
hive提供database的定義,database的主要做用是提供數據分割的做用,方便數據管理。命令以下:ui



建立: create (DATABASE|SCHEMA) [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path] [WITH DBPROPERTIES (property_name=value,name=value....)]。
顯示描述信息:describe DATABASE|SCHEMA [extended] database_name。
刪除:DROP DATABASE|SCHEMA [IF EXISTS] database_name [RESTRICT|CASCADE]
使用: use database_name。


 

Hive表介紹
Hive中的表能夠分爲內部表(託管表)和外部表,區別在於:外部表的數據不是有hive進行管理的,也就是說當刪除外部表的時候,外部表的數據不會從hdfs中刪除。而內部表是又hive進行管理的,在刪除表的時候,數據也會刪除。通常狀況下,咱們在建立外部表的時候會將表數據的存儲路徑定義在hive的數據倉庫路徑以外。
Hive建立表主要有三種方式,第一種直接使用create table命令,第二種使用create table .... AS select ....(會產生數據)。第三種使用create table tablename like exist_tablename.命令。
create table命令介紹

 

案例1:

先將data2.txt文件上傳到hdfs的/customers文件夾中。


 hdfs dfs -mkdir /customers

hdfs dfs -put data2.txt /customers



案例2:
先將data3.txt文件上傳到hdfs的/complex_table_test文件夾中。

 


hdfs dfs -mkdir /complex_table_test
hdfs dfs -put data2.txt /complex_table_test


 


案例3:
hive和hbase關聯


create external table hive_users

(key string,

id int,

name string,

phone string)

row format serde 'org.apache.hadoop.hive.hbase.HBaseSerDe'

stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'

with serdeproperties('hbase.columns.mapping'=':key,f:id,f:name,f:phone')

tblproperties('hbase.table.name'='users');


 

 

Hive shell 命令深刻使用

前置條件:
hive啓動
root用戶登陸 密碼123456
啓動mysql:service mysqld restart
使用hadoop用戶登陸,啓動metastore:hive --service metastore &
數據文件準備
將文檔文件夾中的classes.txt和students.txt移動到linux機器和hdfs文件系統上。
命令:hdfs dfs -put ./13 /kai/
建立hive相關表準備:

 


create database kai13;
create table students(studentId int comment 'this is student id, is not null', classId int comment 'this is class id, can set to null', studentName string comment 'this is student name') row format delimited fields terminated by ',';
create table classes(classId int comment 'this is class id, is not null', className string comment 'this is class name') row format delimited fields terminated by ',';


 

1、導入數據
1. 分別導入local和hdfs的數據
a. 分別從linux機器上導入數據


load data local inpath '/home/hadoop/datas/13/classes.txt' into table classes;
load data local inpath '/home/hadoop/datas/13/students.txt' into table students;
load data local inpath '/home/hadoop/datas/13/classes.txt' overwrite into table classes;



b. 從hdfs上導入數據


load data inpath '/kai/13/students.txt' into table students;
dfs -put /home/hadoop/datas/13/students.txt /kai/13/
load data inpath '/kai/13/students.txt' overwrite into table students;



2. 導入其餘表的數據(多表插入)
將學生表的學生id和classid分別導出到不一樣表中,


create table test1(id int);
create table test2(id int);
from students insert into table test1 select studentid insert overwrite table test2 select distinct classid where classid is not null;



2、select語法介紹
from語法
1. 正常from:


select * from students;



2. from語句提早:


from students select *;



cte語法:
1. 獲取班級號爲1的學生信息:


with tmp as (select studentid as sid,classid as cid,studentname as name from students where classid=1) from tmp select *;



2. 獲取總學生數、已經分配班級的學生數、未分配班級的學生數(做業1)。
分析;
總學生數:studentid的總數
分配班級的學生數:classid不爲空的學生總數
未分配的學生數: classid爲空的學生數
結果: 12 7 5
where & group by語法實例:
group語句只能返回對於的group列&進行聚合的value。
1. 獲取學生數大於3的班級id


from students select classid where classid is not null group by classid having count(studentid) > 3;



排序語法:
1. 使用order by根據學生id倒序。


select * from students order by studentid desc;



2. 設置hive.mapred.mode爲strict,而後在進行order by操做。


set hive.mapred.mode=strict;
select * from students order by studentid desc; 會出現異常
select * from students order by studentid desc limit 5;



3. 使用sort by根據學生id排序。


select * from students sort by studentid desc;



4. 設置mapreduce.job.reduces個數爲兩個,而後再使用sort by進行排序。


set mapreduce.job.reduces=2;
select * from students sort by studentid desc;



3、join語法
內鏈接語法
1. 獲取學生和班級之間徹底匹配的數據。


select students.*,classes.* from classes join students on classes.classid=students.classid;
select students.*,classes.* from classes cross join students on classes.classid=students.classid;



外連接語法:
1. 獲取所有學生的班級信息,若是該學生沒有分配班級,那麼班級信息顯示爲null。


select students.*, classes.* from students left join classes on students.classid = classes.classid;



2. 獲取所有班級的學生信息,若是某個班級沒有學生,那麼學生信息顯示爲null。(做業2)
3. 獲取所有信息,若是沒有匹配數據的顯示null。(做業3)
半鏈接:
1. 獲取學生表中班級id在班級表中的全部學生信息。


sql: select students.* from students where classid in (select distinct classid from classes);
原hql: select students.* from students join classes on students.classid = classes.classid;
新hql: select students.* from students left semi join classes on students.classid=classes.classid;
mapjoin:
select /*+ mapjoin(classes) */ * from students join classes on students.classid=classes.classid;



4、子查詢
1. 獲取學生數最多的班級,學生的個數。
第一步:獲取每一個班級的學生總數
第二步:選擇學生數最多的班級學生數


from (select count(studentid) as sc from students where classid is not null group by classid) as tmp select max(sc);



2. 獲取學生數最多的班級信息。(做業4)
第一步:獲取每一個班級的學生總數
第二步:選擇學生數最多的班級學生數
第三步:根據最多的學生數和第一步獲取的表數據進行比較,獲取班級信息。

5、導出數據
1. 導出表關聯後的班級名稱和學生名稱(loca&hdfs)。(導出所有不爲空的信息)
班級1,學生1


from (select classes.classname as col1, students.studentname as col2 from classes join students on classes.classid = students.classid) as tmp insert overwrite local directory '/home/hadoop/result/13/01' select col1,col2 insert overwrite directory '/kai/result/13/01/' select col1,col2 ;




格式化:


from (select classes.classname as col1, students.studentname as col2 from classes join students on classes.classid = students.classid) as tmp insert overwrite local directory '/home/hadoop/result/13/01' row format delimited fields terminated by ',' select col1,col2 ;



2. 同時分別將已經分配班級的學生和未分配班級的學生導出到不一樣的文件夾中。
6、其餘命令
1. 在students和classes表上建立一個視圖,視圖包含兩列分別是:班級名稱,學生名稱


create view viewname as select classes.classname as cname, students.studentname as sname from classes join students on classes.classid = students.classid



2. 在linux系統中經過命令hive -f/-e將全部學生信息保存到一個文件中。
新建一個文件,文件內容爲:

 


select * from students

執行:hive --database kai13 -f test.sql >> result.txt

相關文章
相關標籤/搜索