hive經常使用命令

啓動hive:
start-all.sh
hive數據庫


進入hive以後,建立一個hive數據庫庫:
create database if not exists db;數組

建立一個新表,結構與其餘同樣
hive> create table new_table like records;app

建立分區表:
hive> create table logs(ts bigint,line string) partitioned by (dt String,country String);函數

加載分區表數據:
hive> load data local inpath '/home/hive/partitions/files' into table logs partition (dt='2017-08-01',country='GB');oop

展現表中有多少分區:
hive> show partitions logs;內存

展現全部表:
hive> show tables;
lists all the tables
hive> show tables '.*s';hadoop


顯示錶的結構信息
hive> DESCRIBE invites;
shows the list of columnsget

更新表的名稱:
hive> ALTER TABLE source RENAME TO target;input

添加新一列
hive> ALTER TABLE invites ADD COLUMNS (new_col2 INT COMMENT 'a comment');

刪除表:
hive> DROP TABLE records;
刪除表中數據,但要保持表的結構定義
hive> dfs -rmr /user/hive/warehouse/records;string

從本地文件加載數據:
hive> LOAD DATA LOCAL INPATH '/home/hadoop/input/ncdc/micro-tab/sample.txt' OVERWRITE INTO TABLE records;

顯示全部函數:
hive> show functions;

查看函數用法:
hive> describe function substr;

查看數組、map、結構
hive> select col1[0],col2['b'],col3.c from complex;


內鏈接:
hive> SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);

查看hive爲某個查詢使用多少個MapReduce做業
hive> Explain SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);

外鏈接:
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);


Map鏈接:Hive能夠把較小的表放入每一個Mapper的內存來執行鏈接操做
hive> SELECT /*+ MAPJOIN(things) */ sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);

INSERT OVERWRITE TABLE ..SELECT:新表預先存在
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:新表表預先不存在hive>CREATE TABLE target AS SELECT col1,col2 FROM source;

相關文章
相關標籤/搜索