學習筆記(2):大數據之Hive-基本查詢

立即學習: https://edu.csdn.net/course/play/8005/164134?utm_source=blogtoedu

導出數據到目錄

create table test3 as select id,name from test2 where province='hebei' and city='baoding';

--導出hive數據到本地目錄(下載)
insert overwrite local directory '/home/hadoop/hive' 
select * from hive1.test1 
where province='hebei';

--導出hive數據到hdfs目錄
insert overwrite directory '/user/hadoop/data' 
select * from hive1.test1 
where province='hebei';

--查詢數據向多個目錄同時輸送
from hive1.test2 t 
insert overwrite directory '/home/hadoop/hive/hebei' 
select * where t.province='hebei' 
insert overwrite directory '/home/hadoop/hive/henan' 
select * where t.province='henan';

查詢語句

select

select col1,col2 from table t;

函數運算

select upper(name) from test2; // name大寫
select lower(name) from test2; // name小寫
select age + 10 from test2;
select round(12.34); //12 四捨五入
select floor(12.34); //12 地板
select ceil(12.34); //13 天花板
select rand(10); //隨機數

聚合函數

select count(*) from test2;
select sum(age) from test2;
select avg(age) from test2;
select max(age) from test2;
select min(age) from test2;

去重

select count(distinct name) from test2;

首字符的 ascii 碼

select ascii("abc");

select ascii('abc');

進制編碼

select base64(binary('http://localhost:8080/helloworld'));

select binary('http://localhost:8080/helloworld');

類型轉換

select cast('120' as bigint) + 200;  //320

select '120' + 200;  //320.0

字符串拼接

select concat('ab','cd');  //abcd

分頁

select * from test2 limit 1,2; //從第二條開始,查出來兩條

where

from test2 e 
select e.id,e.name,e.age 
where e.city='baoding';

select e.id,e.name,e.age 
from test2 e  
where e.city='baoding';

case when then

select name,age, case 
when age<20 then 'low' 
when age>50 then 'old' 
else 'big' end 
as agestatus 
from hive1.test2;

注意!!! hive 中的 where 語句不能使用別名

--select id, name n ,age from test2 where n like 't%'; //wrong where中不能使用字段別名

數據範圍

select * from test2 where age between 12 and 24;
select * from test2 where age <=24 and age >12;

group by 分組查詢

select count(*),province 
from test2 
group by province;

select count(*) as c, province 
from test2 
group by province 
having c>3;

如何避免 map reduce 操作

不使用mr作業的模式就是本地作業,下面方法可以儘量避免mr作業。

  • 全表掃描,沒有where子句
select * from test;
  • where子句作用只有分區字段,也不需要mr(limit也不需要)
select * from test2 where province='hebei';
  • 設置該屬性後,hive會盡量使用local模式查詢
set hive.exec.mode.local.auto=true;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章