1. Hive 常用函數(shù)
參考連接:https://blog.csdn.net/ddydavie/article/details/80667727
- 設(shè)置運(yùn)行內(nèi)存
set mapreduce.map.memory.mb = 1024;
set mapreduce.reduce.memory.mb = 1024;
- 設(shè)置變量
set hivevar: var1 = -0.3; --常數(shù)
set hivevar: pre_month = date_add(date1, -30);
- 刪除表
use xxxdb;
drop table table_name;
- 刪除分區(qū)
注意:若是外部表零抬,則還需要?jiǎng)h除文件(hadoop fs -rm -r -f hdfspath)
alter table table_name drop if exists partitions (d='2019-05-20');
- if 函數(shù)
if(boolean condition, a, b)
- 將字符串類型的數(shù)據(jù)讀取為json類型仇祭,并得到其中的元素key的值
get_json_object(string s, '$.key')
E.g. get_json_object(newexdata['data'], '$.module')
- Url解析
parse_url()
--返回'facebook.com'
parse_url('http://faceb
ook.com/path/p1.php?query=1','HOST')
-- 返回'/path/p1.php'
parse_url('http://facebook.com/path/p1.php?query=1','PATH')
-- 返回'query=1'
parse_url('http://facebook.com/path/p1.php?query=1','QUERY')
- 返回當(dāng)前時(shí)間
from_unixtime(unix_timestamp(). 'yyyy-MM-dd HH:mm:ss')
- 字符串截取
substring(string A祟同, int start嫡纠, int len)
- 字符串連接
concat(string A, string B, string C, ...)
- 自定義分隔符sep的字符串連接
concat_ws(string sep, string A, string B, string C, ...)
- 類型轉(zhuǎn)換
cast(expr as type)
- 返回第一個(gè)非空參數(shù)
coalesce()
- 將一列數(shù)據(jù)拆成多行數(shù)據(jù)
參考: https://blog.csdn.net/SunnyYoona/article/details/62894761
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)
fromClause: FROM baseTable (lateralView)
-- 例子
select pageid, adid
from tmp Lateral view explode(adid_list) adTable AS adid
-- find_in_set : The FIND_IN_SET() function returns the position of a string within a list of strings.
FIND_IN_SET(string, string_list)
2. hive的分區(qū)和分桶
參考鏈接: https://blog.csdn.net/wl1411956542/article/details/52931499
- 注意語句結(jié)構(gòu)需要加分號(hào);
- 創(chuàng)建一個(gè)分區(qū)表
create table invites(id int, name string) partitioned by (d string)
row fomat delimited
fileds terminated by '\t'
stored as textfile;
- 將數(shù)據(jù)添加到 '2019-01-11' 分區(qū)中
load data local inpath '/home/hadoop/Desktop/data.txt' overwrite into table invites partition(d = '2019-01-11');
- 從分區(qū)中查詢數(shù)據(jù)
select *
from invites
where d = '2019-01-11';
- 往一個(gè)分區(qū)表中的某個(gè)分區(qū)添加數(shù)據(jù)
insert overwrite tableinvites partition (d = '2019-01-11')
select *
from test;
- 查看分區(qū)情況
hadoop fs -ls /home/hadoop.hive/warehouse/invites
-- 或者
show partitions table name;
3. hive 桶(Bucket)
- 桶是更為細(xì)粒度的數(shù)據(jù)范圍劃分
- 獲得更高的查詢處理效率
- 取樣(sampling)更高效
- 使用 Clustered by 子句來劃分桶所用的列和桶的個(gè)數(shù)
- hive 使用 id 列上的值蝌衔, 除以桶數(shù)取余帘腹, id mod 4 (hash )
create table bucketed_user(id INT, nmae String)
Clustered by (id) INTO 4buckets;
- 插入數(shù)據(jù)
insert overwrite table bucketed_users
select * from users;
- 對(duì)桶中數(shù)據(jù)進(jìn)行采樣
select *
from bucketed_users
tablesample(bucket 1 out of 4 ON id)
-- 注:tablesample是抽樣語句畜挨,語法:TABLESAMPLE(BUCKET x OUTOF y)
-- y必須是table總bucket數(shù)的倍數(shù)或者因子筒繁。hive根據(jù)y的大小,決定抽樣的比例巴元。例
-- 如毡咏,table總共分了64份,當(dāng)y=32時(shí)逮刨,抽取(64/32=)2個(gè)bucket的數(shù)據(jù)呕缭,當(dāng)y=128時(shí),
-- 抽取(64/128=)1/2個(gè)bucket的數(shù)據(jù)。x表示從哪個(gè)bucket開始抽取恢总。例如迎罗,table總
-- bucket數(shù)為32,tablesample(bucket 3 out of 16)片仿,表示總共抽任瓢病(32/16=)2
-- 個(gè)bucket的數(shù)據(jù),分別為第3個(gè)bucket和第(3+16=)19個(gè)bucket的數(shù)據(jù)滋戳。
4. hive 中內(nèi)部表與外部表的區(qū)別
參考鏈接:http://www.aboutyun.com/thread-7458-1-1.html
5. hive 表操作
參考鏈接:https://blog.csdn.net/bingduanlbd/article/details/52076219
分區(qū)表
連續(xù)數(shù)據(jù)報(bào)表制作需要分區(qū)表
刪除分區(qū)
ALTER TABLE tablename DROP IF EXISTS PARTITION(d = '2019-01-23');刪除掉指定分區(qū)
嚴(yán)格模式
SET hive.mapred.mode = strict/nonstrict
-- 默認(rèn)為 nonstict非嚴(yán)格模式
? 查詢限制
? 1钻蔑、對(duì)于分區(qū)表,必須添加where對(duì)于分區(qū)字段的條件過濾
? 2奸鸯、order by語句必須包含limit輸出限制
? 3咪笑、限制執(zhí)行笛卡爾積的查詢