1. 程式人生 > >hadoop,hive常用指令

hadoop,hive常用指令

hadoop-hdfs常用指令

1.安全模式常用操作命令:

hdfs dfsadmin -safemode leave //強制 NameNode 退出安全模式
hdfs dfsadmin -safemode enter //進入安全模式
hdfs dfsadmin -safemode get //檢視安全模式狀態
hdfs dfsadmin -safemode wait //等待,一直到安全模式結束

2.linux保證時間同步

ntpdate ‘時間伺服器’ 如:ntpdate ntp1.aliyun.com

3.網頁訪問驗證:

hdfs的網頁: namenode主機名:50070
hadoop01:50070
yarn的網頁:resourcemanager的主機名:8088
hadoop03:8088

4.單獨啟動某個程序

hdfs相關程序:
hadoop-daemon.sh start namenode
hadoop-daemon.sh start datanode
yarn相關程序:
yarn-daemon.sh start resourcemanager
yarn-daemon.sh start nodemanager

5.linux執行jar包 指令

右鍵-export-jar.file-選擇要打成jar包的檔案
01 02 03 04 05 06
hadoop jar wc.jar mr01.Driver /words.txt /out02
#03代表上傳的jar包名
#04代表主類的全限定名稱
#05代表輸入路徑(對應主類輸入路徑的args(0)引數)
#06代表輸出路徑(對應主類輸出路徑的args(1)引數)

6、檢視是否丟失資料的指令

hadoop fskc / 檢視根目錄下所有檔案的健康狀況,是否有損壞的資料塊

hive常用命令

1.啟動服務為後臺程序

1).nohup hiveserver2 1>>/home/hadoop/hiveserver.log 2>>/home/hadoop/hiveserver.err &
2).nohup hiveserver2 1> /home/hdp01/lg.log 2>&1 &

2.建立表的格式

1.CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], …)]
[COMMENT table_comment] //指定的是表的描述資訊
[PARTITIONED BY (col_name data_type [COMMENT col_comment], …)]//分割槽資訊
[CLUSTERED BY (col_name, col_name, …)//分桶資訊
[SORTED BY (col_name [ASC|DESC], …)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]

2.create table t_user(id string, name string) row format delimited fields terminated by ‘,’;

3.建表案例

建表案例:
hive中大小寫不敏感的
95002,劉晨,女,19,IS
1.建一個普通的內部表
create table if not exists student(id int,name string,sex string,age int comment ‘19-23’,department string) comment ‘1805student’ row format delimited fields terminated by ‘,’ STORED as TEXTFILE LOCATION ‘/user/hive/testhive’;
2.建一個外部表
create external table if not exists student_external(id int,name string,sex string,age int comment ‘19-23’,department string) row format delimited fields terminated by ‘,’;
注意:沒有一個表可以同時既是內部表又是外部表
3.建一個分割槽表 可以和內部表或外部表同時存在
分割槽欄位作為過濾條件的時候,減少查詢過程中的資料的掃描範圍
create external table student_ptn(id int,name string,sex string,department string) PARTITIONED BY(age int COMMENT ‘partition ziduan’) row format delimited fields terminated by ‘,’;
新增分割槽:
alter table student_ptn add partition(age=17);
4.建一個分桶表
create EXTERNAL table student_buk(id int,name string,sex string,age int,department string) CLUSTERED by (name) SORTED by(age desc) into 3 BUCKETS row format delimited fields terminated by ‘,’;
SORTED by指定的是每一個桶中的排序規則
5.ctas建表
create table as select … from …
student:
load data inpath ‘/stuin’ into table student;
create table student_d20 as select * from student where age>20;
select * from student_d20;
6.表複製 like
只會複製表結構 不會進行表的資料的複製
create table stu_like like student;

4.刪除資料庫:

空資料庫:drop database dbname;
刪除包含表的資料庫:drop database dbname cascade;
檢視hive所有內建函式:show functions;
檢視某一函式的具體使用方法:desc function extended functionname(如:desc function extended explode,檢視爆裂函式使用方法);

5.Hive中內部表與外部表的區別:

1)建立表時:建立內部表時,會將資料移動到資料倉庫指向的路徑;若建立外部表,僅記錄資料所在的路徑, 不對資料的位置做任何改變。
2)刪除表時:在刪除表的時候,內部表的元資料和資料會被一起刪除, 而外部表只刪除元資料,不刪除資料。這樣外部表相對來說更加安全些,資料組織也更加靈活,方便共享源資料。

6.檢視hive所有內建函式

show functions;

7.hive表的資料匯入

1.load方式
load data [local] inpath ‘’ into table tablename;
local:本地資料載入
如果資料從本地載入 需要新增這個關鍵字
如果資料從hdfs進行載入則不需要新增這個關鍵字
從本地:
load data local inpath ‘/home/hadoop/apps/stu’ into table student01;
相當於將本地的檔案 進行拷貝到student01表的hdfs的儲存目錄下
2.insert方式
1)insert into [table] name values();
2)單重資料插入 一次插入多條資料
insert into table tablename select … from … where …
insert into table stu_like01 select * from student01 where age=18; 掃描一次
insert into table stu_like01 select * from student01 where age=19; 掃描一次
insert into table stu_like01 select * from student01 where age=20; 掃描一次
3)多重資料插入
from tablename1
insert into table tablename select … where …
insert into table tablename select … where …
對原始資料表只掃描一次
分別插入資料到兩個表中
from student01
insert into table stu_like01 select * where age=18
insert into table stu_like02 select * where age=20;

8.hive的資料匯出

從hive表將資料匯出到本地或hdfs
1)單重資料匯出:
INSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement
local:將資料匯出到本地
將student01表中的年齡小於19的資料匯出到本地/home/hadoop/apps/stu_18
insert OVERWRITE local DIRECTORY ‘/home/hadoop/apps/stu_18’ select * from student01 where age<19;
2)多重資料匯出
FROM from_statement
INSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement1
[INSERT OVERWRITE [LOCAL] DIRECTORY directory2 select_statement2] …
from student01
insert OVERWRITE local DIRECTORY ‘/home/hadoop/apps/stu_19’ select * where age=19
insert OVERWRITE local DIRECTORY ‘/home/hadoop/apps/stu_20’ select * where age=20;

9.hive中的半連線semi join

半連線:出現的意義是補充hive中一個語句的缺陷
判斷左表中的關聯的欄位是否在右表中出現 如果出現則返回
left semi join 左半連線
select
*
from testa a left semi join testb b on a.id=b.id;
相當於內連線 只取左半表
在sql語句中有一個語法 in/exists 欄位1 in (ziduan …)
在hive中多 對in、exits這種語法支援很薄弱 支援但是效率很低 原因就是key的不好確定 這個時候就出現了半連線

10.hive查詢語句中的group by和常用的4個排序by

1.hive中的group by:

1)執行的順序select語句之前 select後給的欄位的別名是不可以使用的
student01 求每一個部門的年齡之和
select
department dept,sum(age) age
from student01 group by dept 錯錯錯

select
department dept,sum(age) age
from student01 group by department; 正確的

2)如果hql語句中有group by 那麼select後面只能跟兩種內容
1)group by的欄位
2)group by的聚合函式 sum count avg…
select
department dept,name,sum(age) age
from student01 group by department; 錯的

2.常用4個排序by

order by:

hive中的排序 這個排序指的值對hive的表資料進行全排序
對hive表student01表 按照年齡排序 升序
select * from student01 order by age;

sort by:

區域性排序 部分排序
只對每一個reducetask的輸出結果進行排序
select * from student01 sort by age;
當reducetask只有一個的時候 相當於全域性排序 這個時候和order by效果是一樣得
設定reducetask個數:
set mapreduce.job.reduces=3
select * from student01 order by age;//仍然是全域性排序
select * from student01 sort by age;
對於sort by 注意 reducetask的資料劃分是隨機取的一個列

distribute by:分桶 或 分組

類似於job.setNumReducetask()
不會進行排序,作用僅僅是分 將資料按照distribute by的欄位進行分開 最終分成幾部分取決於reducetask的個數 distribute by指定的僅僅是用於分的欄位是哪一個 每一個reducetask中資料分規則:distribute by後面的欄位%4
注意:distribute by後面的欄位數值:distribute by後面的欄位%4
distribute by後面的欄位非數值:distribute by後面的欄位。hashCode%4
set mapreduce.job.reduces=4;
select * from student01 distribute by age;
select * from student01 distribute by age sort by age;

cluster by

當distribute by和sort by的欄位一樣的時候=distribute by+sort by
先進行資料的分組:分組依據cluster by後面的欄位
在每一個組進行排序,排序的規則cluster by後面的欄位
select * from student01 cluster by age;
當distribute by+sort by的欄位相同時可以用cluster by代替兩者,但若兩者by的欄位不相同則不可以用cluster by替代的

11.hive中的資料型別相關知識

基本資料型別:int(tinyint,smallint,int,bigint)、float double、boolean、string、timestamp

複雜資料型別:有基本資料型別構成的
array:陣列資料型別 指定陣列的型別的時候一定給泛型<> 集合list set
map:對映 k-v
struct:類似於java中的物件型別
flowbean:手機號 上行流量 下行流量 總流量

array型別:

資料:
id name hobby
1 xh sing,movies,basketball
2 ls football,food
3 ww sleep,walk,food,chaojia
建表:
create table test_array(id int,name string,hobby array) row format delimited fields terminated by ‘\t’ collection items terminated by ‘,’;
collection items terminated by:指定的是集合的每個元素之間的分割符
資料載入:
load data local inpath ‘/home/hadoop/apps/test_array’ into table test_array;
資料查詢:
通過下標進行資料訪問
select hobby[0] from test_array;

