Hive SQL常用語法總結


  Hive是一個數據倉庫基礎的應用工具,在Hadoop中用來處理結構化數據,通過類SQL語言對數據進行操作。Hive將sql語句通過解析器轉換成MapReduce作業提交到Hadoop集群上,Hadoop監控作業執行過程,並將執行結果返回給用戶。

  值得注意的是,Hive並不支持行級數據的更新,主要使用場合為大數據集的批處理作業中。

  下面為Hive中常用的SQL語句,‘[ ]’中的內容根據實際需求來確定要不要寫。

-- 創建數據庫
create database name;

-- 常用顯示命令
show databases; -- 查看有哪些數據庫
show tables;  -- 查看當前數據庫下有哪些表
show tables like '*cc*'  -- 正則表達式顯示表
show partitions;  -- 查看分區
show functions;
describe extended table_name;    -- 查看表的結構,字段,分區等情況

-- 建表語句
create [external] table [if not exists] table_name   --創建表,指定表名,默認為內部表
[(col_name data_type [comment col_comment], ...)]  -- 創建字段,指定字段類型、注釋
[comment table_comment]  -- 表的注釋
[partitioned by (col_name data_type [comment col_comment], col_name_2 data_type_2, ...)] -- 指定分區,要注意分區字段不能出現的建表的字段中
[clustered by (col_name, col_name_2, ...)] [sorted by (col_name [ASC|DESC], ...)] into num_buckets buckets] -- 分桶
[row format row_format]
[stored as file_format]  -- 指定存儲文件類型
[location hdfs_path]  -- 存儲路徑
 ·external 表示創建的表是否為外部表,默認為內部表
·if not exists 表示該表不存在時創建該表,否則忽略異常
·comment 為表、字段增加注釋
·row_format
    row format delimited [fields terminated by char]
                         [collection items terminated by char]
                         [map keys terminated by char]
                         [lines terminated by char] ·file_format
   stored as textfile  -- 純文本數據
   stored as sequencefile  -- 數據需要壓縮,節省存儲空間

-- like關鍵字復制表結構
create table table_name like old_table_name;         

-- 更改表名
alter table table_name rename to new_table_name;

-- 增加一個字段 並 添加注釋
alter table table_name add columns (col_name data_type comment 'col_comment');

-- 刪除列
alter table table_name replace columns (col_name data_type, col_name_2 data_type_2);

-- 增加、刪除分區
alter table table_name add [if not exists] partition_name;  -- 增加
alter table table_name drop partition_name, partition_name_2;   -- 刪除

 

-- 插入數據
insert into table_1 select * from table_2;  -- 在table_1后追加數據
insert overwrite table_1 select * from table_2;  -- 先將table_1中數據清空,然后添加數據

-- 提取數據常用語句
select [distinct] select_expr_1, select_expr_2
from table_name
[where condition]   -- 篩選條件
[group by col_list [having condition]]  -- 分組、分組返回的條件
[order by col_list]  -- 排序
[limit num_1, num_2]   -- 返回數據的起始位置(num_1)以及返回數據的記錄數(num_2)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM