柚子快報邀請碼778899分享:HIVE 大數(shù)據(jù)學(xué)習(xí)
柚子快報邀請碼778899分享:HIVE 大數(shù)據(jù)學(xué)習(xí)
參考視頻
黑馬程序員Hive全套教程,大數(shù)據(jù)Hive3.x數(shù)倉開發(fā)精講到企業(yè)級實(shí)戰(zhàn)應(yīng)用_嗶哩嗶哩_bilibili
介紹
? ? ? ? Apache Hive是一款建立在Hadoop之上的開源數(shù)據(jù)倉庫系統(tǒng),可以將存儲在Hadoop文件中的結(jié)構(gòu)化、半結(jié)構(gòu)化數(shù)據(jù)文件映射為一張數(shù)據(jù)庫表,基于表提供了一種類似SQL的查詢模式,稱為Hive查詢語言(HQL),用于訪問和分析存儲在Hadoop文件中的大型數(shù)據(jù)集
? ? ? ? Hive核心是將 HQL轉(zhuǎn)換成MapReduce程序,然后將程序提交到Hadoop集群執(zhí)行。
? ? ? ? Hive由Facebook實(shí)現(xiàn)并開源
Hive和Hadoop關(guān)系
? ? ? ? 從功能上來說,數(shù)倉至少具備 存儲數(shù)據(jù) 和 分析數(shù)據(jù) 的能力
? ? ? ? Apache Hive 作為一款大數(shù)據(jù)時代的數(shù)倉軟件,具備以上能力。只是Hive并不是自己實(shí)現(xiàn),而是借助Hadoop。
? ? ? ? Hive利用HDFS存儲數(shù)據(jù),利用MapReduce查詢分析數(shù)據(jù)。
? ? ? ? Hive的最大優(yōu)點(diǎn)在于讓用戶專注于編寫HQL,Hive幫你轉(zhuǎn)換成MapReduce程序完成對數(shù)據(jù)的分析
? ? ? ? 被稱為 Sql On Hadoop
Hive的理解
? ? ? ? 1 Hive能將數(shù)據(jù)文件映射成一張表,這個映射指什么?
? ? ? ? 答:文件和表之間的對應(yīng)關(guān)系
????????在Hive中能夠?qū)憇ql處理的前提是針對表,而不是針對文件,因此需要將文件和表之間的對應(yīng)關(guān)系描述記錄清楚。
? ? ? ? 映射信息 專業(yè)的叫法稱之為 元數(shù)據(jù)信息(元數(shù)據(jù)是指用來描述數(shù)據(jù)的數(shù)據(jù)metadata)
? ? ? ? 具體看,要記錄的元數(shù)據(jù)信息包括:表對應(yīng)哪個文件(位置信息),表的列對應(yīng)文件的哪個字段(順序信息),文件字段的分隔符是什么
? ? ? ? 2 Hive的本身到底承擔(dān)了什么功能職責(zé)?
? ? ? ? 答:SQL語法解析編譯成為MapReduce
結(jié)構(gòu)
1 客戶端 ;
2 Hive對sl的語法解析,編譯,執(zhí)行計(jì)劃變化;
3 Hadoop (MapReduce,Yarn,HDFS)
4 元數(shù)據(jù)存儲
架構(gòu),組件
1 用戶接口:
????????CLI(command line interface,為shell命令行訪問)
????????JDBC/ODBC
????????WebGUI(瀏覽器訪問)
2 元數(shù)據(jù)存儲
? ? ? ? 通常是存儲在關(guān)系數(shù)據(jù)庫,如mysql/derby中。Hive中的元數(shù)據(jù)包括表的名字,表的列和分區(qū)及其屬性,表的屬性(是否為外部表等),表的數(shù)據(jù)所在目錄等
3 Hive 的 Driver 驅(qū)動程序
? ? ? ? 功能:語法解析器,計(jì)劃編譯器,優(yōu)化器,執(zhí)行器
? ? ? ? 完成HQL查詢語句從詞法分析,語法分析,編譯,優(yōu)化以及查詢計(jì)劃的生成。生成的查詢計(jì)劃存儲在HDFS中,并在隨后有執(zhí)行引擎調(diào)用執(zhí)行。
元數(shù)據(jù)(Metadata)
? ? ? ? 元數(shù)據(jù)(metadata),又稱中介數(shù)據(jù),中繼數(shù)據(jù),為描述數(shù)據(jù)的數(shù)據(jù)。主要是描述數(shù)據(jù)屬性的信息,用來支持如指示存儲位置,歷史數(shù)據(jù),資源查找,文件記錄等功能
? ? ? ? Hive Metadata 即 Hive 的元數(shù)據(jù),元數(shù)據(jù)存儲在關(guān)系型數(shù)據(jù)庫中,如hive內(nèi)置的Derby,或者第三方如Mysql等
元數(shù)據(jù)服務(wù)(Metastore)
? ? ? ? Metastore 即 元數(shù)據(jù)服務(wù),作用是管理metadata元數(shù)據(jù),對外保留服務(wù)地址,讓各種客戶端通過臉頰metastore服務(wù),由metastore再去連接Mysql數(shù)據(jù)庫來存取元數(shù)據(jù)
? ? ? ? 服務(wù)配置由3種模式: 內(nèi)嵌模式,本地模式,遠(yuǎn)程模式。? ? ? ??
內(nèi)嵌模式本地模式遠(yuǎn)程模式(企業(yè)一般這個)Metastore單獨(dú)配置、啟動否否是Metastore存儲介質(zhì)derbymysqlmysql
? ? ? ? 遠(yuǎn)程模式:
? ? ? ? ? ? ? ? 在生產(chǎn)環(huán)境中,建議用遠(yuǎn)程模式來配置HIve Metastore。在這種情況下,其他依賴hive的軟件都可以通過Metastore訪問hive。由于還可以完全屏蔽數(shù)據(jù)庫層,因此這也帶來了更好的可管理性,安全性
hive提供的連接:
????????老版本:bin/hive 連接Metastore
????????
.../hive-3.1.2/bin/hive
????????新版本 : bin/beeline 連接Metastore, beeline連接又新啟動的HiveServer2連接訪問元數(shù)據(jù)服務(wù)
? ? ? ? ? ? ? ? hiveserver2服務(wù)啟動前必須先啟動datastore服務(wù)。
.../hive-3.1.2/bin/beeline
啟動后需要手動輸入地址
! connect jdbc:hive2://node1:10000
root
密碼
Hive可視化客戶端: DataGrip,Dbeaver,SQuirrel SQL Client等?
安裝部署
前置: 安裝好Hadoop,Mysql
參考文檔:HIve安裝配置(超詳細(xì))-CSDN博客
Location?
create table(..
)location '/data' -- 指定本張表的數(shù)據(jù)在hdfs上的存儲路徑
Hive 事務(wù)
早期hive的sql中沒有update和delete
局限性:
? ? ? ? 1 尚不支持begin,commit 和 rollback ,所有語言操作都是自動提交的
? ? ? ? 2 僅支持ORC文件格式(STORED AS ORC)
? ? ? ? 3 默認(rèn)情況下事務(wù)配置為關(guān)閉,需要配置參數(shù)開啟功能
? ? ? ? 4 表必須是分桶表(Bucketed)才可以使用事務(wù)功能
? ? ? ? 5?表參數(shù) transactional必須為true
? ? ? ? 6?外部表不能成為ACID表,不允許從非ACID會話讀取/寫入ACID表?
設(shè)置:
set hive.support.concurrency = true; -- hive是否支持并發(fā)
set hive.enforce.bucketing = true; -- hive2.0開始不需要,是否開啟分桶
set hive.exec.dynamic.partition.mode = nonstrict; -- 動態(tài)分區(qū)
set hive.txn.manager = org.apache.hadoop.hive.ql.lockmgr.DbTxnManager; -- 事務(wù)管理類,默認(rèn)類
set hive.compactor.initiator.on = true; -- 是否在Metastore實(shí)例上運(yùn)行啟動線程和關(guān)閉線程
set hive.compactor.worker.threads = 1; -- 在此metastore實(shí)例上運(yùn)行多少個壓縮程序工作線程
創(chuàng)建事務(wù)表
create table(...)
clustered by (id) into 2 buckets stored as orc TALPROPERTIES ('transactional' = 'true');
-- clustered by (id) into 2 buckets id字段分成2個桶
-- orc 是一種高效的列式存儲
-- 開啟事務(wù)
此表可以用update,delete
Hive View視圖
? ? ? ? Hive的視圖(View)一種虛擬的表,只保存定義,不實(shí)際存儲數(shù)據(jù)
? ? ? ? 通常從真實(shí)的物理表查詢中創(chuàng)建生成視圖,也可以從已經(jīng)存在的視圖上創(chuàng)建新視圖
? ? ? ? 創(chuàng)建視圖時,將凍結(jié)視圖的架構(gòu),如果刪除或更改基礎(chǔ)表,則視圖將失敗
? ? ? ? 視圖是用來簡化操作,不緩沖記錄,也沒有提高查詢性能的功能
create view v_v1 as select id,name from t_v1 limit 10;
create view v_v2 as select id,name from v_v1 limit 2;
show views;
-- 查看創(chuàng)建語句
show create table v_v1;
-- 刪除視圖
drop view v_v1;
-- 更改視圖屬性
alter view v_v1 set TALPROPERTIES ('comment' = 'This is a view');
視圖一旦生成,不能加載插入新數(shù)據(jù)
Hive 3.0新功能
? ? ? ? 物化視圖
? ? ? ? ? ? ? ? 物化視圖(Materialized View)是一個包括查詢結(jié)果的數(shù)據(jù)庫對象,可以用于預(yù)先計(jì)算并保存表連接或聚集等耗時較多的操作結(jié)果。
? ? ? ? ? ? ? ? 使用物化視圖的目的是通過預(yù)計(jì)算,提高查詢性能,當(dāng)然需要占用一定的存儲空間
? ? ? ? ? ? ? ? hive3.0丟棄了index索引的語法支持,推薦使用物化視圖的列式存儲文件格式來加快查詢的速度
? ? ? ? 物化視圖和視圖的區(qū)別:
? ? ? ? ? ? ? ? 視圖是虛擬的,物化視圖是真實(shí)的。
? ? ? ? ? ? ? ? 視圖的目的是簡化降低查詢的復(fù)雜度,而物化視圖的目的是提高查詢性能
create materialized view [if not exists] [db_name].materialized_view_name
[DISABLE REWRITE]
[COMMENT materialized_view_comment]
[PARTITIONED ON(...)]
[CLUSTERED ON () DISTRIBUTED ON () SORTED ON ()]
..
AS SELECT ...
目前數(shù)據(jù)更新,只支持手動重構(gòu)
ALTER MATERIALIZED VIEW [db_name].materialized_view_name REBUILD;
是否重寫查詢使用物化視圖,可以通過全局參數(shù)控制,默認(rèn)true
hive.materializedview.rewriting=true
Hive SQL語法
字段類型:
????????1 原生數(shù)據(jù)類型(primitive data type)
? ? ? ? ? ? ? ? 數(shù)字型: TINYINT, SMALLINT, INT/INTEGER, BIGINT, FLOAT, DECIMAL, NUMBERIC
? ? ? ? ? ? ? ? 日期型: TIMESTAMP, DATE, INTERVAL
? ? ? ? ? ? ? ? 字符型:STRING,VARCHAR, CHAR
? ? ? ? ? ? ? ? boolea型:BOOLEAN, BINARY
????????2 復(fù)雜數(shù)據(jù)類型(complex data type)
? ? ? ? ? ? ? ? ARRAY, MAP,用的多? ?
create table mytable(
id int ,
skin_price map
)row format delimited
fields terminated by ',' --字段分割符
collection items terminated by '-' -- 集合元素之間的分割符
map keys termainated by ':'; -- 指定map的kv之間的分隔符
操作
String : 'a'||'b' = concat('a','b')
復(fù)雜類型運(yùn)算符
select price[n] from table; -- array類型,應(yīng)該是這樣,暫時沒試過
select price[key] from table; -- map類型
????????DDL
內(nèi)部表:
? ? ? ? 內(nèi)部表(Internal table)也稱為被Hive擁有和管理的托管表(Managed table)。默認(rèn)情況下創(chuàng)建的表是內(nèi)部表,hive擁有該表的結(jié)構(gòu)和文件。
? ? ? ? 換句話說,hive完全管理表(元數(shù)據(jù)和數(shù)據(jù))的生命周期。當(dāng)刪除內(nèi)部表時,會刪除數(shù)據(jù)和表的元數(shù)據(jù)
外部表:
? ? ? ? 外部表中的數(shù)據(jù)不是hive擁有或管理的,只管理表元數(shù)據(jù)的生命周期。
? ? ? ? 要創(chuàng)建一個外部表,需要使用 EXTERNAL 語法關(guān)鍵字
? ? ? ? 刪除外部表只會刪除元數(shù)據(jù),而不會刪除實(shí)際數(shù)據(jù)。在Hive外部仍然可以訪問實(shí)際數(shù)據(jù)
? ? ? ? 實(shí)際場景中,外部表搭配location語法指定數(shù)據(jù)的路徑,可以讓數(shù)據(jù)更安全
問:如果選擇內(nèi)外表?
答: 1 當(dāng)需要通過Hive完全管理控制表的整個生命周期時,請使用內(nèi)部表;2 當(dāng)數(shù)據(jù)來之不易,防止誤刪,請使用外部表,因?yàn)榧词箘h除表,文件也會被保留
分區(qū)表
優(yōu)化,提高查詢效率
比如一張表里,有杭州,江蘇兩城的信息,但杭州只查詢杭州的信息,江蘇只查詢江蘇的信息,經(jīng)常會出現(xiàn)查詢條件 city='杭州' or city='江蘇'. 避免全表掃描查詢,可以用分區(qū)提高效率
hdfs 上的文件,庫/表/n分區(qū)(role=XXX)[/..分區(qū)]/文件
create table db.mytable(id int ,name string,city string,qu string)
partitioned by (cityname string comment '城市名稱分區(qū)',quname string comment '區(qū)縣名稱')
多分區(qū),一般采用兩級分區(qū),再多了數(shù)據(jù)約碎,不利于管理數(shù)據(jù)
靜態(tài)分區(qū)
load data local inpath '/mmm/hangzhou-shangchengqu.txt' into table db.mytable partition(cityname='hangzhou',quname ='shangchengqu');
查詢sql
select * from db.mytable where city = 'hangzhou'; -- 非分區(qū)表
select * from db.mytable where cityname = 'hangzhou' ; --分區(qū)
-- 查詢的結(jié)果中,有分區(qū)字段,但實(shí)際hdfs的數(shù)據(jù)中是沒有的
動態(tài)分區(qū): 分區(qū)的字段值是基于查詢結(jié)果(參數(shù)位置)自動推斷出來的 ,核心語法 insert + select
1 一張融合表
2 設(shè)置兩個參數(shù)
set hive.exec.dynamic.partition=true; -- 是否開啟動態(tài)分區(qū)
set hive.exec.dynamic.partition.mode=nonstrict; -- 指定分區(qū)模式,nonstrict非嚴(yán)格,strict嚴(yán)格,嚴(yán)格模式至少有一個為靜態(tài)分區(qū)
insert into table db.mytableDynamic partition(cityname,quname) select t.*,t.city,t.qu from db.mytable t;
-- 分區(qū)操作
新增分區(qū)
ADD PARTITION 會更改表元數(shù)據(jù),但不會加載數(shù)據(jù)。如果分區(qū)位置中不存在數(shù)據(jù),查詢時將不會返回結(jié)果
因此需要保證增加的分區(qū)位置路徑下,數(shù)據(jù)已經(jīng)存在,或者增加完分區(qū)后導(dǎo)入分區(qū)數(shù)據(jù)
ALTER TABLE table_name ADD PARTITION(dt='20270101') location '/user/hadoop/warehouse/..'
ALTER TABLE table_name ADD PARTITION(dt='20270101') location '/user/hadoop/warehouse/..' PARTITION(dt='20270102') location '/user/hadoop/warehouse/..'
重命名
ALTER TABLE table_name PARTITION partition_desc RENAME TO PARTITION partition_spec;
ALTER TABLE table_name PARTITION (dt='2008-08-09') RENAME TO PARTITION (dt='20080809');
刪除分區(qū)
ALTER TABLE table_name DROP [IF EXISTS] PARTITION (dt='2008-08-09')
ALTER TABLE table_name DROP [IF EXISTS] PARTITION (dt='2008-08-09') PURGE; --直接刪除數(shù)據(jù),不進(jìn)垃圾桶
修改分區(qū)
-- 更改分區(qū)文件存儲格式
ALTER TABLE table_name PARTITION(dt='2008-08-09') SET FILEFORMAT file_format;
-- 更改分區(qū)位置
ALTER TABLE table_name PARTITION(dt='2008-08-09') SET LOCATION 'new location';
修復(fù)分區(qū)
MSCK [REPAIR] TABLE table_name [ADD/DROP/SYNC PARTITIONS]
-- hdfs暴力新增分區(qū),hive元數(shù)據(jù)里沒有,用ADD PARTITIONS
-- drop同理
-- sync 等效于 add和drop
-- 如果存在大量未跟蹤的分區(qū),則可使用批量運(yùn)行MSCK REPAIR TABLE,以避免OOME(內(nèi)存不足)
eg:
MSCK REPAIR TABLE table_name ADD PARTITIONS;
分區(qū)表的注意事項(xiàng):
1 分區(qū)表不是建表的必要語法規(guī)則,是一種優(yōu)化手段,可選
2 分區(qū)字段不能是表中已有的字段,不能重復(fù)
3 分區(qū)字段是虛擬字段,其數(shù)據(jù)并不存儲在底層的文件中
4 分區(qū)字段值的確來自于用戶價值數(shù)據(jù)手動指定(靜態(tài)分區(qū))或者根據(jù)查詢結(jié)果位置自動推斷(動態(tài)分區(qū))
5 Hive支持多重分區(qū),也就是說在分區(qū)的基礎(chǔ)上繼續(xù)分區(qū),劃分更加細(xì)粒度
分桶表
? ? ? ? 分桶表也叫桶表,是一種用于優(yōu)化查詢二設(shè)計(jì)的表
? ? ? ? 分桶表對應(yīng)的數(shù)據(jù)文件在底層會被分解成若干個部分,通俗來說就是拆分成若干個獨(dú)立的小文件
? ? ? ? 在分桶時,要根據(jù)指定那個字段數(shù)據(jù)分成幾桶
? ? ? ? 桶編號相同的數(shù)據(jù)會被分到同一個桶當(dāng)中。
????????比如用id關(guān)聯(lián)查詢,可以用分桶,數(shù)值或者h(yuǎn)ashcode取模。
2.0之前需要指定開啟分桶
set hive.enforce.bucketing=true
create table t1(...)
clustered by(state) into 5 buckets;
在創(chuàng)建分桶表時,還可以指定分桶內(nèi)的數(shù)據(jù)排序
create table t1(...)
clustered by(state desc) into 5 buckets;
數(shù)據(jù)新增
1 創(chuàng)建一張全量表
2
insert into table_bucket select * from noBucketTable;
hdfs :對5取模,文件夾名稱:000000_0,000001_0,000002_0,000003_0,000004_0
分桶的好處:
1 基于分桶字段查詢時,減少全表掃描
2 JOIN時可以提高M(jìn)R程序效率,減少笛卡爾積數(shù)量
3 分桶表數(shù)據(jù)進(jìn)行高效抽樣
? ? ? ? 當(dāng)數(shù)據(jù)量特別大時,對全體數(shù)據(jù)進(jìn)行處理存在困難時,抽樣就顯得特別重要。抽樣可以從被抽取的數(shù)據(jù)中估計(jì)和推斷出整體的特性
簡單的DDl語句
------------- 數(shù)據(jù)庫 ---------------------------
創(chuàng)建新的數(shù)據(jù)庫
create
COMMENT 數(shù)據(jù)庫的注視說明語句
LOCATION 知道數(shù)據(jù)庫在HDFS存儲位置,默認(rèn)配置文件中的路徑(最好不要指定)
WITH DBPROPERTIES (property_nanme=property_value,...) 用于指定一些數(shù)據(jù)庫的屬性配置
切換數(shù)據(jù)庫
use
刪除數(shù)據(jù)庫,謹(jǐn)慎,庫下沒表才能刪除
drop (DATABASE|SCHEMA)(IF EXISTS)
CASCADE 可以刪除帶有表的數(shù)據(jù)庫
------------- 對表 ------------------------------
新建表
CREATE [TEMPORARY臨時表][EXTERNAL外部表] TABLE [IF NOT EXISTS] [db_name.]table_name [COMMENT col_comment](
col_name data_type [COMMENT col_comment],...
)
[COMMENT table_comment]
[PARTITION BY (col_name data_type[COMMENT col_comment],...)] --分區(qū)
[CLUSTERED BY (col_name,col_name,...)[SORTED BY (col_name [ASC|DESC],...)] INTO num_buckets BUCKETS] -- 分桶
[ROW FORMAT DELIMITED|SERDE ...]
[STORED as file_format]
[LOCATION hdfs_path]
[TBLPROPERTIES (property_name=property_value,...)]
ROW FORMAT DELIMITED
[FIELDS TERMINATED BY char] 字段之間分隔符
[COLLECTION ITEMS TERMINATED By char] 集合元素之間分隔符
[MAP KEYS TERMINATED BY char] Map映射kv之間分隔符
[LINES TERMINATED By char] 行數(shù)據(jù)之間分隔符
eg:
CREATE TABLE IF NOT EXISTS mydb.myUserTable(
id string comment 'id'
) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
CREATE TABLE IF NOT EXISTS mydb.myUserTable AS SELECT id,name FROM table2
默認(rèn)分隔符 \001
顯示庫
show databases;
顯示表
show tables [in database];
顯示元數(shù)據(jù)信息
desc formatted myUserTable;
describe formatted myUserTable;
注釋信息的中文亂碼—— 元數(shù)據(jù)存儲在mysql數(shù)據(jù)庫,默認(rèn)編碼,主要支持Latin編碼
Mysql數(shù)據(jù)庫執(zhí)行:
alter table hive3.COLUMNS_V2 modify column COMMENT varchar(256) character set utf8;
alter table hive3.TABLE_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
alter table hive3.PARTITION_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
alter table hive3.PARTITION_KEYS modify column PKEY_COMMENT varchar(4000) character set utf8;
alter table hive3.INDEX_PARAMS modify column PARAM__VALUE varchar(4000) character set utf8;
-- 原來的表的注釋無法修改,只能新建表才會生效
刪除表
drop table mydb.myUserTable;
Show
顯示表的屬性信息
show tblproperties table_name;
顯示表,視圖的創(chuàng)建語句
show create table [db_name.]table_name|view_name;
顯示表的所有列,包括分區(qū)列
show columns (from|in) table_name [(from|in) db_name];
show columns in student;
顯示當(dāng)前支持的所有自定義和內(nèi)置的函數(shù)
show functions;
DESC用法
查看表信息
desc extended table_name
查看表信息,格式化美觀
desc formatted table_name
查看數(shù)據(jù)庫相關(guān)信息
describe database_name;
DML
導(dǎo)入數(shù)據(jù):1 load 2 insert
新建的數(shù)據(jù)庫,在hdfs下面格式:${hive.metastore.warehouse.dir}/database.db/tablename/結(jié)構(gòu)化文件.txt
暴力,直接放上去:
hadoop fs -put xx.txt ${hive.metastore.warehouse.dir}/database.db/tablename/
hive推薦load
LOAD DATA [LOCAL] INPUT 'filepath' [OVERWRITE] INTO TABLE tablename;
-LOCAL 加上,后面跟的文件需要和hive的metastore一個服務(wù)器,不加則需要在hdfs的文件系統(tǒng)路徑里;文件移動是復(fù)制操作
-OVERWRITE 加上是覆蓋,不加是追加;文件是移動操作,原來的會沒有
3.0D的新特性:insert as select 查詢的最后一組列或load的數(shù)據(jù)里的最后一列組 作為分區(qū),無法轉(zhuǎn)換則報錯,還支持inputformat,
serDe指定輸入格式,例如Text,ORC等
load data local inpath 'pwd' into table tabl1;
INSERT 可用,但速度很慢
INSERT INTO TABLE tablename VALUES (1,2,3);
INSERT INTO TABLE tablename(id,name) VALUES (1,'zl');
INSERT INTO TABLE tablename SELECT id,name FROM tablename2; (推薦)
-- 一次遍歷多次插入
from student
insert overwrite table student_1 select num
insert overwrite table student_2 select name;
查詢?
DISTINCT 去重
GROUP BY 分組 ,搭配 HAVING + 聚合,出現(xiàn)在group by的select字段要么是group by分組字段,要么是被聚合函數(shù)應(yīng)用的字段
- HAVING 后跟 聚合函數(shù)條件,順序 where,group by ,having
LIMIT [offset,] rows 分頁,offset的偏移量從0開始
LIMIT 1,3:從第2行開始(包含),共3行
WHERE
- 判空 age IS NULL
- 區(qū)間 between 1500 and 3000 = >1500 and < 3000
- 數(shù)據(jù) in (1,2,3)
ORDER BY [ASC|DESC] 排序
聚合: COUNT(*)返回被選行數(shù),COUNT(column)返回某列不為NULL的行數(shù),SUM,MAX,MIN,AVG,length,reverse,concat,split,substr,current_date(),unix_timestamp(),datediff()日期比較,date_add()日期增加,date_sub()日期減少,round()四舍五入,rand()隨機(jī)函數(shù),if(),nvl()為空判斷,case when then [when then ] else end 條件判斷,等,不管多少行,聚合后只返回一條數(shù)據(jù)
count(distinct column):列存在重復(fù)數(shù)據(jù),去重后統(tǒng)計(jì)
AS 起別名
執(zhí)行順序:
from -》 where -》 group -》 having -》order -》 select
聚合函數(shù) 比 having 優(yōu)先執(zhí)行
where子查詢 優(yōu)先 聚合函數(shù)
join
inner join .. on = join .. on
left|right join .. on
full outer join = full join 全外連接或者外連接 = 左連接+右連接-重復(fù)行
left semi join 左半開連接,只返回左表的記錄,前提是其記錄對于右邊的表滿足on語句的判定條件(相當(dāng)于內(nèi)連接,只顯示左表信息)
cross join 返回被連接的兩個表的笛卡爾積,慎用
聚合函數(shù)
show functions; 查看當(dāng)下可用的函數(shù)
describe function extended functionname 來查看函數(shù)的使用方式
eg: describe function extended max
炸開作用,表生成函數(shù)
select explode(`array`(11,22,33));
select explode(`map`("id",123,"name","alias"));
select explode(years) from t1;
側(cè)視圖
select a.name,b.year
from t1 a lateral view explode(years) b as year
order by b.year desc;
- b 表別名
- year 列別名,map是多個 col1,col2
select a.name,count(b.year) as nums
from t1 a lateral view explode(years) b as year
group by a.name
order by nums desc;
?HIVE的函數(shù)分兩大類:內(nèi)置函數(shù),用戶自定義函數(shù)(UDF)
內(nèi)置函數(shù),例如聚合函數(shù)
用戶自定義函數(shù):UDF(普通函數(shù)一進(jìn)一出),UDAF(聚合函數(shù),多進(jìn)一出),UDTF(表生成函數(shù),一進(jìn)多出)
CTE
with q1 as (select num,name from student where num = 9898)
select * from q1;
with q1 as (select num,name from student where num = 9898)
from q1 select *;
with q1 as (select num,name from student where num = 9898)
q2 as (selct num from q1)
select * from (select num from q2) a;
with q1 as (select num,name from student where num = 9898)
q2 as (select num,name from student where num = 9899)
select * from q1 union all select * from q2;
with q1 as (select num,name from student where num = 9898)
from q1
insert overwrite table a1 select *;
create table s2 as
with q1 as (select * from student)
select * from q1;
create view v1 as
with q1 as (select * from student)
select * from q1;
Hive支持select導(dǎo)出數(shù)據(jù)
導(dǎo)出操作時一個OVERWRITE覆蓋操作,慎重
標(biāo)準(zhǔn)寫法
INSERT OVERWRITE [LOCAL] DIRECTORY directory1 [ROW FROMAT row_format][STORED AS file_format]
SELECT ... FROM ...
eg:
insert overwrite directory '/temp/xx' select * from student;
insert overwrite directory '/temp/xx' row format delimited fields terminated by ',' stored as orc select * from student ;
insert overwrite local directory '/root/xx' select * from student;
柚子快報邀請碼778899分享:HIVE 大數(shù)據(jù)學(xué)習(xí)
參考鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。