hive常用sql有哪些

本篇內(nèi)容介紹了“hive常用sql有哪些”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供古田企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為古田眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。

1.hive分組去重函數(shù)使用。

select  *,row_num() over(partition by id order by modifytime desc) rn from lyjtest  where rn=1;
 row_num() over函數(shù)對id做分區(qū)根據(jù)修改時間做降序,然后篩選出時間最新的一條(rn=1)的數(shù)據(jù),達到去重的效果。

2.hive 寫入數(shù)據(jù)
insert into table table2 select * from table1; --查詢table1中的數(shù)據(jù)寫入table2;
insert overwrite table table2 select * from table1;--覆蓋寫入

3.where和having的區(qū)別

//where是先限定性條件再分組(對原始數(shù)據(jù)過濾,where不能過濾聚合函數(shù))
hive> select count(*),age from table1 where id>18 group by age;

//having是先分組在限定條件(對每個組進行過濾,having后只能跟select中已有的列)
hive> select age,count(*) c from table1 group by age having c>2;

//where和having一起使用
select id,count(*) from table1 where id>18 group by id having count(*)>2;

4.hive只支持union all,不支持union
  union all 不去重
  select name,age from table1 where id<80
  union all
  select name,age from table2 where age>18;

5.查詢前五條數(shù)據(jù)
select * from table1 order by age desc limit 5; --查詢年齡最大的五條數(shù)據(jù)
select * from student limit 5;--隨機查詢五條數(shù)據(jù)

6.五種子句的嚴(yán)格順序

where → group by → having → order by → limit

7.distinct

//distinct關(guān)鍵字返回唯一不同的值(返回age和id均不相同的記錄)
hive> select distinct age,id from test;

8.復(fù)制表
create table test1_temp  like test1; --只復(fù)制表不包含數(shù)據(jù)
create  table test1 as select * from test2; --復(fù)制表復(fù)制數(shù)據(jù)到新表9.創(chuàng)建表

9.創(chuàng)建表

CREATE TABLE `lyjtest1`(     `id` double,   `name` string, `sex` string) 
COMMENT 'create table from sql'
ROW FORMAT SERDE  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'  
WITH SERDEPROPERTIES (  'field.delim'='\t',  'line.delim'='\n', 'serialization.format'='\t')   
STORED AS INPUTFORMAT   'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT   'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION   'hdfs://ambari1:8020/warehouse/tablespace/managed/hive/ods_lyjtest.db/lyjtest1' ;

“hive常用sql有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

網(wǎng)站標(biāo)題:hive常用sql有哪些
當(dāng)前URL:http://bm7419.com/article44/gighee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)站維護企業(yè)網(wǎng)站制作、、云服務(wù)器關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管