load:數(shù)據(jù)導(dǎo)入
load DATA [local] INPATH 'filepath' [overwrite] into table tablename [partition (partition1=val1,partition2=val2...)]
1,'filepath' 可以為/root/data/data1.txt一個(gè)文件,也可以是/root/data一個(gè)路徑,此時(shí)則會(huì)將改路徑下所有文件都會(huì)寫入到tablename表當(dāng)中
查詢
SELECT [ALL | DISTINCT] select_expr,select_expr,...
FROM table_reference
[WHERE where_condition]
[
GROUP BY col_list
| [DISTRIBUTE BY col_list] [ STORT BY col_list] (DISTRIBUTE 指定分發(fā)器,多Reducer可用)
| [ORDER BY col_list]
]
[LIMIT number]
字段解釋
[ALL | DISTINCT] # 查詢的字段
[WHERE where_condition] # where子句
# 類似我們傳統(tǒng)SQL的where 條件
# 目前支持 AND,OR ,0.9版本支持between
# IN, NOT IN
# 不支持EXIST ,NOT EXIST
[GROUP BY col_list [HAVING condition]] # 字段分組和having子句
[SORT BY| ORDER BY col_list] # 排序
# ORDER BY與SORT BY的不同
# ORDER BY 全局排序,只有一個(gè)Reduce任務(wù)
# SORT BY 只在本機(jī)做排序
[LIMIT number] # 限制查詢記錄數(shù)
fetch task
配置fetch task后—(當(dāng)使用簡(jiǎn)單的查詢語(yǔ)句(不含有排序、函數(shù))。不會(huì)生成mapreduce作業(yè),而直接使用fetch task在HDFS中查詢輸出)
1— set hive.fetch.task.conversion=more
2— hive --hiveconf hive.fetch.task.conversion=more
3— 修改hive-site.xml
1,2 在 hive下設(shè)置,當(dāng)退出時(shí)失效,3可以一直有效
where
where_condition:int型可以不用 " ",string要用(區(qū)分大小寫)
order by
set hive.groupby.orderby.position,alias=true
之后列可以直接使用數(shù)字來(lái)表示
eg:select col1,col2 from tablename oder by 4;
NULL
只能使用is來(lái)判斷,不能用=
nvl(colume,0),當(dāng)colum列為NULL時(shí),則改為
在order by排序中,升序顯示在最前(一般先設(shè)置為0),降序在最后
函數(shù)
1,內(nèi)置函數(shù)
round(,) ceil() float()
字符函數(shù):
lower(),upper(),substr(,,),trim(),lpad(),rpad()
收集函數(shù):size(map(<>,<>))
轉(zhuǎn)換函數(shù):cast()
日期函數(shù):to_data(),year(),month(),day(),weekofyear(),datediff(),data_add(),data_sub()
條件函數(shù):coalesce(),case..then...
2,聚合函數(shù):count(),sum(),max().min(),avg()
3,表生成函數(shù):explode(),把map集合,或者 數(shù)組中每個(gè)元素單獨(dú)生成一個(gè)行
4,java自定義函數(shù)
連接
1,等值連接(where連接條件為=)
select a.col1,a.col2,b.col1
from tablename1 a , tablename2 b
where a.col3=b.col2;
2,不等值連接
select a.col1,a.col2,b.col1
from tablename1 a , tablename2 b
where a.col3 between b.col1 and b.col2;
3,外連接
right outer join
left outer join
full join
outer join
inner join
4,自連接(表內(nèi)關(guān)系查詢)
select a.col1, b.col2
from tablename1 a,tablename1 b
where a.col1=b.col3
子查詢
Hive只支持where和from子句的子查詢
主查詢和子查詢可以不是同一張表
子查詢中的空值NULL問(wèn)題: 不能使用not in
select a.col1 from tablename1 a where a,col1 in (select b.tablename2 from tablename2 b where b.col2="val1")