1. 程式人生 > >hive學習--建表語法

hive學習--建表語法

建立內部表

建立表語句:create table [if not existts] linuxidc_hive_log (  num string,  sn string,userkey string);

執行後:會在庫目錄下生成表目錄,預設分隔符為^A。

create table [if not existts] linuxidc_hive_log (  num string,  sn string,userkey string)

row format delimited fields terminated by ',';    指定表分隔符為 “,”。

刪除表: drop table 表名;

會刪除表資訊,

(內部表)還會從HDFS中刪除表目錄。-----引入內部表,外部表

建立外部表

外部表:可以任意指定對映目錄(表目錄路的路徑)。即不會對資料進行遷移。

create external table [if not existts] linuxidc_hive_log (  num string,  sn string,userkey string)

row format delimited fields terminated by ','   --指定表分隔符為 “,”。

location '/user/bushu07/flume/aaaa' ; -- 指定對映目錄為,不會再遷移到/hive/warehouse下

建立分割槽表:方便查詢

按照某一個標記分割槽。比如地址、時間等

create external table [if not existts] linuxidc_hive_log (  num string,  sn string,userkey string)

partitioned by (day string)   --按照day進行分割槽,自動建立分割槽子目錄

row format delimited fields terminated by ', '

location '/user/bushu07/flume/aaaa' ;

對應匯入分割槽資料時:

load data location inpath '/home/hadoop/logs/aaa.log' into table t_name partiton(分割槽欄位名='分割槽欄位內容');

注意:分割槽欄位不能是表定義中的已經存在的欄位

檢視分割槽表:select ..........from 表名 where 分割槽欄位名=分割槽欄位內容

建立兩個表結構一樣的表:create table 新表名 like 舊錶名;

建表時插入資料:create table 新表名 as select..........from 表名 where 判斷條件