map型別:指定泛型

資料:
id name piaofang
1 luhan paonan:1000,dadianying:100
2 hunagbo yichuhaoxi:10000,shansheng:20000,taijun:250000
3 xuzheng yaoshen:350000,zhubajie:60000

建表:
注意:建表時候指定分割符 === 從外向內依次指定
create table test_map(id int,name string,piaofang map<string,bigint>) row format delimited fields terminated by ‘\t’ collection items terminated by ‘,’ map keys terminated by ‘:’;
資料匯入:
load data local inpath ‘/home/hadoop/apps/test_map’ into table test_map;
資料查詢:
select piaofang[‘paonan’] from test_map;

struct:資料比較規程,資料中的每一個欄位對應的含義一樣

id name age
1 zs 23
2 ls 32
class {
id;
name;
age;
}
資料:
id name info
1 zs xian,20,680
2 zl wuhan,18,520
3 xh shenzheng,17,490
建表:
create table test_struct(id int,name string,info structchushengdi:string,age:int,score:int) row format delimited fields terminated by ‘\t’ collection items terminated by ‘,’;
collection items terminated by ‘,’:指定struct的每一個屬性之間的分割符
載入資料:
load data local inpath ‘/home/hadoop/apps/test_struct’ into table test_struct;
資料查詢:
通過.進行資料訪問
select info.chushengdi,info.score from test_struct;

hive的視窗函式

用於解決 topN 動態分組的問題 累加計算的問題等
樣例資料:
userid userdate count
A 2015-01 5
A 2015-01 15
B 2015-01 5
A 2015-01 8
B 2015-01 25
A 2015-01 5
A 2015-02 4
A 2015-02 6
B 2015-02 10
B 2015-02 5
A 2015-03 16
A 2015-03 22
B 2015-03 23
B 2015-03 10
B 2015-03 11
1)over子句
相當於單獨的一個計算語句 這個計算語句相當於重新取資料進行計算 可以指定分組條件和排序規則的 不是一定需要同時指定的 按需指定
over(distribute by sort by)
over(partition by order by)
1)聚合函式+over 版本2.1之後
count sum avg max min
這時候的聚合函式 是根據over子句指定的規則進行聚合的
select userid,userdate,sum(count) over(distribute by userid,userdate) from user_info;
select userid,userdate,sum(count) from user_info group by userid,userdate;

A 2015-01 5 33
A 2015-01 15 33
A 2015-01 8 33
A 2015-01 5 33
A 2015-02 6 10
A 2015-02 4 10
A 2015-03 16 38
A 2015-03 22 38
B 2015-01 25 30
B 2015-01 5 30
B 2015-02 10 15
B 2015-02 5 15
B 2015-03 11 44
B 2015-03 23 44
B 2015-03 10 44

去重:
select distinct userid,userdate,sum(count) over(distribute by userid,userdate) from user_info;
A 2015-01 33
A 2015-02 10
A 2015-03 38
B 2015-01 30
B 2015-02 15
B 2015-03 44

select distinct userid,userdate,sum(count) over(distribute by userid,userdate sort by userdate) from user_info;
A 2015-01 33
A 2015-02 10
A 2015-03 38
B 2015-01 30
B 2015-02 15
B 2015-03 44

2)分析函式:
row_number
rank
dense_rank
3)over子句中可以指定統計的資料的範圍
用於指定聚合函式的統計的資料的範圍的
(ROWS | RANGE 指定行) BETWEEN and指定行的範圍的
UNBOUNDED 無邊界 向前到第一條資料 向後最後資料
| [num]) PRECEDING 向前幾行的資料
CURRENT ROW 當前行
[num]) FOLLOWING) 向後幾行的資料
沒有指定需要統計的資料範圍 預設是全部的資料
select userid,userdate,count,sum(count) over(distribute by userid,userdate sort by userdate rows between CURRENT row and UNBOUNDED FOLLOWING) from user_info;

4)視窗函式和over子句連用
lag(需要取的欄位,偏移量,如果取不到 給的預設值)
偏移量 取前面的
lead 取後面的
比賽資料分析
這個人 賽成績
1 182828 6
2 272873 7

select userid,userdate,count,lag(count,1,0) over(distribute by userid,userdate sort by userdate) from user_info;
select userid,userdate,count,lead(count,1,0) over(distribute by userid,userdate sort by userdate) from user_info;
first_value(需要獲取的欄位) 分組的第一個值
last_value 分組的最後一個值
select userid,userdate,count,first_value(count) over(distribute by userid,userdate sort by count) from user_info;
select userid,userdate,count,last_value(count) over(distribute by userid,userdate sort by count) from user_info;

mysql寫的順序及執行順序

寫的順序:
select … from …where …group by…having…order by…limit
執行順序:
from
where
group by 不能使用select後面的欄位別名的 必須使用原聲的
having
select
order by 可以使用select後面的欄位的別名的
注:本部落格內容後續會不斷補充完善