HIVE sql使用總結

一、常用數據庫命令

1、查詢有哪些數據庫 show databases

 2、查詢有哪些數據表:show tables

3顯示所有函數: show functions

4使用use databasename;可以切換到某個數據庫下

示例(切換到test數據庫):use test

5、查看當前數據庫:select current_database()

6、查詢數據表有哪些字段及字段詳情:describe tablename

示例(查詢staged_employees數據表):describe staged_employees

可以簡寫爲desc staged_employees

結果如下:

 

7、查看指定數據庫裏有哪些數據表:SHOW TABLES IN DbName;

示例:查看xiaoxiao數據庫下面有哪些表

 SHOW TABLES IN xiaoxiao

8獲得表的建表語句

示例(查看創建emailtest這個表使用的語句方法):show create table emailtest

9查看數據庫的描述信息和文件目錄位置路徑信息

示例(查看datetest數據庫的描述信息和文件目錄位置信息):describe database datetest;

二、創建數據庫

創建xiaoxiao數據庫:

create database xiaoxiao;

 

 

二、創建數據表




create   table staged_employees (

id int   comment 'id',

user_name   string comment 'user name')

 

 

 

 

三、刪除數據庫

 

刪除數據庫的時候,不允許刪除有數據的數據庫,如果數據庫裏面有數據則會報錯。如果要忽略這些內容,則在後面增加CASCADE關鍵字,則忽略報錯,刪除數據庫。

 

DROP DATABASE DbName CASCADE(可選);

DROP DATABASE IF EXISTS DbName CASCADE;

 

 

 

三、刪除數據表

 

drop table staged_employees

 

 

 

四、刪除數據表中所有內容

刪除emaitest數據表中的所有內容。

insert overwrite table emailtest select * from emailtest where 1=0

 

 

 

五、更改表名

1、更改表名

-- 重命名錶名 ALTER TABLE table_name RENAME TO new_table_name;

 

六、數據表添加字段:

alter table lemailtest add columns(time int comment 'now time')

 

 

七、HIVE統計函數

1、count(1)與count(*)得到的結果一致,包含null值。count(字段)不計算null值

2集合統計函數

2.1 個數統計函數: count

語法: count(*), count(expr), count(DISTINCT expr[, expr_.])

返回值: int

說明: count(*)統計檢索出的行的個數,包括NULL值的行;count(expr)返回指定字段的非空值的個數;count(DISTINCTexpr[, expr_.])返回指定字段的不同的非空值的個數

舉例:

hive> select count(*) from lxw_dual;

20

hive> select count(distinct t) from lxw_dual;

10

2.2 總和統計函數: sum

語法: sum(col), sum(DISTINCT col)

返回值: double

說明: sum(col)統計結果集中col的相加的結果;sum(DISTINCT col)統計結果中col不同值相加的結果

舉例:

hive> select sum(t) from lxw_dual;

100

hive> select sum(distinct t) from lxw_dual;

70

 

七、插入數據到emailtest數據表中(追加數據到原有的數據表中)

讀取fx01數據表數據的一行,然後插入到emailtest數據表,對emailtest數據表原有的數據不會動的。

insert into table emailtest

select * from fx01 limit 1

 

八、插入數據倒emailtest數據表中(覆蓋原有數據表的數據,相當於把原有數據表先清空,再寫入新的數據)

fx01數據表讀取數據寫入到emailtest數據表,對原有emailtest數據表數據清空處理。

INSERT OVERWRITE TABLE emailtest

SELECT email,y,m,d

FROM fx01 where m=06 and d=19

 

九、關鍵詞匹配——like

 

1、從fx01表中查找列 content中包含"制度"的行數信息

Select * from fx01 where content like '%制度%'

 

十、根據某個關鍵字去匹配,然後去設置新的關鍵詞case…when..方法

1、case用法一:CASE  條件判斷函數 CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END

 

如果a等於b,那麼返回c;如果a等於d,那麼返回e;否則返回f

select policy,case policy when 'abc' then '測試' else 'ccc' end as policy from fx01 limit 6

 

2、case用法二:

假如要用到case when又要用到like這樣的功能,即如果字符串包含‘語文’就怎麼怎麼樣,包含‘數學’就怎麼怎麼樣,包含‘英語’就怎麼怎麼樣,like是用於where中的,放在case when裏面是無效的,可以用instr()這個函數來查找這些字符出現的位置,代替like的功能,這樣寫就好吶。 

case when instr(t.str,’語文’) > 0 then 0 

when instr(t.str,’語文’) > 0 then 1 

when instr(t.str,’語文’) > 0 then 2 

else 3 end 

示例:

select t1.policy,case when instr(t1.policy,'信託') > 0 then '信託'

when instr(t1.policy,'張三') > 0  then '張三1'

when instr(t1.policy,'李四') > 0  then '李四1'

when instr(t1.policy,'小明') > 0  then '小明1' else '小紅' end from (select distinct policy from fx01 limit 6) t1

 

 

十一、order by——TOP N

hive實現topN,使用order bylimit組合方式實現。Order by 是進行降序排列,limit是選取多少。默認按升序排列,如果要按降序排列在末尾加上DESCASC是升序。

取排名TOP 5的數據。

select distinct company,count(company) as num from fx01 where m=05 group by company order by num DESC limit5

 

十二、數據表多表連接——join

 

多表連接——join兩個以上的表

SELECT a.val, b.val, c.val FROM a JOIN b ON (a.key = b.key1) JOIN c ON (c.key = b.key2);

 

join支持left join(左連接)、right join(右連接)、full join(全連接)

 

1、兩表相關聯示例

select fx01_lanjie.company,fx01_lanjie.lanjie,fx01_yujing.yujing,fx01_lanjie.y,fx01_lanjie.m from fx01_lanjie left join fx01_yujing ON fx01_lanjie.company=fx01_yujing.company

 

2、三表相關聯示例

select fx01_lanjie.company,fx01_lanjie.lanjie,fx01_yujing.yujing,fx01_fangxing.fangxing,fx01_lanjie.y,fx01_lanjie.m from fx01_lanjie left join fx01_yujing ON fx01_lanjie.company=fx01_yujing.company  join fx01_fangxing on fx01_lanjie.company=fx01_fangxing.company

 

十三、實現某一列字段關鍵詞統計——split+explode函數

 

0  stu表數據:

 

stu:

id             name

hello,you zm2008

hello,me zm2015

 

 

1 實現單詞計數:     (列轉行)   ---> split切分+explode(炸開)

 

1.0 數據拆分成數組

select split(id,',') from  stu;    得到數組

[hello,you]

[hello,me]

 

1.1 繼續將數組拆分(hive explode函數會將數組繼續拆分成單個字符)

select explode(split(id,','))  from stu;   窗體函數

hello

you

hello

me

 

 

1.2 分組統計:

select t1.c1, count(1) from (select explode(split(id,',')) as c1  from stu)  t1 group by t1.c1;

hello 2

you 1

me 1

 

 

案例實現需求:統計“關鍵詞”這一列每個關鍵詞出現的次數,把數字去掉,只統計中文關鍵詞

表名:Testtable

ID

keword

y

m

d

1

北京;廣州;深圳;貴州

2017

2

8

2

重慶;河南

2017

2

5

7

12345555

2017

9

5

 


實現語句:

Select explode(split(keyword, ';')) as c1 from testtable  where keword' rlike '^[\\u4e00-\\u9fa5]+$'

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章