hive链接mysql的shell命令_Hive shell 基本命令
首先連接 hive shell
直接輸入 hive啟動,?使用--開頭的字符串來表示注釋
hive>quit; --退出hive
hive> exit; --exit會影響之前的使用,所以需要下一句kill掉hadoop的進(jìn)程
hine>hadoop job -kill jobid
1、顯示表
hive>create database database_name; 創(chuàng)建數(shù)據(jù)庫
如果數(shù)據(jù)庫已經(jīng)存在就會拋出一個錯誤信息,使用如下語句可以避免拋出錯誤信息:
hive>creat database if not exists database_name;
hive> show databases; 查看數(shù)據(jù)庫;
如果數(shù)據(jù)庫比較多的話,也可以用正則表達(dá)式來查看:
hive> show databases like 'h.*';
hive> use default; ? ?--使用哪個數(shù)據(jù)庫;
hive> show tables; 或者支持模糊查詢:hive> show tables '*t*';
hive> describe tab_name; --查看表的結(jié)構(gòu)及表的路徑
hive> describe database database_name; --查看數(shù)據(jù)庫的描述及路徑
2、創(chuàng)建表
hive> create table test(key string);
OK
Time taken: 0.265 seconds
3、創(chuàng)建分區(qū)表:
hive> create table logs(ts bigint,line string) partitioned by (dt String,country String);
4、加載分區(qū)表數(shù)據(jù):
hive> load data local inpath '/home/Hadoop/input/file1' into table logs partition (dt='2014-03-11',country='CN');
5、展示表中有多少分區(qū):
hive> show partitions logs;
6、顯示表的結(jié)構(gòu)信息
hive> describe?table_name;
hive> describe database database_name; ?--查看數(shù)據(jù)庫的描述及路徑
7、更新表名稱:
hive>alter table table_name rename to another_name;
8、添加新一列:
hive> ALTER TABLE invites ADD COLUMNS (new_col2 INT COMMENT 'a comment');
9、刪除表:
hive>drop table?t1?;? ? ? --刪除表t1 或者:hive> drop table if exists t1;
刪除表中數(shù)據(jù),但要保持表的結(jié)構(gòu)定義:
hive> dfs -rmr /user/hive/warehouse/records;
10、可以用下面的命令來修改數(shù)據(jù)庫的路徑:
hive> creat database database_name location '路徑';
hive> drop database if exists database_name; --刪除空的數(shù)據(jù)庫
hive> drop database if exists database_name cascade; --先刪除數(shù)據(jù)庫中的表再刪除數(shù)據(jù)庫;
11、hive不支持修改表中數(shù)據(jù),但是可以修改表結(jié)構(gòu),而不影響數(shù)據(jù)
有l(wèi)ocal的速度明顯比沒有l(wèi)ocal慢:
hive>load data inpath '/root/inner_table.dat' into table t1; 移動hdfs中數(shù)據(jù)到t1表中
hive>load data local inpath '/root/inner_table.dat' into table t1; 上傳本地數(shù)據(jù)到hdfs中
hive> !ls; 查詢當(dāng)前l(fā)inux文件夾下的文件
hive> dfs -ls /; 查詢當(dāng)前hdfs文件系統(tǒng)下 '/'目錄下的文件;
它不支持行級插入操作、更新操作和刪除操作,也不支持事務(wù),那么往表里面裝數(shù)據(jù)的唯一的途徑就是使用一種“大量”的數(shù)據(jù)裝載操作,
或者僅僅將文件寫入到正確的目錄下面,即以load的方式加載到建立好的表中,且數(shù)據(jù)一旦導(dǎo)入就不可以修改,加載的目標(biāo)可以是一個表
或者分區(qū),如果表包含分區(qū),必須指定每一個分區(qū)名:
a、使用overwrite關(guān)鍵字,加載本地數(shù)據(jù),同時給分區(qū)信息:
hive>load data local inpath '${env:HOME}/目錄'?overwrite into table table_name ?partition (分區(qū)(ds='2018-05-05'));
目標(biāo)表(或者分區(qū))中的內(nèi)容(如果有)會被刪除,然后再將 filepath 指向的文件/目錄中的內(nèi)容添加到表/分區(qū)中,如果
目標(biāo)表(分區(qū))已經(jīng)有一個文件,并且文件名和 filepath 中的文件名沖突,那么現(xiàn)有的文件會被新文件所替代。
b、將查詢結(jié)果插入hive表:
hive>insert overwrite table tab_name [partition(par1=val1,par2=val2)] select_statement1 from from_statement;--基本模式
from from_statement
insert overwrite table tab_name [partition(par1=val1,par2=val2)] select_statement1
[insert overwrite table tab_name [partition(par1=val1,par2=val2)] select_statement2]...;--多插入模式
insert overwrite table tab_name partition(par1[=val1],par2[=val2]...) select_statement1 from from_statement;--自動分區(qū)模式
c、將查詢結(jié)果寫入HDFS文件系統(tǒng):
insert overwrite [local] directory directory1?select ... from ...;--基本模式
from from_statement
insert overwrite[local] directory?directory1?select_statement1
[?insert overwrite[local] directory?directory2 select_statement2];--多插入模式
數(shù)據(jù)寫入文件系統(tǒng)時進(jìn)行文本序列化,且每列用^A 來區(qū)分,\n換行。
d、insert into:
insert into table tab_name [partition(par1=val1,par2=val2)] select_statement1 from from_statement;
12、顯示所有函數(shù):
hive>show functions;
hive>describe function fun_name;--查看函數(shù)的用法;
hive>select col1[0],col2['b'],col3.c from complex;--查看數(shù)組、map、結(jié)構(gòu);
13、內(nèi)連接:
hive> SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);
查看hive為某個查詢使用多少個MapReduce作業(yè)
hive> Explain SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);
14、外連接:
hive> SELECT sales.*, things.* FROM sales LEFT OUTER JOIN things ON (sales.id = things.id);
hive> SELECT sales.*, things.* FROM sales RIGHT OUTER JOIN things ON (sales.id = things.id);
hive> SELECT sales.*, things.* FROM sales FULL OUTER JOIN things ON (sales.id = things.id);
in查詢:Hive不支持,但可以使用LEFT SEMI JOIN
hive> SELECT * FROM things LEFT SEMI JOIN sales ON (sales.id = things.id);
15、Map連接:Hive可以把較小的表放入每個Mapper的內(nèi)存來執(zhí)行連接操作
hive> SELECT /*+ MAPJOIN(things) */ sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);
INSERT OVERWRITE TABLE ..SELECT:新表預(yù)先存在
hive> FROM records2
> INSERT OVERWRITE TABLE stations_by_year SELECT year, COUNT(DISTINCT station) GROUP BY year
> INSERT OVERWRITE TABLE records_by_year SELECT year, COUNT(1) GROUP BY year
> INSERT OVERWRITE TABLE good_records_by_year SELECT year, COUNT(1) WHERE temperature != 9999 AND (quality = 0 OR quality = 1 OR quality = 4 OR quality = 5 OR quality = 9) GROUP BY year;
CREATE TABLE ... AS SELECT:新表預(yù)先不存在
hive>CREATE TABLE target AS SELECT col1,col2 FROM source;
16、創(chuàng)建視圖:
hive> CREATE VIEW valid_records AS SELECT * FROM records2 WHERE temperature !=9999;
17、查看視圖詳細(xì)信息:
hive> DESCRIBE EXTENDED valid_records;
18、從表中導(dǎo)出數(shù)據(jù):
hadoop fs -cp source_path target_path
或者:用戶可以使用 insert……directory……
insert overwrite local directory '/tmp/目錄' 這里指定的路徑也可以是全URL路徑
19、hive中使用正則表達(dá)式
(1) hive> select 'price.*' from table_name;
選出所有列名以price作為前綴的列
(2) 用Like或者RLike
20、聚合函數(shù)
可以通過設(shè)置屬性hive.map.aggr值為true來提高聚合的性能:
hive>hive.map.aggr=true;
21、什么情況下hive可以避免進(jìn)行mapreduce?
在本地模式的時候可以避免觸發(fā)一個mr的job,此外,如果屬性hive.execmode.local.auto的值為true的話,hive還戶嘗試本地模式進(jìn)行其他的操作。
set hive.execmode.local.auto=true;
說明:最好將 set hive.execmode.local.auto=true;這個設(shè)置增加到你的$HOME/.hiverc配置文件中去。
22、JOIN語句
hive支持通常的SQL JOIN語句,但是只支持等值連接。hive也不支持在on子句中用謂詞OR
23、union all
將兩個表或者多個表進(jìn)行合并,每一個union all子查詢都必須具有相同的列,而且對應(yīng)每個字段的每個類型都必須一致。
總結(jié)
以上是生活随笔為你收集整理的hive链接mysql的shell命令_Hive shell 基本命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 羊胆汁的功效与作用、禁忌和食用方法
- 下一篇: mysql算法优化原则_Mysql优化原