HiveQL基本操作整理
生活随笔
收集整理的這篇文章主要介紹了
HiveQL基本操作整理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 創建操作
1.1 創建表
CREATE TABLE pokes (foo INT, bar STRING);1.2 基于現有的表結構創建一個新表
create table new_table like records;1.3 創建視圖:
CREATE VIEW valid_records AS SELECT * FROM records2 WHERE temperature !=9999;1.4 創建外部表:
CREATE EXTERNAL TABLE page_view(viewTime INT, userid BIGINT,page_url STRING, referrer_url STRING,ip STRING COMMENT 'IP Address of the User',country STRING COMMENT 'country of origination') COMMENT 'This is the staging page view table' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\054' STORED AS TEXTFILE LOCATION '<hdfs_location>';1.5 建分區表:
CREATE TABLE par_table(viewTime INT, userid BIGINT,page_url STRING, referrer_url STRING,ip STRING COMMENT 'IP Address of the User') COMMENT 'This is the page view table' PARTITIONED BY(date STRING, pos STRING) ROW FORMAT DELIMITED fields terminated by '\t' lines terminated by '\n'; STORED AS SEQUENCEFILE;2. 加載數據
hive不支持用insert語句一條一條的進行插入操作,也不支持update操作。數據是以load的方式加載到建立好的表中。數據一旦導入就不可以修改。
路徑支持:
* 相對路徑,例如:project/data1
* 絕對路徑,例如: /user/hive/project/data1
* 包含模式的完整 URI,例如:hdfs://namenode:9000/user/hive/project/data1
2.1 從本地文件加載數據:
LOAD DATA LOCAL INPATH '/home/hadoop/input/ncdc/micro-tab/sample.txt' OVERWRITE INTO TABLE records;2.2 加載分區表數據:
load data local inpath '/home/hadoop/input/hive/partitions/file1' into table logs partition (dt='2001-01-01',country='GB');3. 查看表結構
3.1 展示所有表:
SHOW TABLES; SHOW TABLES '.*s'; //按正條件(正則表達式)顯示表3.2 顯示表的結構信息
DESCRIBE invites;3.3 展示表中有多少分區:
show partitions logs;3.4 顯示所有函數:
show functions;3.5 查看函數用法:
describe function substr;3.6 查看數組、map、結構
select col1[0],col2['b'],col3.c from complex;3.7 查看hive為某個查詢使用多少個MapReduce作業
Explain SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);3.8 查看視圖詳細信息:
DESCRIBE EXTENDED valid_records;4. 更新操作
4.1 更新表的名稱:
ALTER TABLE source RENAME TO target;4.2 添加、更新一列
ALTER TABLE invites ADD|REPLACE COLUMNS (new_col2 INT COMMENT 'a comment');4.3 增加、刪除分區
ALTER TABLE table_name ADD [IF NOT EXISTS] partition_spec [ LOCATION 'location1' ] partition_spec [ LOCATION 'location2' ] ...partition_spec:: PARTITION (partition_col = partition_col_value, partition_col = partiton_col_value, ...)ALTER TABLE table_name DROP partition_spec, partition_spec,...4.4 增加表的元數據信息
ALTER TABLE table_name SET TBLPROPERTIES table_properties table_properties::[property_name = property_value…..]4.5 改變表文件格式與組織
ALTER TABLE table_name SET FILEFORMAT file_format ALTER TABLE table_name CLUSTERED BY(userid) SORTED BY(viewTime) INTO num_buckets BUCKETS5. 刪除操作
5.1 刪除表:
DROP TABLE records;5.2 刪除表中數據,但要保持表的結構定義
dfs -rmr /user/hive/warehouse/records;5.3 刪除視圖
DROP VIEW view_name6. 連接操作
6.1 內連接:
SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);6.2 外連接:
SELECT sales.*, things.* FROM sales LEFT OUTER JOIN things ON (sales.id = things.id); SELECT sales.*, things.* FROM sales RIGHT OUTER JOIN things ON (sales.id = things.id); SELECT sales.*, things.* FROM sales FULL OUTER JOIN things ON (sales.id = things.id);6.3 in查詢:Hive不支持,但可以使用LEFT SEMI JOIN
SELECT * FROM things LEFT SEMI JOIN sales ON (sales.id = things.id);6.4 Map連接:Hive可以把較小的表放入每個Mapper的內存來執行連接操作
SELECT /*+ MAPJOIN(things) */ sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);INSERT OVERWRITE TABLE ..SELECT:新表預先存在
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:新表表預先不存在
CREATE TABLE target AS SELECT col1,col2 FROM source;7. 插入數據
7.1 基本模式
INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement7.2 多插入模式
FROM from_statement INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 [INSERT OVERWRITE TABLE tablename2 [PARTITION ...] select_statement2] ...7.3 自動分區模式
INSERT OVERWRITE TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) select_statement FROM from_statement8. 導出數據到HDFS
數據寫入文件系統時進行文本序列化,且每列用^A 來區分,\n換行
INSERT OVERWRITE [LOCAL] DIRECTORY directory1 SELECT ... FROM ...FROM from_statementINSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement1[INSERT OVERWRITE [LOCAL] DIRECTORY directory2 select_statement2]總結
以上是生活随笔為你收集整理的HiveQL基本操作整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS下Hive2.0.0集群模式
- 下一篇: Hive体系结构(一)架构与基本组成