HIVE小結 HIVE基本語法 HIVE和Mysql十分類似 建表規則 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], ...)] [COMMENT tabl ...
HIVE小結
HIVE基本語法
HIVE和Mysql十分類似
建表規則
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]
CREATE TABLE 創建一個指定名字的表。如果相同名字的表已經存在,則拋出異常;用戶可以用 IF NOT EXIST 選項來忽略這個異常
EXTERNAL 關鍵字可以讓用戶創建一個外部表,在建表的同時指定一個指向實際數據的路徑(LOCATION)
LIKE 允許用戶複製現有的表結構,但是不複製數據
COMMENT可以為表與欄位增加描述
創建表
hive> CREATE TABLE IF NOT EXISTS test1
> (id INT,name STRING);
刪除表
drop table test1;
查看表結構
desc test1;
修改表名
alter table test1 rename to test2;
修改表結構
alter table test1 add columns(address string ,grade string);
創建和已知表相同結構的表
create table test3 like test1;
載入本地數據
load date local inpath '/home/date/' into table test1;
註意可以在into 前面添加overwrite表示覆蓋之前在test1的數據,如果沒有就表示載入本地數據在原始數據的後面
載入hdfs的文件
首先將文件上傳到hdfs文件系統對對應的目錄上
hadoop fs -put /home/.txt /usr/
然後載入hdfs中的數據
load data inpath /usr/ into table test1;
插入數據
insert overwrite table test2 select * from test1;
查詢數據
和mysql語法上沒甚沒區別
- 查詢單個欄位的數據
- where條件查詢
- all和distinct
- limit限制查詢
- group by
- order by
- sort bu
- distribute by
- cluster by
HIVE分區
hive分區是為了更方便數據管理,常見的有時間分區和業分區
create table t1(
id int
,name string
,hobby array<string>
,add map<String,string>
)
partitioned by (pt_d string)
需要註意的是分區欄位不能和表中的欄位重覆,否則就會報錯:
FAILED: SemanticException [Error 10035]: Column repeated in partitioning columns
我們在載入數據的時候也可以分區載入
load data local inpath '/home/hadoop/Desktop/data' overwrite into table t1 partition ( pt_d = '201701');
之後我們再將同一份數據載入到不同的分區中
load data local inpath '/home/hadoop/Desktop/data' overwrite into table t1 partition ( pt_d = '000000');
查詢一下數據 select * from t1;
1 xiaoming ["book","TV","code"] {"beijing":"chaoyang","shagnhai":"pudong"} 000000
2 lilei ["book","code"] {"nanjing":"jiangning","taiwan":"taibei"} 000000
3 lihua ["music","book"] {"heilongjiang":"haerbin"} 000000
1 xiaoming ["book","TV","code"] {"beijing":"chaoyang","shagnhai":"pudong"} 201701
2 lilei ["book","code"] {"nanjing":"jiangning","taiwan":"taibei"} 201701
3 lihua ["music","book"] {"heilongjiang":"haerbin"} 201701
創建分區除了在創建表的時候啟動partition by實現,還可以
alter table t1 add partition (pt_d string)
這樣就創建了一個分區,這時會看到hive在hdfs中創建了相應的文件夾
查詢相應的分區的數據
select * from t1 where pt_d = ‘000000’
添加分區,增加一個分區文件
alter table t1 add partition (pt_d = ‘333333’);
刪除分區(刪除對應的分區文件)
註意,對於外表進行drop partition並不會刪除hdfs上的文件,並且通過msck repair table table_name同步回hdfs上的分區。
alter table test1 drop partition (pt_d = ‘20170101’);
查詢分區
show partitions table_name;
修複分區
修複分區就是重新同步hdfs上的分區信息。
msck repair table table_name;
插入數據
insert overwrite table partition_test partition(stat_date='2015-01-18',province='jiangsu')
select member_id,name from partition_test_input
where stat_date='2015-01-18'
and province='jiangsu';
內部表和外部表的區別
Hive中表與外部表的區別:
1、在導入數據到外部表,數據並沒有移動到自己的數據倉庫目錄下,也就是說外部表中的數據並不是由它自己來管理的!而表則不一樣;
2、在刪除表的時候,Hive將會把屬於表的元數據和數據全部刪掉;而刪除外部表的時候,Hive僅僅刪除外部表的元數據,數據是不會刪除的!
那麼,應該如何選擇使用哪種表呢?在大多數情況沒有太多的區別,因此選擇只是個人喜好的問題。但是作為一個經驗,如果所有處理都需要由Hive完成,那麼你應該創建表,否則使用外部表!