Hive基礎 案例

2021-08-03 16:20:50 字數 4396 閱讀 8406

h

ive shell

檢視所有資料庫:show databases;

建立資料庫:create database database_name;

切換資料庫:use database_name;

檢視所有表:show tables;

模糊查詢表:show tables like '*name*';

檢視所有的hive函式:show functions;

檢視table的表結構: desc tablesname ;   desc formatted table_name;

檢視表建立的指令碼:show create table uv_etltest; 

刪除表:drop table uv_etltest; 

檢視分割槽資訊:show partitions table_name; 

案例:countpv日誌。l=字段匹配正則:m\.fang\.com/news/\w+/03_\d+.html,且l=不包含sf_source=的每個標黃的id的pv和uv。

日誌格式:

8801b688-1492445566729-8c8230bd^u_50cfe454-1499154971810-2c970e6d^0^http^m.fang.com^/news/sh/03_25116147.html^sf_source=ttcollaborate^5^無^無^/^^0^無^122.194.3.70^0^1^0^1^[1]^無^2017-07-04^15^56^11^2017-07-04^15^56^11^^^

show databases;

create database uvtest;

use uvtest;

show tables;

desc uv_etltest;

show create table uv_etltest;

show partitions uv_etltest;

建立表:

create external table if not exists uv_etltest (

visitor string

,visit string

,isfirstvisit int

,locationprotocol string

,locationdomain string

,locationpath string

,locationparametersstring string

,referertype string

,refererprotocol string

,refererdomain string

,refererpath string

,refererparametersstring string

,searchgrouptype string

,searchkeyword string

,ip string

,openpagetype string

,usertype string

,userinfo string

,mouse int

,bids string

,bbssign string

,servertimedate date

,servertimehour string

,servertimeminute string

,servertimesecond string

,clienttimedate date

,clienttimehour string

,clienttimeminute string

,clienttimesecond string

,title string

,urlowner string

,pageid string )

partitioned by (logdate int)

row format delimited

fields terminated by '^'

lines terminated by '\n'

stored as textfile

新增分割槽

:alter table uv_etltest  add partition (logdate= 20170704)  location '/logs/uv/etlbeginchar/20170704/';

修改分割槽的location

:alter table uv_etltest partition (logdate='20170704') set location '/logs/uv/etlbeginchar/20170704/';

修改分割槽的資訊

: alter table table_name partition (logdate='20170704') rename to partition (logdate='20170504');

刪除分割槽

:alter table uv_etltest drop if exists partition (logdate='20170704');

查詢結果

結果存放在hdfs上,並以空格分開

set hive.merge.mapredfiles= true;

insert overwrite directory '/user/hive/warehouse/uvtest.db/uv_etltest' row format delimited fields terminated by "\t" 

結果存放在新的hive表中

set hive.merge.mapredfiles= true; 

create table uv_etltestresult as

修改表名稱

:alter table  uv_etltest1 rename to uv_etltestresult;

select * from uv_etltestresult where pv > 500;

hive 行列轉換案例

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的小案例

資料 record time 通話時間 imei 基站編號 cell 手機編號 drop num 掉話秒數 duration 通話持續總秒數 2011 07 13 00 00 00 08,356966,29448 37062,0,0,0,0,0,g,0 2011 07 13 00 00 00 08,...

hive案例調優

無效id在關聯時的資料傾斜問題 問題 日誌中常會出現資訊丟失,比如每日約為 20 億的全網日誌,其中的 user id 為主 鍵,在日誌收集過程中會丟失,出現主鍵為 null 的情況,如果取其中的 user id 和 bmw users 關聯,就會碰到資料傾斜的問題。原因是 hive 中,主鍵為 n...