Hive個人心得筆記之表結構,數據類型,join
目錄
2.Full outer 全外連接 6 四條重複+2條沒提交的
一.連接-join
1.建表:
create external table order_t (id string,time string,pid string,amount int) row format delimited fields terminated by ' ' location '/order';
create external table product_t (pid string,name string,price int) row format delimited fields terminated by ' ' location '/product';
2.查詢:
select * from product_t join order_t on product_t.pid=order_t.pid;
3.inner join
select * from product_t inner join order_t on product_t.pid=order_t.pid;
4.left join
select * from product_t left join order_t on product_t.pid=order_t.pid;
5.right join
select * from product_t right join order_t on product_t.pid=order_t.pid;
6.Full outer join
select * from product_t full outer join order_t on product_t.pid=order_t.pid;
7.left semi join
select * from product_t left semi join order_t on product_t.pid=order_t.pid;
1.創建兩個外部表
2.創建語句
3.不指定默認內連接 inner join
1.假設 五條數據 成功提交四條 5 4 5
左外連接,右外連接,全外連接 LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
1.inner join 內連接 4 提交爲準
2.Full outer 全外連接 6 四條重複+2條沒提交的
3.Left/right 以左表,或者右表爲基準
4.笛卡爾積 25 相乘
二.內部表和外部表
一、內部表的概念
- 先在hive裏建一張表,然後向這個表插入數據(用insert可以插入數據,也可以通過加載外部文件方式來插入數據),這樣的表稱之爲hive的內部表
二、外部表的概念
- HDFS裏已經有數據了,然後,通過hive創建一張表來管理這個文件數據。則這樣表稱之爲外部表
- 注意,hive外部表管理的是HDFS裏的某一個目錄下的文件數據
三、外部表創建命令
- 進入hive,執行:create external table stu (id int,name string) row format delimited fields terminated by ' ' location '/目錄路徑'
四、內部表和外部表的區別
- 對於內部表,在刪除該表的時候,HDFS對應的目錄節點會被刪除
- 對於外部表,在刪除該表的時候,HDFS對應的目錄節點不會刪除
三.分區表
一、概述
- 分區表可以通過添加指定的字段來提高Hive的查詢效率
- 在數據量較大的情況下,往往會添加分區表來避免全表查詢
二、分區表指令
指令 |
作用 |
額外說明 |
create table book (id int, name string) partitioned by (category string) row format delimited fields terminated by '\t'; |
創建book表,以category作爲分區 |
在創建分區表時,partitioned字段可以不在字段列表中。生成的表中自動就會具有該字段。 |
load data local inpath '/home/cn.txt' overwrite into table book partition (category='cn'); |
將本地文件cn.txt添加到book表中,分區字段爲cn |
在HDFS下生成category=cn目錄 |
select * from book where category='cn'; |
查看分區爲cn的數據 |
|
ALTER TABLE book add PARTITION (category = 'jp') location '/user/hive/warehouse/park.db/book/category=jp'; |
將指定的目錄添加爲分區字段 |
|
show partitions iteblog; |
查看分區 |
|
msck repair table book; |
修復分區 |
|
alter table book drop partition(category='cn'); |
刪除分區 |
|
alter table book partition(category='french') rename to partition (category='hh'); |
修改分區的名字 |
|
四.分桶表
一、概述
- 分桶表是一種更細粒度的數據分配方式
- 一個表既可以分區也可以分桶
- 分桶的主要作用是實現數據的抽樣,方便進行數據測試
- 分桶表通過hash分桶算法,將數據分放在不同的桶(hdfs中的文件)中,方便後續獲取
- 分桶表機制默認是不開啓的,需要手動開啓:set hive.enforce.bucketing=true;
- 分桶表不允許以外部文件方式導入數據,只能從另外一張表數據導入
二、分桶表語法
指令 |
作用 |
額外說明 |
create table teacher(name string) clustered by (name) into 3 buckets row format delimited fields terminated by ' '; |
創建teacher表,以name作爲分桶機制,分爲3個桶 |
|
insert overwrite table teacher select * from tmp; |
將tmp表中的數據添加到teacher表中 |
實際上是產生了3個文件用於存儲不分桶的數據 |
select * from teacher tablesample(bucket 1 out of 3 on name); |
進行抽樣 |
抽樣格式爲:bucket x out of y on XXX
|
創建一個表,分桶爲6,計算name類型的hashcode的取模隨機分配一個桶中
在分桶結構中無法直接導入外部文件,需要重新創建一個表
生成數據
寫其他不認,寫*
1.單詞統計
拆分數據
把外部文件加載進來
以空格才分
切成單獨的字段
將切分數據放入一個臨時表,分組計數聚合
2.去掉非數字函數 打成JAR包
打成jar
添加jar
五.基本類型
Hive中的類型 |
Java中的類型 |
tinyint |
byte |
smallint |
short |
int |
int |
bigint |
long |
boolean |
boolean |
float |
float |
double |
double |
string |
String |
timestamp |
TimeStamp |
binary |
byte[] |
四.複雜類型
一、數組類型 array
1.建表語句:
- create external table ex(vals array<int>) row format delimited fields terminated by '\t' collection items terminated by ',' location '/ex';
2.查詢每行數組的個數,查詢語句:
- select size(vals) from ex;
注:hive 內置函數不具備查詢某個具體行的數組元素。需要自定義函數來實現,但這樣的需求在實際開發裏很少,所以不需要在意。
二、map類型
1.建表語句:
- create external table m1 (vals map<string,int>) row format delimited fields terminated by '\t' map keys terminated by ',' location '/map';
2.查詢語句:
- select vals['tom'] from m1;
- 查詢數據和非空查詢
注意:map類型,列的分割符必須是\t
3.查詢語句
- select vals['tom'] from ex where vals['tom'] is not null;
4.如果想做去重工作,可以調用distinct內置函數
- select distinct(ip) from (select vals['tom'] ip from ex where vals['tom'] is not null)ex1;
- 或者select distinct(vals['tom']) from m2 where vals['tom'] is not null;
三、struct 類型
1.建表語句:
-
create external table ex (vals struct<name:string,age:int>)row format delimited collection items terminated by ' ' location '/ex';
2.查詢語句:
- select vals.age from ex where vals.name='tom';
- 單個查詢
執行