Hive 的啟動方式:
1、hive 命令行模式,直接輸入/hive/bin/hive的執行程序,或者輸入 hive - -service cli
2、hive web界面的啟動方式,hive - -service hwi
3、hive 遠程服務 (端口號10000) 啟動方式,nohup hive - -service hiveserver &
2、hive web界面的啟動方式,hive - -service hwi
3、hive 遠程服務 (端口號10000) 啟動方式,nohup hive - -service hiveserver &
創建表cite,並指定列的分隔符為“,”:
create table cite(citing INT, cited INT) row format delimited fields terminated by ',';
創建表page_view,並指定分區字段為dt,同時把列的分隔符設置為“,”:
create table page_view (pid INT, url string) partitioned by (dt string) row format delimited fields terminated by ',';
創建表S_USER_ERROR,並指定分區字段為YEAR、MONTH和DAY,同時把列的分隔符設置為“,”,最后指定表的存儲路徑為/data/stg/s_user_error/:
CREATE TABLE S_USER_ERROR
(
bar INT,
foo string
)
PARTITIONED BY (YEAR STRING,MONTH STRING,DAY STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\,' STORED AS TEXTFILE LOCATION '/data/stg/s_user_error/';
(
bar INT,
foo string
)
PARTITIONED BY (YEAR STRING,MONTH STRING,DAY STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\,' STORED AS TEXTFILE LOCATION '/data/stg/s_user_error/';
注意:如果不是外表部,drop table的時候會將HDFS上文件刪除。
創建外部表:
CREATE EXTERNAL TABLE lhttest (
id int,
name string
)
PARTITIONED BY (pt string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\,'
STORED AS TEXTFILE
LOCATION '/data/stg/lhttest';
id int,
name string
)
PARTITIONED BY (pt string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\,'
STORED AS TEXTFILE
LOCATION '/data/stg/lhttest';
注意:在刪除外部表的時候,不會刪除HDFS上的關聯文件。
把一個本地系統中的文件加載到visiter表的2010-06-08分區下:
load data local inpath '/root/user/myHiveTestFile/02' overwrite into table visiter PARTITION (dates='2010-06-08');
load data local inpath '/root/user/myHiveTestFile/02' overwrite into table visiter PARTITION (dates='2010-06-08');
查詢cite表的前3行記錄:
select * from cite limit 3;
描述表pv的結構:
描述表pv的結構:
Describe pv;
desc pv;
刪除表:
drop table lhttest;
把表page_view重命名為pv:
把表page_view重命名為pv:
Alter table page_view rename to pv;
給表pv添加一個名為IP的列:
給表pv添加一個名為IP的列:
Alter table pv add columns (ip string);
添加一個分區:
添加一個分區:
Alter table pv add partition (dt='2012-06-26');
把已經存在的一個目錄設置為表的一個分區:
alter table pv add partition (dt= '2012-06-27') location '/hive/warehouse/pv/2012-06-27';
顯示一個表的分區信息:
show partitions lhtstudent;
刪除一個分區:
Alter table pv drop partition (dt='2012-06-26');
從lhtstudent的分區2012-06-26中查出數據,插入到studentnum的名為2012-06-26的分區中:
insert overwrite table studentnum partition (dt='2012-06-26') select count(1) from lhtstudent where dt='2012-06-26'
取lhtstudent中字段name的前3個字符:
Select substr(name,0,3) as subname from lhtstudent;
對字段id進行去重操作:
Select distinct(id) from lhtstudent;
從lhtstudent的分區2012-06-26中查出數據,插入到studentnum的名為2012-06-26的分區中:
insert overwrite table studentnum partition (dt='2012-06-26') select count(1) from lhtstudent where dt='2012-06-26'
取lhtstudent中字段name的前3個字符:
Select substr(name,0,3) as subname from lhtstudent;
對字段id進行去重操作:
Select distinct(id) from lhtstudent;
通過Join關聯查詢(相當於inner join):
select distinct i.id,i.name,s.score from lhtstudent i join studentscore s on i.id = s.stuid;
通過left outer join關聯查詢(相當於sql的left join):
select i.id,i.name,s.score from lhtstudent i left outer join studentscore s on i.id = s.stuid;
通過right outer join關聯查詢(相當於sql的right join):
select i.id,i.name,s.score from lhtstudent i right outer join studentscore s on i.id = s.stuid;
通過order by 進行排序:
select * from lhtstudent order by id desc;
通過like進行模糊查詢:
select * from lhtstudent where name like '%liu%';
通過group by 進行排序:
select id from lhtstudent group by id;
通過case語句進行轉換:
select case dt when '2012-06-25' then '今天' when '2012-06-26' then '明天' end from lhtstudent;
select score , ( case when score >= 60 then '及格' when score < 60 then '不及格' end) from lhtstudent;
通過concat連接兩個字符串:
select concat(id,name) from lhtstudent;
upper()字符串轉大寫:
select upper(name) from lhtstudent;
lower()字符串小寫:
select lower(name) from lhtstudent;
length()取到字符串長度:
select name,length(name) from lhtstudent;
trim()去空格:
select trim(name) from lhtstudent;
year(string date)返回年:
select year('2012-07-11 12:30:50') from lhttest; //返回2012
month(string date) 返回月:
select month('2012-07-11 12:30:50') from lhttest;//返回7
day(string date) 返回日:
select day('2012-07-11 12:30:50') from lhttest;//返回11
dayofmonth(date)返回一個月中的第幾天:
select dayofmonth('2012-07-11 12:30:50') from lhttest;//返回11
hour(string date) 返回小時:
select hour('2012-07-11 12:30:50') from lhttest;//返回12
minute(string date) 返回分鍾:
select minute('2012-07-11 12:30:50') from lhttest;//返回30
second(string date) 返回秒:
select second('2012-07-11 12:30:50') from lhttest;//返回50
weekofyear(string date) 返回一年中的第幾周:
select weekofyear('2012-07-11 12:30:50') from lhttest;//返回28
datediff(string enddate, string startdate) 返回enddate和startdate的天數的差:
select datediff('2012-07-11','2012-07-09') from lhttest;//返回2
select datediff('2012-07-11','2012-07-12') from lhttest;//返回-1
date_add(string startdate, int days) 加days天數到startdate:
select date_add('2012-07-11',1) from lhttest;//返回2012-07-12
date_sub(string startdate, int days) 從startdate減去days天:
select date_sub('2012-07-11',1) from lhttest;//返回2012-07-10
to_date(string timestamp) 返回日期字符串:
select to_date('2012-07-12 10:30') from lhttest; //返回2012-07-12
unix_timestamp() 獲得當前時間戳:
select unix_timestamp() from lhttest;//返回當前時間的秒數
from_unixtime(int unixtime) 將時間戳(unix epoch秒數)轉換為日期時間字符串:
select from_unixtime(1341910120) from lhttest;//返回2012-07-10 16:48:40
select from_unixtime(0) from lhttest;//返回1970-01-01 00:00:00